2022-08-14 03:34:19 +01:00
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-14 04:17:55 +01:00
|
|
|
"time"
|
2022-08-14 03:34:19 +01:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-08-17 07:26:58 +01:00
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
2022-08-14 03:59:09 +01:00
|
|
|
|
2022-08-14 03:34:19 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-09-01 08:09:22 +01:00
|
|
|
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Stage) error, filter *client.Filter) *Poller {
|
2022-08-14 03:34:19 +01:00
|
|
|
return &Poller{
|
|
|
|
Client: cli,
|
2022-08-28 07:05:56 +01:00
|
|
|
Filter: filter,
|
2022-08-14 06:29:00 +01:00
|
|
|
Dispatch: dispatch,
|
2022-08-14 03:34:19 +01:00
|
|
|
routineGroup: newRoutineGroup(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Poller struct {
|
2022-08-14 06:29:00 +01:00
|
|
|
Client client.Client
|
2022-08-28 07:05:56 +01:00
|
|
|
Filter *client.Filter
|
2022-09-01 08:09:22 +01:00
|
|
|
Dispatch func(context.Context, *runnerv1.Stage) error
|
2022-08-14 03:34:19 +01:00
|
|
|
|
|
|
|
routineGroup *routineGroup
|
|
|
|
}
|
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
func (p *Poller) Poll(ctx context.Context, n int) error {
|
|
|
|
// register new runner.
|
2022-09-01 08:09:22 +01:00
|
|
|
_, err := p.Client.Register(ctx, &runnerv1.RegisterRequest{
|
2022-08-28 07:05:56 +01:00
|
|
|
Os: p.Filter.OS,
|
|
|
|
Arch: p.Filter.Arch,
|
|
|
|
Capacity: int64(p.Filter.Capacity),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("poller: cannot register new runner")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-14 03:34:19 +01:00
|
|
|
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
|
|
|
|
}
|
2022-09-01 08:09:22 +01:00
|
|
|
if err := p.poll(ctx, i+1); err != nil {
|
2022-09-03 08:59:44 +01:00
|
|
|
log.WithField("thread", i+1).
|
|
|
|
WithError(err).Error("poll error")
|
2022-08-14 03:34:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
p.routineGroup.Wait()
|
2022-08-28 07:05:56 +01:00
|
|
|
return nil
|
2022-08-14 03:34:19 +01:00
|
|
|
}
|
|
|
|
|
2022-09-01 08:09:22 +01:00
|
|
|
func (p *Poller) poll(ctx context.Context, thread int) error {
|
2022-09-03 08:59:44 +01:00
|
|
|
logger := log.WithField("thread", thread)
|
|
|
|
logger.Info("poller: request stage from remote server")
|
2022-08-14 03:34:19 +01:00
|
|
|
|
2022-09-03 08:59:44 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
|
|
defer cancel()
|
2022-08-14 04:17:55 +01:00
|
|
|
|
2022-09-01 08:09:22 +01:00
|
|
|
// 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 {
|
2022-09-03 08:59:44 +01:00
|
|
|
logger.WithError(err).Trace("poller: no stage returned")
|
2022-09-01 08:09:22 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-03 08:57:53 +01:00
|
|
|
// FIXME: for testing task
|
|
|
|
// stage.Id = 111
|
|
|
|
// stage.BuildId = 1222
|
|
|
|
|
|
|
|
// set client to context, so that the stage can use it to
|
|
|
|
ctx = client.WithClient(ctx, p.Client)
|
|
|
|
|
2022-09-01 08:09:22 +01:00
|
|
|
return p.Dispatch(ctx, stage)
|
2022-08-14 03:34:19 +01:00
|
|
|
}
|