chore(runner): update fetch job request

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu 2022-09-29 22:19:21 +08:00 committed by Jason Song
parent 82431d8e11
commit 7486d2ab91
5 changed files with 17 additions and 38 deletions

View File

@ -79,8 +79,6 @@ func runDaemon(ctx context.Context, task *runtime.Task) func(cmd *cobra.Command,
cli, cli,
runner.Run, runner.Run,
&client.Filter{ &client.Filter{
Kind: runtime.Kind,
Type: runtime.Type,
OS: cfg.Platform.OS, OS: cfg.Platform.OS,
Arch: cfg.Platform.Arch, Arch: cfg.Platform.Arch,
Capacity: cfg.Runner.Capacity, Capacity: cfg.Runner.Capacity,

2
go.mod
View File

@ -3,7 +3,7 @@ module gitea.com/gitea/act_runner
go 1.18 go 1.18
require ( require (
gitea.com/gitea/proto-go v0.0.0-20220925101213-1ac8a05257e1 gitea.com/gitea/proto-go v0.0.0-20220929140437-812ae50fdce4
github.com/avast/retry-go/v4 v4.1.0 github.com/avast/retry-go/v4 v4.1.0
github.com/bufbuild/connect-go v0.5.0 github.com/bufbuild/connect-go v0.5.0
github.com/docker/docker v20.10.17+incompatible github.com/docker/docker v20.10.17+incompatible

2
go.sum
View File

@ -27,6 +27,8 @@ gitea.com/gitea/act v0.0.0-20220922135643-52a5bba9e7fa h1:HHqlvfIvqFlny3sgJgAM1B
gitea.com/gitea/act v0.0.0-20220922135643-52a5bba9e7fa/go.mod h1:9W/Nz16tjfnWp7O5DUo3EjZBnZFBI/5rlWstX4o7+hU= gitea.com/gitea/act v0.0.0-20220922135643-52a5bba9e7fa/go.mod h1:9W/Nz16tjfnWp7O5DUo3EjZBnZFBI/5rlWstX4o7+hU=
gitea.com/gitea/proto-go v0.0.0-20220925101213-1ac8a05257e1 h1:JGApntYc07jawNxrxv1WhU6IHX0i73nqhloZlaUR5Nc= gitea.com/gitea/proto-go v0.0.0-20220925101213-1ac8a05257e1 h1:JGApntYc07jawNxrxv1WhU6IHX0i73nqhloZlaUR5Nc=
gitea.com/gitea/proto-go v0.0.0-20220925101213-1ac8a05257e1/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y= gitea.com/gitea/proto-go v0.0.0-20220925101213-1ac8a05257e1/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y=
gitea.com/gitea/proto-go v0.0.0-20220929140437-812ae50fdce4 h1:HW38qGi3yd/7eUk8ihkz+opF6YGb1uLn8d1ZCUaxNg8=
gitea.com/gitea/proto-go v0.0.0-20220929140437-812ae50fdce4/go.mod h1:hD8YwSHusjwjEEgubW6XFvnZuNhMZTHz6lwjfltEt/Y=
github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=

View File

@ -2,6 +2,7 @@ package poller
import ( import (
"context" "context"
"errors"
"time" "time"
"gitea.com/gitea/act_runner/client" "gitea.com/gitea/act_runner/client"
@ -11,6 +12,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var ErrDataLock = errors.New("Data Lock Error")
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, filter *client.Filter) *Poller { func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, filter *client.Filter) *Poller {
return &Poller{ return &Poller{
Client: cli, Client: cli,
@ -67,25 +70,32 @@ func (p *Poller) Poll(ctx context.Context, n int) error {
} }
func (p *Poller) poll(ctx context.Context, thread int) error { func (p *Poller) poll(ctx context.Context, thread int) error {
logger := log.WithField("thread", thread) l := log.WithField("thread", thread)
logger.Info("poller: request stage from remote server") l.Info("poller: request stage from remote server")
ctx, cancel := context.WithTimeout(ctx, 30*time.Second) ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel() defer cancel()
// request a new build stage for execution from the central // request a new build stage for execution from the central
// build server. // build server.
resp, err := p.Client.Request(ctx, connect.NewRequest(&runnerv1.RequestRequest{ resp, err := p.Client.FetchTask(ctx, connect.NewRequest(&runnerv1.FetchTaskRequest{
Kind: p.Filter.Kind, Kind: p.Filter.Kind,
Os: p.Filter.OS, Os: p.Filter.OS,
Arch: p.Filter.Arch, Arch: p.Filter.Arch,
Type: p.Filter.Type, Type: p.Filter.Type,
})) }))
if err == context.Canceled || err == context.DeadlineExceeded { if err == context.Canceled || err == context.DeadlineExceeded {
logger.WithError(err).Trace("poller: no stage returned") l.WithError(err).Trace("poller: no stage returned")
return nil return nil
} }
if err != nil && err == ErrDataLock {
l.WithError(err).Info("task accepted by another runner")
return nil
}
if err != nil { if err != nil {
l.WithError(err).Error("cannot accept task")
return err return err
} }

View File

@ -2,23 +2,13 @@ package runtime
import ( import (
"context" "context"
"errors"
"gitea.com/gitea/act_runner/client" "gitea.com/gitea/act_runner/client"
runnerv1 "gitea.com/gitea/proto-go/runner/v1" runnerv1 "gitea.com/gitea/proto-go/runner/v1"
"github.com/bufbuild/connect-go"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var ErrDataLock = errors.New("Data Lock Error")
// Defines the Resource Kind and Type.
const (
Kind = "pipeline"
Type = "docker"
)
// Runner runs the pipeline. // Runner runs the pipeline.
type Runner struct { type Runner struct {
Machine string Machine string
@ -30,28 +20,7 @@ type Runner struct {
func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
l := log. l := log.
WithField("task.id", task.Id) WithField("task.id", task.Id)
l.Info("start running pipeline") l.Info("start running pipeline")
// update machine in stage
task.Machine = s.Machine
data, err := s.Client.Detail(ctx, connect.NewRequest(&runnerv1.DetailRequest{
Task: task,
}))
if err != nil && err == ErrDataLock {
l.Info("task accepted by another runner")
return nil
}
if err != nil {
l.WithError(err).Error("cannot accept task")
return err
}
l.Info("task details fetched")
return s.run(ctx, data.Msg.Task)
}
func (s *Runner) run(ctx context.Context, task *runnerv1.Task) error {
return NewTask(task.Id, s.Client).Run(ctx, task) return NewTask(task.Id, s.Client).Run(ctx, task)
} }