2022-08-14 13:29:00 +08:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-04 15:19:07 +08:00
|
|
|
"errors"
|
2022-08-14 13:29:00 +08:00
|
|
|
|
|
|
|
"gitea.com/gitea/act_runner/client"
|
2022-08-17 14:26:58 +08:00
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
2022-08-14 13:29:00 +08:00
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
"github.com/bufbuild/connect-go"
|
2022-09-03 20:57:32 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-14 13:29:00 +08:00
|
|
|
)
|
|
|
|
|
2022-09-04 15:19:07 +08:00
|
|
|
var ErrDataLock = errors.New("Data Lock Error")
|
|
|
|
|
2022-08-28 14:05:56 +08:00
|
|
|
// Defines the Resource Kind and Type.
|
|
|
|
const (
|
|
|
|
Kind = "pipeline"
|
|
|
|
Type = "docker"
|
|
|
|
)
|
|
|
|
|
2022-08-14 13:29:00 +08:00
|
|
|
// Runner runs the pipeline.
|
|
|
|
type Runner struct {
|
|
|
|
Machine string
|
|
|
|
Environ map[string]string
|
|
|
|
Client client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the pipeline stage.
|
2022-09-25 18:54:00 +08:00
|
|
|
func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
|
2022-09-03 20:57:32 +08:00
|
|
|
l := log.
|
2022-09-25 18:54:00 +08:00
|
|
|
WithField("task.id", task.Id)
|
2022-08-14 13:29:00 +08:00
|
|
|
|
|
|
|
l.Info("start running pipeline")
|
2022-09-01 15:09:22 +08:00
|
|
|
|
2022-09-03 20:57:32 +08:00
|
|
|
// update machine in stage
|
2022-09-25 18:54:00 +08:00
|
|
|
task.Machine = s.Machine
|
|
|
|
data, err := s.Client.Detail(ctx, connect.NewRequest(&runnerv1.DetailRequest{
|
|
|
|
Task: task,
|
|
|
|
}))
|
2022-09-04 15:19:07 +08:00
|
|
|
if err != nil && err == ErrDataLock {
|
2022-09-25 18:54:00 +08:00
|
|
|
l.Info("task accepted by another runner")
|
2022-09-03 20:57:32 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-04 15:19:07 +08:00
|
|
|
if err != nil {
|
2022-09-25 18:54:00 +08:00
|
|
|
l.WithError(err).Error("cannot accept task")
|
2022-09-04 15:19:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-09-03 20:57:32 +08:00
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
l.Info("task details fetched")
|
2022-09-03 20:57:32 +08:00
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
return s.run(ctx, data.Msg.Task)
|
2022-09-03 20:57:32 +08:00
|
|
|
}
|
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
func (s *Runner) run(ctx context.Context, task *runnerv1.Task) error {
|
|
|
|
return NewTask(task.Id, s.Client).Run(ctx, task)
|
2022-08-14 13:29:00 +08:00
|
|
|
}
|