From 705f59f3e4ab591f74cfbddf94b6f39e6a548a3f Mon Sep 17 00:00:00 2001 From: Lennart Austenfeld Date: Sat, 27 Jul 2024 16:51:45 +0200 Subject: [PATCH] Add report_interval option to config --- RELEASE-NOTES.md | 4 ++++ internal/app/run/runner.go | 2 +- internal/pkg/config/config.example.yaml | 2 ++ internal/pkg/config/config.go | 4 ++++ internal/pkg/report/reporter.go | 24 +++++++++++++----------- internal/pkg/report/reporter_test.go | 3 ++- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 76d9e18..194a855 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,5 +1,9 @@ # 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 * [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). diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index b17705d..e7b9b0b 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -109,7 +109,7 @@ func (r *Runner) Run(ctx context.Context, task *runnerv1.Task) error { ctx, cancel := context.WithTimeout(ctx, r.cfg.Runner.Timeout) 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 defer func() { lastWords := "" diff --git a/internal/pkg/config/config.example.yaml b/internal/pkg/config/config.example.yaml index fa40f5c..32dfb68 100644 --- a/internal/pkg/config/config.example.yaml +++ b/internal/pkg/config/config.example.yaml @@ -35,6 +35,8 @@ runner: fetch_timeout: 5s # The interval for fetching the job from the Forgejo instance. 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. # 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. diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 5c260fb..a1536b3 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -30,6 +30,7 @@ type Runner struct { 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. 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 } @@ -144,6 +145,9 @@ func LoadDefault(file string) (*Config, error) { if cfg.Runner.FetchInterval <= 0 { 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. if cfg.Container.NetworkMode != "" && cfg.Container.Network == "" { diff --git a/internal/pkg/report/reporter.go b/internal/pkg/report/reporter.go index a751858..25f16bf 100644 --- a/internal/pkg/report/reporter.go +++ b/internal/pkg/report/reporter.go @@ -29,10 +29,11 @@ type Reporter struct { client client.Client clientM sync.Mutex - logOffset int - logRows []*runnerv1.LogRow - logReplacer *strings.Replacer - oldnew []string + logOffset int + logRows []*runnerv1.LogRow + logReplacer *strings.Replacer + oldnew []string + reportInterval time.Duration state *runnerv1.TaskState stateMu sync.RWMutex @@ -42,7 +43,7 @@ type Reporter struct { 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 if v := task.Context.Fields["token"].GetStringValue(); v != "" { oldnew = append(oldnew, v, "***") @@ -55,11 +56,12 @@ func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.C } rv := &Reporter{ - ctx: ctx, - cancel: cancel, - client: client, - oldnew: oldnew, - logReplacer: strings.NewReplacer(oldnew...), + ctx: ctx, + cancel: cancel, + client: client, + oldnew: oldnew, + reportInterval: reportInterval, + logReplacer: strings.NewReplacer(oldnew...), state: &runnerv1.TaskState{ Id: task.Id, }, @@ -180,7 +182,7 @@ func (r *Reporter) RunDaemon() { _ = r.ReportLog(false) _ = r.ReportState() - time.AfterFunc(time.Second, r.RunDaemon) + time.AfterFunc(r.reportInterval, r.RunDaemon) } func (r *Reporter) Logf(format string, a ...interface{}) { diff --git a/internal/pkg/report/reporter_test.go b/internal/pkg/report/reporter_test.go index cdb2f21..a916c1d 100644 --- a/internal/pkg/report/reporter_test.go +++ b/internal/pkg/report/reporter_test.go @@ -7,6 +7,7 @@ import ( "context" "strings" "testing" + "time" runnerv1 "code.gitea.io/actions-proto-go/runner/v1" connect_go "github.com/bufbuild/connect-go" @@ -173,7 +174,7 @@ func TestReporter_Fire(t *testing.T) { require.NoError(t, err) reporter := NewReporter(ctx, cancel, client, &runnerv1.Task{ Context: taskCtx, - }) + }, time.Second) defer func() { assert.NoError(t, reporter.Close("")) }()