1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-01 17:38:36 +00:00
forgejo-runner/internal/app/job/job.go

95 lines
2.2 KiB
Go
Raw Permalink Normal View History

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package job
import (
"context"
"errors"
"fmt"
"sync/atomic"
"connectrpc.com/connect"
log "github.com/sirupsen/logrus"
chore: upgrade to code.forgejo.org/forgejo/actions-proto (#655) In replacement of code.gitea.io/actions-proto-go - https://gitea.com/gitea/actions-proto-def and https://gitea.com/gitea/actions-proto-go were merged into https://code.forgejo.org/forgejo/actions-proto to facilitate maintenance - the generated go code is different because the package name is different - https://code.forgejo.org/forgejo/actions-proto/commit/f4285dfc2855e3ef26f49d74a5c596e015d40607 shows they compare exactly identical before the name change - https://code.forgejo.org/forgejo/actions-proto/commit/a3c95cb82fbcb972432d04a51423710c43ed27ec is the generated code right after the name change - the cascading pull request further shows the protocol is compatible by running [end-to-end actions tests](https://code.forgejo.org/forgejo/end-to-end/src/branch/main/actions) that rely on it, using a runner binary built from [this pull request](https://code.forgejo.org/forgejo/end-to-end/actions/runs/3329/jobs/2#jobstep-4-640) `0296d988d65e66b8d8a7951d0d7d7f8c6cf78b44` matches `v0.0.1+576-g0296d98` - `time="2025-07-03T12:53:50Z" level=info msg="runner: runner, with version: v0.0.1+576-g0296d98, with labels: [docker], declared successfully" func="[func6]" file="[daemon.go:108]"` A similar pull request will be sent to Forgejo once this one is merged (less risky environment) Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/655 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-07-03 16:55:53 +00:00
runnerv1 "code.forgejo.org/forgejo/actions-proto/runner/v1"
"code.forgejo.org/forgejo/runner/v9/internal/app/run"
"code.forgejo.org/forgejo/runner/v9/internal/pkg/client"
"code.forgejo.org/forgejo/runner/v9/internal/pkg/config"
)
type Job struct {
client client.Client
runner run.RunnerInterface
cfg *config.Config
tasksVersion atomic.Int64
}
func NewJob(cfg *config.Config, client client.Client, runner run.RunnerInterface) *Job {
j := &Job{}
j.client = client
j.runner = runner
j.cfg = cfg
return j
}
func (j *Job) Run(ctx context.Context) error {
task, ok := j.fetchTask(ctx)
if !ok {
return fmt.Errorf("could not fetch task")
}
return j.runTaskWithRecover(ctx, task)
}
func (j *Job) runTaskWithRecover(ctx context.Context, task *runnerv1.Task) error {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("panic: %v", r)
log.WithError(err).Error("panic in runTaskWithRecover")
}
}()
if err := j.runner.Run(ctx, task); err != nil {
log.WithError(err).Error("failed to run task")
return err
}
return nil
}
func (j *Job) fetchTask(ctx context.Context) (*runnerv1.Task, bool) {
reqCtx, cancel := context.WithTimeout(ctx, j.cfg.Runner.FetchTimeout)
defer cancel()
// Load the version value that was in the cache when the request was sent.
v := j.tasksVersion.Load()
resp, err := j.client.FetchTask(reqCtx, connect.NewRequest(&runnerv1.FetchTaskRequest{
TasksVersion: v,
}))
if err != nil {
if errors.Is(err, context.Canceled) {
log.WithError(err).Debugf("shutdown, fetch task canceled")
} else {
log.WithError(err).Error("failed to fetch task")
}
return nil, false
}
if resp == nil || resp.Msg == nil {
return nil, false
}
if resp.Msg.GetTasksVersion() > v {
j.tasksVersion.CompareAndSwap(v, resp.Msg.GetTasksVersion())
}
if resp.Msg.Task == nil {
return nil, false
}
j.tasksVersion.CompareAndSwap(resp.Msg.GetTasksVersion(), 0)
return resp.Msg.GetTask(), true
}