Execute runs a set of scripts according to a filter.
(ctx context.Context, option ExecuteOption)
| 199 | |
| 200 | // Execute runs a set of scripts according to a filter. |
| 201 | func (r *Runner) Execute(ctx context.Context, option ExecuteOption) error { |
| 202 | initErr := func() error { |
| 203 | r.initMutex.Lock() |
| 204 | defer r.initMutex.Unlock() |
| 205 | if !r.initialized { |
| 206 | return xerrors.New("execute: not initialized") |
| 207 | } |
| 208 | return nil |
| 209 | }() |
| 210 | if initErr != nil { |
| 211 | return initErr |
| 212 | } |
| 213 | |
| 214 | var eg errgroup.Group |
| 215 | for _, script := range r.scripts { |
| 216 | runScript := (option == ExecuteStartScripts && script.RunOnStart) || |
| 217 | (option == ExecuteStopScripts && script.RunOnStop) || |
| 218 | (option == ExecuteCronScripts && script.Cron != "") || |
| 219 | option == ExecuteAllScripts |
| 220 | |
| 221 | if !runScript { |
| 222 | continue |
| 223 | } |
| 224 | |
| 225 | eg.Go(func() error { |
| 226 | err := r.trackRun(ctx, script, option) |
| 227 | if err != nil { |
| 228 | return xerrors.Errorf("run agent script %q: %w", script.LogSourceID, err) |
| 229 | } |
| 230 | return nil |
| 231 | }) |
| 232 | } |
| 233 | return eg.Wait() |
| 234 | } |
| 235 | |
| 236 | // trackRun wraps "run" with metrics. |
| 237 | func (r *Runner) trackRun(ctx context.Context, script codersdk.WorkspaceAgentScript, option ExecuteOption) error { |