2022-08-13 21:34:19 -05:00
|
|
|
package poller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-01 23:33:17 -05:00
|
|
|
"encoding/json"
|
2022-09-29 09:19:21 -05:00
|
|
|
"errors"
|
2022-10-01 23:33:17 -05:00
|
|
|
"os"
|
2022-08-13 22:17:55 -05:00
|
|
|
"time"
|
2022-08-13 21:34:19 -05:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-10-01 23:33:17 -05:00
|
|
|
"gitea.com/gitea/act_runner/config"
|
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-10-01 23:33:17 -05:00
|
|
|
"github.com/appleboy/com/file"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-09-29 09:19:21 -05:00
|
|
|
var ErrDataLock = errors.New("Data Lock Error")
|
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, filter *client.Filter) *Poller {
|
2022-08-13 21:34:19 -05:00
|
|
|
return &Poller{
|
|
|
|
Client: cli,
|
2022-08-28 01:05:56 -05:00
|
|
|
Filter: filter,
|
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-28 01:05:56 -05:00
|
|
|
Filter *client.Filter
|
2022-09-25 05:54:00 -05:00
|
|
|
Dispatch func(context.Context, *runnerv1.Task) error
|
2022-08-13 21:34:19 -05:00
|
|
|
|
|
|
|
routineGroup *routineGroup
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:33:17 -05:00
|
|
|
type runner struct {
|
|
|
|
id int64
|
|
|
|
uuid string
|
|
|
|
name string
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) Register(ctx context.Context, cfg config.Runner) error {
|
|
|
|
// check .runner config exist
|
|
|
|
if file.IsFile(".runner") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-28 01:05:56 -05:00
|
|
|
// register new runner.
|
2022-10-01 23:33:17 -05:00
|
|
|
resp, err := p.Client.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{
|
|
|
|
Os: p.Filter.OS,
|
|
|
|
Arch: p.Filter.Arch,
|
|
|
|
Labels: p.Filter.Labels,
|
|
|
|
Name: cfg.Name,
|
|
|
|
Token: cfg.Token,
|
2022-09-25 05:54:00 -05:00
|
|
|
}))
|
2022-08-28 01:05:56 -05:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("poller: cannot register new runner")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-01 23:33:17 -05:00
|
|
|
data := &runner{
|
|
|
|
id: resp.Msg.Runner.Id,
|
|
|
|
uuid: resp.Msg.Runner.Uuid,
|
|
|
|
name: resp.Msg.Runner.Name,
|
|
|
|
token: resp.Msg.Runner.Token,
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("poller: cannot marshal the json input")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store runner config in .runner file
|
|
|
|
return os.WriteFile(".runner", file, 0o644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Poller) Poll(ctx context.Context, n int) error {
|
2022-08-13 21:34:19 -05: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 02:09:22 -05:00
|
|
|
if err := p.poll(ctx, i+1); err != nil {
|
2022-09-03 02:59:44 -05:00
|
|
|
log.WithField("thread", i+1).
|
|
|
|
WithError(err).Error("poll error")
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
p.routineGroup.Wait()
|
2022-08-28 01:05:56 -05:00
|
|
|
return nil
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|
|
|
|
|
2022-09-01 02:09:22 -05:00
|
|
|
func (p *Poller) poll(ctx context.Context, thread int) error {
|
2022-09-29 09:19:21 -05:00
|
|
|
l := log.WithField("thread", thread)
|
|
|
|
l.Info("poller: request stage from remote server")
|
2022-08-13 21:34:19 -05:00
|
|
|
|
2022-09-03 02:59:44 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
|
|
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-09-29 09:19:21 -05:00
|
|
|
resp, err := p.Client.FetchTask(ctx, connect.NewRequest(&runnerv1.FetchTaskRequest{
|
2022-09-01 02:09:22 -05:00
|
|
|
Os: p.Filter.OS,
|
|
|
|
Arch: p.Filter.Arch,
|
2022-09-25 05:54:00 -05:00
|
|
|
}))
|
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-09-01 02:09:22 -05:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-29 09:19:21 -05:00
|
|
|
|
|
|
|
if err != nil && err == ErrDataLock {
|
|
|
|
l.WithError(err).Info("task accepted by another runner")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-09-01 02:09:22 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-01 02:09:22 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
return p.Dispatch(ctx, resp.Msg.Task)
|
2022-08-13 21:34:19 -05:00
|
|
|
}
|