2022-08-13 21:34:19 -05:00
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-14 00:29:00 -05:00
|
|
|
"errors"
|
2022-08-13 22:17:55 -05:00
|
|
|
"time"
|
2022-08-13 21:34:19 -05:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-08-17 01:26:58 -05:00
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
2022-08-13 21:59:09 -05:00
|
|
|
|
2022-08-13 21:34:19 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-17 01:25:14 -05:00
|
|
|
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Stage) error) *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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Poller struct {
|
2022-08-14 00:29:00 -05:00
|
|
|
Client client.Client
|
2022-08-17 01:25:14 -05:00
|
|
|
Dispatch func(context.Context, *runnerv1.Stage) error
|
2022-08-13 21:34:19 -05:00
|
|
|
|
|
|
|
routineGroup *routineGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) Poll(ctx context.Context, n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
func(i int) {
|
|
|
|
p.routineGroup.Run(func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Infof("stopped the runner: %d", i+1)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
log.Infof("stopping the runner: %d", i+1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.poll(ctx, i+1); err != nil {
|
|
|
|
log.WithError(err).Error("poll error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
p.routineGroup.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) poll(ctx context.Context, thread int) error {
|
|
|
|
log.WithField("thread", thread).Info("poller: request stage from remote server")
|
|
|
|
|
2022-08-13 22:17:55 -05:00
|
|
|
// TODO: fetch the job from remote server
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2022-08-14 00:29:00 -05:00
|
|
|
// request a new build stage for execution from the central
|
|
|
|
// build server.
|
|
|
|
stage, err := p.Client.Request(ctx)
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(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
|
|
|
|
}
|
|
|
|
|
|
|
|
if stage == nil || stage.BuildUuid == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Dispatch(ctx, stage)
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|