Skip to content

Commit 40bcd9d

Browse files
committed
Fix comments
1 parent 45b395e commit 40bcd9d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

agent/agentscripts/agentscripts.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"sync"
11+
"sync/atomic"
1112
"time"
1213

1314
"github.com/robfig/cron/v3"
@@ -55,14 +56,20 @@ type Runner struct {
5556
closeMutex sync.Mutex
5657
ctx context.Context
5758
cron *cron.Cron
59+
initialized atomic.Bool
5860
scripts []codersdk.WorkspaceAgentScript
5961
}
6062

6163
// Init initializes the runner with the provided scripts.
6264
// It also schedules any scripts that have a schedule.
6365
// This function must be called before Execute.
6466
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)
6571
r.scripts = scripts
72+
r.Logger.Info(r.ctx, "initializing agent scripts", slog.F("script_count", len(scripts)), slog.F("log_dir", r.LogDir))
6673

6774
for _, script := range scripts {
6875
if script.Cron == "" {
@@ -165,9 +172,9 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
165172
}
166173
}()
167174

168-
infoW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelInfo)
175+
infoW := agentsdk.LogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelInfo)
169176
defer infoW.Close()
170-
errW := agentsdk.StartupLogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelError)
177+
errW := agentsdk.LogsWriter(ctx, send, script.LogSourceID, codersdk.LogLevelError)
171178
defer errW.Close()
172179
cmd.Stdout = io.MultiWriter(fileWriter, infoW)
173180
cmd.Stderr = io.MultiWriter(fileWriter, errW)

codersdk/agentsdk/logs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ func (w *startupLogsWriter) Close() error {
7373
return nil
7474
}
7575

76-
// StartupLogsWriter returns an io.WriteCloser that sends logs via the
76+
// LogsWriter returns an io.WriteCloser that sends logs via the
7777
// provided sender. The sender is expected to be non-blocking. Calling
7878
// Close flushes any remaining partially written log lines but is
79-
// otherwise no-op. If the context passed to StartupLogsWriter is
79+
// otherwise no-op. If the context passed to LogsWriter is
8080
// canceled, any remaining logs will be discarded.
8181
//
8282
// Neither Write nor Close is safe for concurrent use and must be used
8383
// by a single goroutine.
84-
func StartupLogsWriter(ctx context.Context, sender func(ctx context.Context, log ...Log) error, source uuid.UUID, level codersdk.LogLevel) io.WriteCloser {
84+
func LogsWriter(ctx context.Context, sender func(ctx context.Context, log ...Log) error, source uuid.UUID, level codersdk.LogLevel) io.WriteCloser {
8585
return &startupLogsWriter{
8686
ctx: ctx,
8787
send: sender,

codersdk/agentsdk/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestStartupLogsWriter_Write(t *testing.T) {
184184
got = append(got, log...)
185185
return nil
186186
}
187-
w := agentsdk.StartupLogsWriter(tt.ctx, send, uuid.New(), tt.level)
187+
w := agentsdk.LogsWriter(tt.ctx, send, uuid.New(), tt.level)
188188
for _, s := range tt.writes {
189189
_, err := w.Write([]byte(s))
190190
if err != nil {

0 commit comments

Comments
 (0)