chore(runtime): fetch build data

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu
2022-09-03 20:57:32 +08:00
committed by Jason Song
parent 631f801aa6
commit d3d56ed0ef
9 changed files with 77 additions and 78 deletions

View File

@ -2,11 +2,12 @@ package runtime
import (
"context"
"fmt"
"gitea.com/gitea/act_runner/client"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
// Defines the Resource Kind and Type.
@ -24,12 +25,45 @@ type Runner struct {
// Run runs the pipeline stage.
func (s *Runner) Run(ctx context.Context, stage *runnerv1.Stage) error {
l := logrus.
WithField("runner.ID", stage.Id).
WithField("runner.BuildID", stage.BuildId)
l := log.
WithField("stage.id", stage.Id).
WithField("stage.name", stage.Name)
l.Info("start running pipeline")
// TODO: use ctx to transfer usage information
return startTask(stage.BuildId, ctx)
// update machine in stage
stage.Machine = s.Machine
data, err := s.Client.Detail(ctx, &runnerv1.DetailRequest{
Stage: stage,
})
if err != nil {
l.Debug("stage accepted by another runner")
return nil
}
l = log.WithField("repo.id", data.Repo.Id).
WithField("repo.name", data.Repo.Name).
WithField("build.id", data.Build.Id).
WithField("build.name", data.Build.Name)
l.Info("stage details fetched")
return s.run(ctx, data)
}
func (s *Runner) run(ctx context.Context, data *runnerv1.DetailResponse) error {
_, exist := globalTaskMap.Load(data.Build.Id)
if exist {
return fmt.Errorf("task %d already exists", data.Build.Id)
}
task := NewTask(data.Build.Id, s.Client)
// set task ve to global map
// when task is done or canceled, it will be removed from the map
globalTaskMap.Store(data.Build.Id, task)
go task.Run(ctx)
return nil
}

View File

@ -9,14 +9,13 @@ import (
"time"
"gitea.com/gitea/act_runner/client"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
"github.com/nektos/act/pkg/artifacts"
"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/model"
"github.com/nektos/act/pkg/runner"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
)
type TaskInput struct {
@ -116,11 +115,11 @@ type Task struct {
logHook *taskLogHook
state TaskState
client client.Client
log *logrus.Entry
log *log.Entry
}
// newTask creates a new task
func NewTask(buildID int64) *Task {
func NewTask(buildID int64, client client.Client) *Task {
task := &Task{
Input: &TaskInput{
reuseContainers: true,
@ -129,8 +128,8 @@ func NewTask(buildID int64) *Task {
BuildID: buildID,
state: TaskStatePending,
client: nil,
log: logrus.WithField("buildID", buildID),
client: client,
log: log.WithField("buildID", buildID),
logHook: &taskLogHook{},
}
task.Input.repoDirectory, _ = os.Getwd()
@ -168,13 +167,12 @@ func (t *Task) reportFailure(ctx context.Context, err error) {
if t.client == nil {
// TODO: fill the step request
stepRequest := &runnerv1.UpdateStepRequest{}
t.client.UpdateStep(ctx, stepRequest)
_ = t.client.UpdateStep(ctx, stepRequest)
return
}
}
func (t *Task) startReporting(interval int64, ctx context.Context) {
func (t *Task) startReporting(ctx context.Context, interval int64) {
for {
time.Sleep(time.Duration(interval) * time.Second)
if t.state == TaskStateSuccess || t.state == TaskStateFailure {
@ -199,7 +197,7 @@ func (t *Task) reportStep(ctx context.Context) {
// TODO: fill the step request
stepRequest := &runnerv1.UpdateStepRequest{}
t.client.UpdateStep(ctx, stepRequest)
_ = t.client.UpdateStep(ctx, stepRequest)
}
// reportSuccess reports the success of the task
@ -215,18 +213,10 @@ func (t *Task) reportSuccess(ctx context.Context) {
// TODO: fill the step request
stepRequest := &runnerv1.UpdateStepRequest{}
t.client.UpdateStep(ctx, stepRequest)
_ = t.client.UpdateStep(ctx, stepRequest)
}
func (t *Task) Run(ctx context.Context) {
// get client for context, use for reporting
t.client = client.FromContext(ctx)
if t.client == nil {
t.log.Warnf("no client found in context")
} else {
t.log.Infof("client found in context")
}
workflowsPath, err := getWorkflowsPath(t.Input.repoDirectory)
if err != nil {
t.reportFailure(ctx, err)
@ -324,7 +314,7 @@ func (t *Task) Run(ctx context.Context) {
// add logger recorders
ctx = common.WithLoggerHook(ctx, t.logHook)
go t.startReporting(1, ctx)
go t.startReporting(ctx, 1)
if err := executor(ctx); err != nil {
t.reportFailure(ctx, err)

View File

@ -1,31 +1,11 @@
package runtime
import (
"context"
"fmt"
"sync"
)
var globalTaskMap sync.Map
// startTask adds the task to global map
func startTask(buildID int64, ctx context.Context) error {
_, exist := globalTaskMap.Load(buildID)
if exist {
return fmt.Errorf("task %d already exists", buildID)
}
task := NewTask(buildID)
// set task ve to global map
// when task is done or canceled, it will be removed from the map
globalTaskMap.Store(buildID, task)
go task.Run(ctx)
return nil
}
// finishTask removes the task from global map
func finishTask(buildID int64) {
globalTaskMap.Delete(buildID)