mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-05 18:40:59 +00:00
feat: insert the daemon context in the poller context
The daemon context is needed when the context of a job or the poller is done. Otherwise it is no longer possible to send a conclusion report to Forgejo, short of creating a context.Background() which poses its own set of problems. - WithDaemonContext is used to store the daemon context - The poller uses the daemon context instead of context.Background
This commit is contained in:
parent
fccf857bce
commit
d114f3646d
5 changed files with 37 additions and 8 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"code.forgejo.org/forgejo/runner/v9/internal/pkg/common"
|
||||
"code.forgejo.org/forgejo/runner/v9/internal/pkg/config"
|
||||
"code.forgejo.org/forgejo/runner/v9/internal/pkg/ver"
|
||||
)
|
||||
|
@ -45,7 +46,7 @@ func Execute(ctx context.Context) {
|
|||
Use: "daemon",
|
||||
Short: "Run as a runner daemon",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: runDaemon(ctx, &configFile),
|
||||
RunE: runDaemon(common.WithDaemonContext(ctx, ctx), &configFile),
|
||||
}
|
||||
rootCmd.AddCommand(daemonCmd)
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
|
|||
}
|
||||
}
|
||||
|
||||
poller := poll.New(cfg, cli, runner)
|
||||
poller := poll.New(ctx, cfg, cli, runner)
|
||||
|
||||
go poller.Poll()
|
||||
|
||||
|
|
|
@ -42,14 +42,14 @@ type poller struct {
|
|||
done chan any
|
||||
}
|
||||
|
||||
func New(cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
||||
return (&poller{}).init(cfg, client, runner)
|
||||
func New(ctx context.Context, cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
||||
return (&poller{}).init(ctx, cfg, client, runner)
|
||||
}
|
||||
|
||||
func (p *poller) init(cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
||||
pollingCtx, shutdownPolling := context.WithCancel(context.Background())
|
||||
func (p *poller) init(ctx context.Context, cfg *config.Config, client client.Client, runner run.RunnerInterface) Poller {
|
||||
pollingCtx, shutdownPolling := context.WithCancel(ctx)
|
||||
|
||||
jobsCtx, shutdownJobs := context.WithCancel(context.Background())
|
||||
jobsCtx, shutdownJobs := context.WithCancel(ctx)
|
||||
|
||||
done := make(chan any)
|
||||
|
||||
|
|
|
@ -118,7 +118,7 @@ func setTrace(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPoller_New(t *testing.T) {
|
||||
p := New(&config.Config{}, &mockClient{}, &mockRunner{})
|
||||
p := New(t.Context(), &config.Config{}, &mockClient{}, &mockRunner{})
|
||||
assert.NotNil(t, p)
|
||||
}
|
||||
|
||||
|
@ -172,6 +172,7 @@ func TestPoller_Runner(t *testing.T) {
|
|||
}
|
||||
p := &mockPoller{}
|
||||
p.init(
|
||||
t.Context(),
|
||||
&config.Config{
|
||||
Runner: configRunner,
|
||||
},
|
||||
|
@ -239,6 +240,7 @@ func TestPoller_Fetch(t *testing.T) {
|
|||
}
|
||||
p := &mockPoller{}
|
||||
p.init(
|
||||
t.Context(),
|
||||
&config.Config{
|
||||
Runner: configRunner,
|
||||
},
|
||||
|
|
26
internal/pkg/common/daemon_context.go
Normal file
26
internal/pkg/common/daemon_context.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2025 The Forgejo Authors
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type daemonContextKey string
|
||||
|
||||
const daemonContextKeyVal = daemonContextKey("daemon")
|
||||
|
||||
func DaemonContext(ctx context.Context) context.Context {
|
||||
val := ctx.Value(daemonContextKeyVal)
|
||||
if val != nil {
|
||||
if daemon, ok := val.(context.Context); ok {
|
||||
return daemon
|
||||
}
|
||||
}
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
func WithDaemonContext(ctx, daemon context.Context) context.Context {
|
||||
return context.WithValue(ctx, daemonContextKeyVal, daemon)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue