Skip to content

Commit 6d829f3

Browse files
fix: get tests passing again
1 parent 09a1a42 commit 6d829f3

File tree

4 files changed

+32
-55
lines changed

4 files changed

+32
-55
lines changed

agent/agent.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,6 @@ func (a *agent) reportMetadata(ctx context.Context, conn drpc.Conn) error {
582582
select {
583583
case <-ctx.Done():
584584
return ctx.Err()
585-
case timing := <-*a.scriptRunner.ScriptTimings():
586-
_, err := aAPI.ScriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
587-
Timing: timing.ToProto(),
588-
})
589-
return err
590585
case mr := <-metadataResults:
591586
// This can overwrite unsent values, but that's fine because
592587
// we're only interested about up-to-date values.
@@ -946,7 +941,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
946941
}
947942
}
948943

949-
err = a.scriptRunner.Init(manifest.Scripts)
944+
err = a.scriptRunner.Init(manifest.Scripts, aAPI.ScriptCompleted)
950945
if err != nil {
951946
return xerrors.Errorf("init script runner: %w", err)
952947
}

agent/agentscripts/agentscripts.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"github.com/spf13/afero"
2020
"golang.org/x/sync/errgroup"
2121
"golang.org/x/xerrors"
22+
"google.golang.org/protobuf/types/known/timestamppb"
2223

2324
"cdr.dev/slog"
2425

