2022-08-14 02:34:19 +00:00
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-14 03:17:55 +00:00
|
|
|
"time"
|
2022-08-14 02:34:19 +00:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-08-17 06:26:58 +00:00
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
2022-08-14 02:59:09 +00:00
|
|
|
|
2022-08-14 02:34:19 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-28 06:05:56 +00:00
|
|
|
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Runner) error, filter *client.Filter) *Poller {
|
2022-08-14 02:34:19 +00:00
|
|
|
return &Poller{
|
|
|
|
Client: cli,
|
2022-08-28 06:05:56 +00:00
|
|
|
Filter: filter,
|
2022-08-14 05:29:00 +00:00
|
|
|
Dispatch: dispatch,
|
2022-08-14 02:34:19 +00:00
|
|
|
routineGroup: newRoutineGroup(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Poller struct {
|
2022-08-14 05:29:00 +00:00
|
|
|
Client client.Client
|
2022-08-28 06:05:56 +00:00
|
|
|
Filter *client.Filter
|
|
|
|
Dispatch func(context.Context, *runnerv1.Runner) error
|
2022-08-14 02:34:19 +00:00
|
|
|
|
|
|
|
routineGroup *routineGroup
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:05:56 +00:00
|
|
|
func (p *Poller) Poll(ctx context.Context, n int) error {
|
|
|
|
// register new runner.
|
|
|
|
runner, err := p.Client.Register(ctx, &runnerv1.RegisterRequest{
|
|
|
|
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 02:34:19 +00: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-08-28 06:05:56 +00:00
|
|
|
if err := p.poll(ctx, runner, i+1); err != nil {
|
2022-08-14 02:34:19 +00:00
|
|
|
log.WithError(err).Error("poll error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
p.routineGroup.Wait()
|
2022-08-28 06:05:56 +00:00
|
|
|
return nil
|
2022-08-14 02:34:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 06:05:56 +00:00
|
|
|
func (p *Poller) poll(ctx context.Context, runner *runnerv1.Runner, thread int) error {
|
2022-08-14 02:34:19 +00:00
|
|
|
log.WithField("thread", thread).Info("poller: request stage from remote server")
|
|
|
|
|
2022-08-14 03:17:55 +00:00
|
|
|
// TODO: fetch the job from remote server
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2022-08-28 06:05:56 +00:00
|
|
|
return p.Dispatch(ctx, runner)
|
2022-08-14 02:34:19 +00:00
|
|
|
}
|