@@ -141,7 +141,7 @@ func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript, scriptCompleted S
141
141
}
142
142
script := script
143
143
_ , err := r .cron .AddFunc (script .Cron , func () {
144
- err := r .trackRun (r .cronCtx , script )
144
+ err := r .trackRun (r .cronCtx , script , ExecuteCronScripts )
145
145
if err != nil {
146
146
r .Logger .Warn (context .Background (), "run agent script on schedule" , slog .Error (err ))
147
147
}
@@ -178,22 +178,31 @@ func (r *Runner) StartCron() {
178
178
}
179
179
}
180
180
181
+ type ExecuteOption int
182
+
183
+ const (
184
+ ExecuteAllScripts ExecuteOption = iota
185
+ ExecuteStartScripts
186
+ ExecuteStopScripts
187
+ ExecuteCronScripts
188
+ )
189
+
181
190
// Execute runs a set of scripts according to a filter.
182
- func (r * Runner ) Execute (ctx context.Context , filter func (script codersdk.WorkspaceAgentScript ) bool ) error {
183
- if filter == nil {
184
- // Execute em' all!
185
- filter = func (script codersdk.WorkspaceAgentScript ) bool {
186
- return true
187
- }
188
- }
191
+ func (r * Runner ) Execute (ctx context.Context , option ExecuteOption ) error {
189
192
var eg errgroup.Group
190
193
for _ , script := range r .scripts {
191
- if ! filter (script ) {
194
+ runScript := (option == ExecuteStartScripts && script .RunOnStart ) ||
195
+ (option == ExecuteStopScripts && script .RunOnStop ) ||
196
+ (option == ExecuteCronScripts && script .Cron != "" ) ||
197
+ option == ExecuteAllScripts
198
+
199
+ if ! runScript {
192
200
continue
193
201
}
202
+
194
203
script := script
195
204
eg .Go (func () error {
196
- err := r .trackRun (ctx , script )
205
+ err := r .trackRun (ctx , script , option )
197
206
if err != nil {
198
207
return xerrors .Errorf ("run agent script %q: %w" , script .LogSourceID , err )
199
208
}
@@ -204,8 +213,8 @@ func (r *Runner) Execute(ctx context.Context, filter func(script codersdk.Worksp
204
213
}
205
214
206
215
// trackRun wraps "run" with metrics.
207
- func (r * Runner ) trackRun (ctx context.Context , script codersdk.WorkspaceAgentScript ) error {
208
- err := r .run (ctx , script )
216
+ func (r * Runner ) trackRun (ctx context.Context , script codersdk.WorkspaceAgentScript , option ExecuteOption ) error {
217
+ err := r .run (ctx , script , option )
209
218
if err != nil {
210
219
r .scriptsExecuted .WithLabelValues ("false" ).Add (1 )
211
220
} else {
@@ -218,7 +227,7 @@ func (r *Runner) trackRun(ctx context.Context, script codersdk.WorkspaceAgentScr
218
227
// If the timeout is exceeded, the process is sent an interrupt signal.
219
228
// If the process does not exit after a few seconds, it is forcefully killed.
220
229
// This function immediately returns after a timeout, and does not wait for the process to exit.
221
- func (r * Runner ) run (ctx context.Context , script codersdk.WorkspaceAgentScript ) error {
230
+ func (r * Runner ) run (ctx context.Context , script codersdk.WorkspaceAgentScript , option ExecuteOption ) error {
222
231
logPath := script .LogPath
223
232
if logPath == "" {
224
233
logPath = fmt .Sprintf ("coder-script-%s.log" , script .LogSourceID )
@@ -321,15 +330,25 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
321
330
logger .Info (ctx , fmt .Sprintf ("%s script completed" , logPath ), slog .F ("execution_time" , execTime ), slog .F ("exit_code" , exitCode ))
322
331
}
323
332
333
+ var stage proto.Timing_Stage
334
+ switch option {
335
+ case ExecuteStartScripts :
336
+ stage = proto .Timing_START
337
+ case ExecuteStopScripts :
338
+ stage = proto .Timing_STOP
339
+ case ExecuteCronScripts :
340
+ stage = proto .Timing_CRON
341
+ }
342
+
324
343
_ , err = r .scriptCompleted (ctx , & proto.WorkspaceAgentScriptCompletedRequest {
325
344
Timing : & proto.Timing {
326
- ScriptId : script .ID [:],
327
- DisplayName : script .DisplayName ,
328
- Start : timestamppb .New (start ),
329
- End : timestamppb .New (end ),
330
- ExitCode : int32 (exitCode ),
331
- RanOnStart : script . RunOnStart ,
332
- BlockedLogin : script . StartBlocksLogin ,
345
+ ScriptId : script .ID [:],
346
+ DisplayName : script .DisplayName ,
347
+ Start : timestamppb .New (start ),
348
+ End : timestamppb .New (end ),
349
+ ExitCode : int32 (exitCode ),
350
+ Stage : stage ,
351
+ TimedOut : errors . Is ( err , ErrTimeout ) ,
333
352
},
334
353
})
335
354
0 commit comments