|
8 | 8 | "os/exec"
|
9 | 9 | "path/filepath"
|
10 | 10 | "sync"
|
| 11 | + "sync/atomic" |
11 | 12 | "time"
|
12 | 13 |
|
13 | 14 | "github.com/robfig/cron/v3"
|
@@ -55,14 +56,20 @@ type Runner struct {
|
55 | 56 | closeMutex sync.Mutex
|
56 | 57 | ctx context.Context
|
57 | 58 | cron *cron.Cron
|
| 59 | + initialized atomic.Bool |
58 | 60 | scripts []codersdk.WorkspaceAgentScript
|
59 | 61 | }
|
60 | 62 |
|
61 | 63 | // Init initializes the runner with the provided scripts.
|
62 | 64 | // It also schedules any scripts that have a schedule.
|
63 | 65 | // This function must be called before Execute.
|
64 | 66 | func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error {
|
| 67 | + if r.initialized.Load() { |
| 68 | + return xerrors.New("init: already initialized") |
| 69 | + } |
| 70 | + r.initialized.Store(true) |
65 | 71 | r.scripts = scripts
|
| 72 | + r.Logger.Info(r.ctx, "initializing agent scripts", slog.F("script_count", len(scripts)), slog.F("log_dir", r.LogDir)) |
66 | 73 |
|
67 | 74 | for _, script := range scripts {
|
68 | 75 | if script.Cron == "" {
|
@@ -165,9 +172,9 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
|
165 | 172 | }
|
166 | 173 | }()
|
167 | 174 |
|
168 |
| - infoW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelInfo) |
| 175 | + infoW := agentsdk.LogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelInfo) |
169 | 176 | defer infoW.Close()
|
170 |
| - errW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelError) |
| 177 | + errW := agentsdk.LogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelError) |
171 | 178 | defer errW.Close()
|
172 | 179 | cmd.Stdout = io.MultiWriter(fileWriter, infoW)
|
173 | 180 | cmd.Stderr = io.MultiWriter(fileWriter, errW)
|
|
0 commit comments