2022-08-13 21:34:19 -05:00
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-29 09:19:21 -05:00
|
|
|
"errors"
|
2022-11-16 20:49:32 -06:00
|
|
|
"sync"
|
2022-08-13 22:17:55 -05:00
|
|
|
"time"
|
2022-08-13 21:34:19 -05:00
|
|
|
|
2022-12-06 02:37:38 -06:00
|
|
|
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
|
2022-09-25 05:54:00 -05:00
|
|
|
"github.com/bufbuild/connect-go"
|
2022-08-13 21:34:19 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-04-02 09:41:48 -05:00
|
|
|
|
|
|
|
"codeberg.org/forgejo/runner/client"
|
|
|
|
"codeberg.org/forgejo/runner/config"
|
2022-08-13 21:34:19 -05:00
|
|
|
)
|
|
|
|
|
2022-11-10 23:51:00 -06:00
|
|
|
var ErrDataLock = errors.New("Data Lock Error")
|
2022-09-29 09:19:21 -05:00
|
|
|
|
2023-04-02 09:41:48 -05:00
|
|
|
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, cfg *config.Config) *Poller {
|
2022-08-13 21:34:19 -05:00
|
|
|
return &Poller{
|
|
|
|
Client: cli,
|
2022-08-14 00:29:00 -05:00
|
|
|
Dispatch: dispatch,
|
2022-08-13 21:34:19 -05:00
|
|
|
routineGroup: newRoutineGroup(),
|
2022-11-11 01:00:38 -06:00
|
|
|
metric: &metric{},
|
2022-11-17 03:32:10 -06:00
|
|
|
ready: make(chan struct{}, 1),
|
2023-04-02 09:41:48 -05:00
|
|
|
cfg: cfg,
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Poller struct {
|
2022-08-14 00:29:00 -05:00
|
|
|
Client client.Client
|
2022-09-25 05:54:00 -05:00
|
|
|
Dispatch func(context.Context, *runnerv1.Task) error
|
2022-08-13 21:34:19 -05:00
|
|
|
|
2022-11-16 20:49:32 -06:00
|
|
|
sync.Mutex
|
2022-11-11 02:57:41 -06:00
|
|
|
routineGroup *routineGroup
|
|
|
|
metric *metric
|
2022-11-16 20:49:32 -06:00
|
|
|
ready chan struct{}
|
2023-04-02 09:41:48 -05:00
|
|
|
cfg *config.Config
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:49:32 -06:00
|
|
|
func (p *Poller) schedule() {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
2023-04-02 09:41:48 -05:00
|
|
|
if int(p.metric.BusyWorkers()) >= p.cfg.Runner.Capacity {
|
2022-11-16 20:49:32 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p.ready <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 19:10:52 -06:00
|
|
|
func (p *Poller) Wait() {
|
|
|
|
p.routineGroup.Wait()
|
|
|
|
}
|
|
|
|
|
2023-03-28 10:51:38 -05:00
|
|
|
func (p *Poller) handle(ctx context.Context, l *log.Entry) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
l.Errorf("handle task panic: %+v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
task, err := p.pollTask(ctx)
|
|
|
|
if task == nil || err != nil {
|
|
|
|
if err != nil {
|
|
|
|
l.Errorf("can't find the task: %v", err.Error())
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
p.metric.IncBusyWorker()
|
|
|
|
p.routineGroup.Run(func() {
|
|
|
|
defer p.schedule()
|
|
|
|
defer p.metric.DecBusyWorker()
|
|
|
|
if err := p.dispatchTask(ctx, task); err != nil {
|
|
|
|
l.Errorf("execute task: %v", err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:49:32 -06:00
|
|
|
func (p *Poller) Poll(ctx context.Context) error {
|
|
|
|
l := log.WithField("func", "Poll")
|
|
|
|
|
|
|
|
for {
|
|
|
|
// check worker number
|
|
|
|
p.schedule()
|
|
|
|
|
|
|
|
select {
|
|
|
|
// wait worker ready
|
|
|
|
case <-p.ready:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-28 10:51:38 -05:00
|
|
|
p.handle(ctx, l)
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:49:32 -06:00
|
|
|
func (p *Poller) pollTask(ctx context.Context) (*runnerv1.Task, error) {
|
|
|
|
l := log.WithField("func", "pollTask")
|
2022-09-29 09:19:21 -05:00
|
|
|
l.Info("poller: request stage from remote server")
|
2022-08-13 21:34:19 -05:00
|
|
|
|
2022-10-14 05:30:36 -05:00
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
2022-09-03 02:59:44 -05:00
|
|
|
defer cancel()
|
2022-08-13 22:17:55 -05:00
|
|
|
|
2022-09-01 02:09:22 -05:00
|
|
|
// request a new build stage for execution from the central
|
|
|
|
// build server.
|
2022-10-14 05:30:36 -05:00
|
|
|
resp, err := p.Client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{}))
|
2022-09-01 02:09:22 -05:00
|
|
|
if err == context.Canceled || err == context.DeadlineExceeded {
|
2022-09-29 09:19:21 -05:00
|
|
|
l.WithError(err).Trace("poller: no stage returned")
|
2022-11-16 20:49:32 -06:00
|
|
|
return nil, nil
|
2022-09-01 02:09:22 -05:00
|
|
|
}
|
2022-09-29 09:19:21 -05:00
|
|
|
|
|
|
|
if err != nil && err == ErrDataLock {
|
|
|
|
l.WithError(err).Info("task accepted by another runner")
|
2022-11-16 20:49:32 -06:00
|
|
|
return nil, nil
|
2022-09-29 09:19:21 -05:00
|
|
|
}
|
|
|
|
|
2022-09-01 02:09:22 -05:00
|
|
|
if err != nil {
|
2022-09-29 09:19:21 -05:00
|
|
|
l.WithError(err).Error("cannot accept task")
|
2022-11-16 20:49:32 -06:00
|
|
|
return nil, err
|
2022-09-01 02:09:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit if a nil or empty stage is returned from the system
|
|
|
|
// and allow the runner to retry.
|
2022-09-25 05:54:00 -05:00
|
|
|
if resp.Msg.Task == nil || resp.Msg.Task.Id == 0 {
|
2022-11-16 20:49:32 -06:00
|
|
|
return nil, nil
|
2022-09-01 02:09:22 -05:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:49:32 -06:00
|
|
|
return resp.Msg.Task, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) dispatchTask(ctx context.Context, task *runnerv1.Task) error {
|
|
|
|
l := log.WithField("func", "dispatchTask")
|
|
|
|
defer func() {
|
|
|
|
e := recover()
|
|
|
|
if e != nil {
|
|
|
|
l.Errorf("panic error: %v", e)
|
|
|
|
}
|
2022-11-11 01:00:38 -06:00
|
|
|
}()
|
|
|
|
|
2023-04-02 09:41:48 -05:00
|
|
|
runCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.Timeout)
|
2022-11-16 20:49:32 -06:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return p.Dispatch(runCtx, task)
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|