Skip to content

Commit b744c9f

Browse files
committed
Fix comments
1 parent 40bcd9d commit b744c9f

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

agent/agent.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (a *agent) init(ctx context.Context) {
214214
sshSrv.Manifest = &a.manifest
215215
sshSrv.ServiceBanner = &a.serviceBanner
216216
a.sshServer = sshSrv
217-
a.scriptRunner = agentscripts.New(ctx, agentscripts.Options{
217+
a.scriptRunner = agentscripts.New(agentscripts.Options{
218218
LogDir: a.logDir,
219219
Logger: a.logger,
220220
SSHServer: sshSrv,
@@ -638,12 +638,12 @@ func (a *agent) run(ctx context.Context) error {
638638
}
639639
}
640640

641-
err = a.scriptRunner.Init(manifest.Scripts)
641+
err = a.scriptRunner.Init(ctx, manifest.Scripts)
642642
if err != nil {
643643
return xerrors.Errorf("init script runner: %w", err)
644644
}
645645
err = a.trackConnGoroutine(func() {
646-
err := a.scriptRunner.Execute(func(script codersdk.WorkspaceAgentScript) bool {
646+
err := a.scriptRunner.Execute(ctx, func(script codersdk.WorkspaceAgentScript) bool {
647647
return script.RunOnStart
648648
})
649649
if err != nil {
@@ -1244,7 +1244,7 @@ func (a *agent) Close() error {
12441244
}
12451245

12461246
lifecycleState := codersdk.WorkspaceAgentLifecycleOff
1247-
err = a.scriptRunner.Execute(func(script codersdk.WorkspaceAgentScript) bool {
1247+
err = a.scriptRunner.Execute(ctx, func(script codersdk.WorkspaceAgentScript) bool {
12481248
return script.RunOnStop
12491249
})
12501250
if err != nil {

agent/agentscripts/agentscripts.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ type Options struct {
3939
}
4040

4141
// New creates a runner for the provided scripts.
42-
func New(ctx context.Context, opts Options) *Runner {
42+
func New(opts Options) *Runner {
4343
return &Runner{
4444
Options: opts,
4545
cron: cron.New(cron.WithParser(parser)),
46-
ctx: ctx,
4746
closed: make(chan struct{}),
4847
}
4948
}
@@ -54,7 +53,6 @@ type Runner struct {
5453
cmdCloseWait sync.WaitGroup
5554
closed chan struct{}
5655
closeMutex sync.Mutex
57-
ctx context.Context
5856
cron *cron.Cron
5957
initialized atomic.Bool
6058
scripts []codersdk.WorkspaceAgentScript
@@ -63,23 +61,23 @@ type Runner struct {
6361
// Init initializes the runner with the provided scripts.
6462
// It also schedules any scripts that have a schedule.
6563
// 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 {
6765
if r.initialized.Load() {
6866
return xerrors.New("init: already initialized")
6967
}
7068
r.initialized.Store(true)
7169
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))
7371

7472
for _, script := range scripts {
7573
if script.Cron == "" {
7674
continue
7775
}
7876
script := script
7977
_, err := r.cron.AddFunc(script.Cron, func() {
80-
err := r.run(script)
78+
err := r.run(ctx, script)
8179
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))
8381
}
8482
})
8583
if err != nil {
@@ -96,7 +94,7 @@ func (r *Runner) StartCRON() {
9694
}
9795

9896
// 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 {
10098
if filter == nil {
10199
// Execute em' all!
102100
filter = func(script codersdk.WorkspaceAgentScript) bool {
@@ -110,7 +108,7 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
110108
}
111109
script := script
112110
eg.Go(func() error {
113-
err := r.run(script)
111+
err := r.run(ctx, script)
114112
if err != nil {
115113
return xerrors.Errorf("run agent script %q: %w", script.LogPath, err)
116114
}
@@ -124,9 +122,8 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
124122
// If the timeout is exceeded, the process is sent an interrupt signal.
125123
// If the process does not exit after a few seconds, it is forcefully killed.
126124
// 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 {
128126
logger := r.Logger.With(slog.F("log_source", script.LogPath))
129-
ctx := r.ctx
130127
logger.Info(ctx, "running agent script", slog.F("script", script.Script))
131128

132129
logPath := script.LogPath

agent/agentscripts/agentscripts_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func TestExecuteBasic(t *testing.T) {
3030
return nil
3131
})
3232
defer runner.Close()
33-
err := runner.Init([]codersdk.WorkspaceAgentScript{{
33+
err := runner.Init(context.Background(), []codersdk.WorkspaceAgentScript{{
3434
Script: "echo hello",
3535
}})
3636
require.NoError(t, err)
37-
require.NoError(t, runner.Execute(func(script codersdk.WorkspaceAgentScript) bool {
37+
require.NoError(t, runner.Execute(context.Background(), func(script codersdk.WorkspaceAgentScript) bool {
3838
return true
3939
}))
4040
log := <-logs
@@ -45,12 +45,12 @@ func TestTimeout(t *testing.T) {
4545
t.Parallel()
4646
runner := setup(t, nil)
4747
defer runner.Close()
48-
err := runner.Init([]codersdk.WorkspaceAgentScript{{
48+
err := runner.Init(context.Background(), []codersdk.WorkspaceAgentScript{{
4949
Script: "sleep 3",
5050
TimeoutSeconds: time.Nanosecond,
5151
}})
5252
require.NoError(t, err)
53-
require.ErrorIs(t, runner.Execute(nil), agentscripts.ErrTimeout)
53+
require.ErrorIs(t, runner.Execute(context.Background(), nil), agentscripts.ErrTimeout)
5454
}
5555

5656
func setup(t *testing.T, patchLogs func(ctx context.Context, req agentsdk.PatchLogs) error) *agentscripts.Runner {
@@ -62,16 +62,15 @@ func setup(t *testing.T, patchLogs func(ctx context.Context, req agentsdk.PatchL
6262
}
6363
}
6464
fs := afero.NewMemMapFs()
65-
ctx := context.Background()
6665
logger := slogtest.Make(t, nil)
67-
s, err := agentssh.NewServer(ctx, logger, prometheus.NewRegistry(), fs, 0, "")
66+
s, err := agentssh.NewServer(context.Background(), logger, prometheus.NewRegistry(), fs, 0, "")
6867
require.NoError(t, err)
6968
s.AgentToken = func() string { return "" }
7069
s.Manifest = atomic.NewPointer(&agentsdk.Manifest{})
7170
t.Cleanup(func() {
7271
_ = s.Close()
7372
})
74-
return agentscripts.New(ctx, agentscripts.Options{
73+
return agentscripts.New(agentscripts.Options{
7574
LogDir: t.TempDir(),
7675
Logger: logger,
7776
SSHServer: s,

provisioner/terraform/resources_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ func TestConvertResources(t *testing.T) {
132132
MotdFile: "/etc/motd",
133133
DisplayApps: &displayApps,
134134
Scripts: []*proto.Script{{
135-
DisplayName: "Shutdown Script",
136-
RunOnStop: true,
137-
LogPath: "coder-shutdown-script.log",
138-
Script: "echo bye bye",
139-
Timeout: 30,
135+
DisplayName: "Shutdown Script",
136+
RunOnStop: true,
137+
LogPath: "coder-shutdown-script.log",
138+
Script: "echo bye bye",
139+
TimeoutSeconds: 30,
140140
}},
141141
}, {
142142
Name: "dev3",

0 commit comments

Comments
 (0)