chore(gRPC): add request interface client

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-09-01 15:09:22 +08:00
committed by Jason Song
parent 449388f3ab
commit f2fb8798fa
6 changed files with 58 additions and 21 deletions

View File

@ -10,7 +10,7 @@ import (
log "github.com/sirupsen/logrus"
)
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Runner) error, filter *client.Filter) *Poller {
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Stage) error, filter *client.Filter) *Poller {
return &Poller{
Client: cli,
Filter: filter,
@ -22,14 +22,14 @@ func New(cli client.Client, dispatch func(context.Context, *runnerv1.Runner) err
type Poller struct {
Client client.Client
Filter *client.Filter
Dispatch func(context.Context, *runnerv1.Runner) error
Dispatch func(context.Context, *runnerv1.Stage) error
routineGroup *routineGroup
}
func (p *Poller) Poll(ctx context.Context, n int) error {
// register new runner.
runner, err := p.Client.Register(ctx, &runnerv1.RegisterRequest{
_, err := p.Client.Register(ctx, &runnerv1.RegisterRequest{
Os: p.Filter.OS,
Arch: p.Filter.Arch,
Capacity: int64(p.Filter.Capacity),
@ -52,7 +52,7 @@ func (p *Poller) Poll(ctx context.Context, n int) error {
log.Infof("stopping the runner: %d", i+1)
return
}
if err := p.poll(ctx, runner, i+1); err != nil {
if err := p.poll(ctx, i+1); err != nil {
log.WithError(err).Error("poll error")
}
}
@ -64,11 +64,34 @@ func (p *Poller) Poll(ctx context.Context, n int) error {
return nil
}
func (p *Poller) poll(ctx context.Context, runner *runnerv1.Runner, thread int) error {
func (p *Poller) poll(ctx context.Context, thread int) error {
log.WithField("thread", thread).Info("poller: request stage from remote server")
// TODO: fetch the job from remote server
time.Sleep(time.Second)
return p.Dispatch(ctx, runner)
// request a new build stage for execution from the central
// build server.
stage, err := p.Client.Request(ctx, &runnerv1.RequestRequest{
Kind: p.Filter.Kind,
Os: p.Filter.OS,
Arch: p.Filter.Arch,
Type: p.Filter.Type,
})
if err == context.Canceled || err == context.DeadlineExceeded {
log.WithError(err).Trace("poller: no stage returned")
return nil
}
if err != nil {
log.WithError(err).Error("poller: cannot request stage")
return err
}
// exit if a nil or empty stage is returned from the system
// and allow the runner to retry.
if stage == nil || stage.Id == 0 {
return nil
}
return p.Dispatch(ctx, stage)
}