@@ -39,11 +39,10 @@ type Options struct {
39
39
}
40
40
41
41
// New creates a runner for the provided scripts.
42
- func New (ctx context. Context , opts Options ) * Runner {
42
+ func New (opts Options ) * Runner {
43
43
return & Runner {
44
44
Options : opts ,
45
45
cron : cron .New (cron .WithParser (parser )),
46
- ctx : ctx ,
47
46
closed : make (chan struct {}),
48
47
}
49
48
}
@@ -54,7 +53,6 @@ type Runner struct {
54
53
cmdCloseWait sync.WaitGroup
55
54
closed chan struct {}
56
55
closeMutex sync.Mutex
57
- ctx context.Context
58
56
cron * cron.Cron
59
57
initialized atomic.Bool
60
58
scripts []codersdk.WorkspaceAgentScript
@@ -63,23 +61,23 @@ type Runner struct {
63
61
// Init initializes the runner with the provided scripts.
64
62
// It also schedules any scripts that have a schedule.
65
63
// This function must be called before Execute.
66
- func (r * Runner ) Init (scripts []codersdk.WorkspaceAgentScript ) error {
64
+ func (r * Runner ) Init (ctx context. Context , scripts []codersdk.WorkspaceAgentScript ) error {
67
65
if r .initialized .Load () {
68
66
return xerrors .New ("init: already initialized" )
69
67
}
70
68
r .initialized .Store (true )
71
69
r .scripts = scripts
72
- r .Logger .Info (r . ctx , "initializing agent scripts" , slog .F ("script_count" , len (scripts )), slog .F ("log_dir" , r .LogDir ))
70
+ r .Logger .Info (ctx , "initializing agent scripts" , slog .F ("script_count" , len (scripts )), slog .F ("log_dir" , r .LogDir ))
73
71
74
72
for _ , script := range scripts {
75
73
if script .Cron == "" {
76
74
continue
77
75
}
78
76
script := script
79
77
_ , err := r .cron .AddFunc (script .Cron , func () {
80
- err := r .run (script )
78
+ err := r .run (ctx , script )
81
79
if err != nil {
82
- r .Logger .Warn (r . ctx , "run agent script on schedule" , slog .Error (err ))
80
+ r .Logger .Warn (context . Background () , "run agent script on schedule" , slog .Error (err ))
83
81
}
84
82
})
85
83
if err != nil {
@@ -96,7 +94,7 @@ func (r *Runner) StartCRON() {
96
94
}
97
95
98
96
// Execute runs a set of scripts according to a filter.
99
- func (r * Runner ) Execute (filter func (script codersdk.WorkspaceAgentScript ) bool ) error {
97
+ func (r * Runner ) Execute (ctx context. Context , filter func (script codersdk.WorkspaceAgentScript ) bool ) error {
100
98
if filter == nil {
101
99
// Execute em' all!
102
100
filter = func (script codersdk.WorkspaceAgentScript ) bool {
@@ -110,7 +108,7 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
110
108
}
111
109
script := script
112
110
eg .Go (func () error {
113
- err := r .run (script )
111
+ err := r .run (ctx , script )
114
112
if err != nil {
115
113
return xerrors .Errorf ("run agent script %q: %w" , script .LogPath , err )
116
114
}
@@ -124,9 +122,8 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
124
122
// If the timeout is exceeded, the process is sent an interrupt signal.
125
123
// If the process does not exit after a few seconds, it is forcefully killed.
126
124
// This function immediately returns after a timeout, and does not wait for the process to exit.
127
- func (r * Runner ) run (script codersdk.WorkspaceAgentScript ) error {
125
+ func (r * Runner ) run (ctx context. Context , script codersdk.WorkspaceAgentScript ) error {
128
126
logger := r .Logger .With (slog .F ("log_source" , script .LogPath ))
129
- ctx := r .ctx
130
127
logger .Info (ctx , "running agent script" , slog .F ("script" , script .Script ))
131
128
132
129
logPath := script .LogPath
0 commit comments