2526
"github.com/coder/coder/v2/agent/agentssh"
27+
"github.com/coder/coder/v2/agent/proto"
2628
"github.com/coder/coder/v2/codersdk"
2729
"github.com/coder/coder/v2/codersdk/agentsdk"
2830
)
@@ -66,7 +68,6 @@ func New(opts Options) *Runner {
6668
cronCtxCancel: cronCtxCancel,
6769
cron: cron.New(cron.WithParser(parser)),
6870
closed: make(chan struct{}),
69-
scriptTimings: make(chan TimingSpan),
7071
dataDir: filepath.Join(opts.DataDirBase, "coder-script-data"),
7172
scriptsExecuted: prometheus.NewCounterVec(prometheus.CounterOpts{
7273
Namespace: "agent",
@@ -76,30 +77,28 @@ func New(opts Options) *Runner {
7677
}
7778
}
7879

80+
type ScriptCompletedFunc func(context.Context, *proto.WorkspaceAgentScriptCompletedRequest) (*proto.WorkspaceAgentScriptCompletedResponse, error)
81+
7982
type Runner struct {
8083
Options
8184

82-
cronCtx context.Context
83-
cronCtxCancel context.CancelFunc
84-
cmdCloseWait sync.WaitGroup
85-
closed chan struct{}
86-
closeMutex sync.Mutex
87-
cron *cron.Cron
88-
initialized atomic.Bool
89-
scripts []codersdk.WorkspaceAgentScript
90-
scriptTimings chan TimingSpan
91-
dataDir string
85+
cronCtx context.Context
86+
cronCtxCancel context.CancelFunc
87+
cmdCloseWait sync.WaitGroup
88+
closed chan struct{}
89+
closeMutex sync.Mutex
90+
cron *cron.Cron
91+
initialized atomic.Bool
92+
scripts []codersdk.WorkspaceAgentScript
93+
dataDir string
94+
scriptCompleted ScriptCompletedFunc
9295

9396
// scriptsExecuted includes all scripts executed by the workspace agent. Agents
9497
// execute startup scripts, and scripts on a cron schedule. Both will increment
9598
// this counter.
9699
scriptsExecuted *prometheus.CounterVec
97100
}
98101

99-
func (r *Runner) ScriptTimings() *chan TimingSpan {
100-
return &r.scriptTimings
101-
}
102-
103102
// DataDir returns the directory where scripts data is stored.
104103
func (r *Runner) DataDir() string {
105104
return r.dataDir
@@ -122,12 +121,13 @@ func (r *Runner) RegisterMetrics(reg prometheus.Registerer) {
122121
// Init initializes the runner with the provided scripts.
123122
// It also schedules any scripts that have a schedule.
124123
// This function must be called before Execute.
125-
func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error {
124+
func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript, scriptCompleted ScriptCompletedFunc) error {
126125
if r.initialized.Load() {
127126
return xerrors.New("init: already initialized")
128127
}
129128
r.initialized.Store(true)
130129
r.scripts = scripts
130+
r.scriptCompleted = scriptCompleted
131131
r.Logger.Info(r.cronCtx, "initializing agent scripts", slog.F("script_count", len(scripts)), slog.F("log_dir", r.LogDir))
132132

133133
err := r.Filesystem.MkdirAll(r.ScriptBinDir(), 0o700)
@@ -321,12 +321,14 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript)
321321
logger.Info(ctx, fmt.Sprintf("%s script completed", logPath), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
322322
}
323323

324-
r.scriptTimings <- TimingSpan{
325-
displayName: script.DisplayName,
326-
start: start,
327-
end: end,
328-
exitCode: int32(exitCode),
329-
}
324+
_, err = r.scriptCompleted(ctx, &proto.WorkspaceAgentScriptCompletedRequest{
325+
Timing: &proto.Timing{
326+
DisplayName: script.DisplayName,
327+
Start: timestamppb.New(start),
328+
End: timestamppb.New(end),
329+
ExitCode: int32(exitCode),
330+
},
331+
})
330332
}()
331333

332334
err = cmd.Start()

agent/agentscripts/agentscripts_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"cdr.dev/slog/sloggers/slogtest"
1818
"github.com/coder/coder/v2/agent/agentscripts"
1919
"github.com/coder/coder/v2/agent/agentssh"
20+
"github.com/coder/coder/v2/agent/agenttest"
2021
"github.com/coder/coder/v2/codersdk"
2122
"github.com/coder/coder/v2/codersdk/agentsdk"
2223
"github.com/coder/coder/v2/testutil"
@@ -34,10 +35,11 @@ func TestExecuteBasic(t *testing.T) {
3435
return fLogger
3536
})
3637
defer runner.Close()
38+
aAPI := agenttest.NewFakeAgentAPI(t, slogtest.Make(t, nil), nil, nil)
3739
err := runner.Init([]codersdk.WorkspaceAgentScript{{
3840
LogSourceID: uuid.New(),
3941
Script: "echo hello",
40-
}})
42+
}}, aAPI.ScriptCompleted)
4143
require.NoError(t, err)
4244
require.NoError(t, runner.Execute(context.Background(), func(script codersdk.WorkspaceAgentScript) bool {
4345
return true
@@ -61,10 +63,11 @@ func TestEnv(t *testing.T) {
6163
cmd.exe /c echo %CODER_SCRIPT_BIN_DIR%
6264
`
6365
}
66+
aAPI := agenttest.NewFakeAgentAPI(t, slogtest.Make(t, nil), nil, nil)
6467
err := runner.Init([]codersdk.WorkspaceAgentScript{{
6568
LogSourceID: id,
6669
Script: script,
67-
}})
70+
}}, aAPI.ScriptCompleted)
6871
require.NoError(t, err)
6972

7073
ctx := testutil.Context(t, testutil.WaitLong)
@@ -103,11 +106,12 @@ func TestTimeout(t *testing.T) {
103106
t.Parallel()
104107
runner := setup(t, nil)
105108
defer runner.Close()
109+
aAPI := agenttest.NewFakeAgentAPI(t, slogtest.Make(t, nil), nil, nil)
106110
err := runner.Init([]codersdk.WorkspaceAgentScript{{
107111
LogSourceID: uuid.New(),
108112
Script: "sleep infinity",
109113
Timeout: time.Millisecond,
110-
}})
114+
}}, aAPI.ScriptCompleted)
111115
require.NoError(t, err)
112116
require.ErrorIs(t, runner.Execute(context.Background(), nil), agentscripts.ErrTimeout)
113117
}

agent/agentscripts/timings.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)