Merge pull request 'Add report_interval option to config' (#220) from l_austenfeld/runner:report_interval into main

Reviewed-on: https://code.forgejo.org///forgejo/runner/pulls/220
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
pull/224/head
earl-warren 2024-07-27 16:57:52 +00:00
commit a90b14b0e4
6 changed files with 26 additions and 13 deletions

View File

@ -1,5 +1,9 @@
# Release Notes # Release Notes
## 3.5.1
* [Add report_interval option to config](https://code.forgejo.org/forgejo/runner/pulls/220) to allow setting the interval of status and log reports
## 3.5.0 ## 3.5.0
* [Allow graceful shutdowns](https://code.forgejo.org/forgejo/runner/pulls/202): when receiving a signal (INT or TERM) wait for running jobs to complete (up to shutdown_timeout). * [Allow graceful shutdowns](https://code.forgejo.org/forgejo/runner/pulls/202): when receiving a signal (INT or TERM) wait for running jobs to complete (up to shutdown_timeout).

View File

@ -109,7 +109,7 @@ func (r *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
ctx, cancel := context.WithTimeout(ctx, r.cfg.Runner.Timeout) ctx, cancel := context.WithTimeout(ctx, r.cfg.Runner.Timeout)
defer cancel() defer cancel()
reporter := report.NewReporter(ctx, cancel, r.client, task) reporter := report.NewReporter(ctx, cancel, r.client, task, r.cfg.Runner.ReportInterval)
var runErr error var runErr error
defer func() { defer func() {
lastWords := "" lastWords := ""

View File

@ -35,6 +35,8 @@ runner:
fetch_timeout: 5s fetch_timeout: 5s
# The interval for fetching the job from the Forgejo instance. # The interval for fetching the job from the Forgejo instance.
fetch_interval: 2s fetch_interval: 2s
# The interval for reporting the job status and logs to the Forgejo instance.
report_interval: 1s
# The labels of a runner are used to determine which jobs the runner can run, and how to run them. # The labels of a runner are used to determine which jobs the runner can run, and how to run them.
# Like: ["macos-arm64:host", "ubuntu-latest:docker://node:20-bookworm", "ubuntu-22.04:docker://node:20-bookworm"] # Like: ["macos-arm64:host", "ubuntu-latest:docker://node:20-bookworm", "ubuntu-22.04:docker://node:20-bookworm"]
# If it's empty when registering, it will ask for inputting labels. # If it's empty when registering, it will ask for inputting labels.

View File

@ -30,6 +30,7 @@ type Runner struct {
Insecure bool `yaml:"insecure"` // Insecure indicates whether the runner operates in an insecure mode. Insecure bool `yaml:"insecure"` // Insecure indicates whether the runner operates in an insecure mode.
FetchTimeout time.Duration `yaml:"fetch_timeout"` // FetchTimeout specifies the timeout duration for fetching resources. FetchTimeout time.Duration `yaml:"fetch_timeout"` // FetchTimeout specifies the timeout duration for fetching resources.
FetchInterval time.Duration `yaml:"fetch_interval"` // FetchInterval specifies the interval duration for fetching resources. FetchInterval time.Duration `yaml:"fetch_interval"` // FetchInterval specifies the interval duration for fetching resources.
ReportInterval time.Duration `yaml:"report_interval"` // ReportInterval specifies the interval duration for reporting status and logs of a running job.
Labels []string `yaml:"labels"` // Labels specify the labels of the runner. Labels are declared on each startup Labels []string `yaml:"labels"` // Labels specify the labels of the runner. Labels are declared on each startup
} }
@ -144,6 +145,9 @@ func LoadDefault(file string) (*Config, error) {
if cfg.Runner.FetchInterval <= 0 { if cfg.Runner.FetchInterval <= 0 {
cfg.Runner.FetchInterval = 2 * time.Second cfg.Runner.FetchInterval = 2 * time.Second
} }
if cfg.Runner.ReportInterval <= 0 {
cfg.Runner.ReportInterval = time.Second
}
// although `container.network_mode` will be deprecated, but we have to be compatible with it for now. // although `container.network_mode` will be deprecated, but we have to be compatible with it for now.
if cfg.Container.NetworkMode != "" && cfg.Container.Network == "" { if cfg.Container.NetworkMode != "" && cfg.Container.Network == "" {

View File

@ -29,10 +29,11 @@ type Reporter struct {
client client.Client client client.Client
clientM sync.Mutex clientM sync.Mutex
logOffset int logOffset int
logRows []*runnerv1.LogRow logRows []*runnerv1.LogRow
logReplacer *strings.Replacer logReplacer *strings.Replacer
oldnew []string oldnew []string
reportInterval time.Duration
state *runnerv1.TaskState state *runnerv1.TaskState
stateMu sync.RWMutex stateMu sync.RWMutex
@ -42,7 +43,7 @@ type Reporter struct {
stopCommandEndToken string stopCommandEndToken string
} }
func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.Client, task *runnerv1.Task) *Reporter { func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.Client, task *runnerv1.Task, reportInterval time.Duration) *Reporter {
var oldnew []string var oldnew []string
if v := task.Context.Fields["token"].GetStringValue(); v != "" { if v := task.Context.Fields["token"].GetStringValue(); v != "" {
oldnew = append(oldnew, v, "***") oldnew = append(oldnew, v, "***")
@ -55,11 +56,12 @@ func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.C
} }
rv := &Reporter{ rv := &Reporter{
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
client: client, client: client,
oldnew: oldnew, oldnew: oldnew,
logReplacer: strings.NewReplacer(oldnew...), reportInterval: reportInterval,
logReplacer: strings.NewReplacer(oldnew...),
state: &runnerv1.TaskState{ state: &runnerv1.TaskState{
Id: task.Id, Id: task.Id,
}, },
@ -180,7 +182,7 @@ func (r *Reporter) RunDaemon() {
_ = r.ReportLog(false) _ = r.ReportLog(false)
_ = r.ReportState() _ = r.ReportState()
time.AfterFunc(time.Second, r.RunDaemon) time.AfterFunc(r.reportInterval, r.RunDaemon)
} }
func (r *Reporter) Logf(format string, a ...interface{}) { func (r *Reporter) Logf(format string, a ...interface{}) {

View File

@ -7,6 +7,7 @@ import (
"context" "context"
"strings" "strings"
"testing" "testing"
"time"
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
connect_go "github.com/bufbuild/connect-go" connect_go "github.com/bufbuild/connect-go"
@ -173,7 +174,7 @@ func TestReporter_Fire(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
reporter := NewReporter(ctx, cancel, client, &runnerv1.Task{ reporter := NewReporter(ctx, cancel, client, &runnerv1.Task{
Context: taskCtx, Context: taskCtx,
}) }, time.Second)
defer func() { defer func() {
assert.NoError(t, reporter.Close("")) assert.NoError(t, reporter.Close(""))
}() }()