Skip to content

feat(agent): Add shutdown lifecycle states and shutdown_script support #6139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix startup/shutdown script log messaging
  • Loading branch information
mafredri committed Mar 6, 2023
commit 292a87861383d036a8f3625f40f2fbfcde5692d5
22 changes: 12 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,10 @@ func (a *agent) run(ctx context.Context) error {
}
}

lifecycleState := codersdk.WorkspaceAgentLifecycleReady
scriptDone := make(chan error, 1)
scriptStart := time.Now()
err := a.trackConnGoroutine(func() {
err = a.trackConnGoroutine(func() {
defer close(scriptDone)
scriptDone <- a.runStartupScript(ctx, metadata.StartupScript)
})
Expand Down Expand Up @@ -343,15 +344,16 @@ func (a *agent) run(ctx context.Context) error {
if errors.Is(err, context.Canceled) {
return
}
execTime := time.Since(scriptStart)
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
if err != nil {
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
} else {
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
// Only log if there was a startup script.
if metadata.StartupScript != "" {
execTime := time.Since(scriptStart)
if err != nil {
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
} else {
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
}
}

a.setLifecycle(ctx, lifecycleState)
}()
}
Expand Down Expand Up @@ -1325,7 +1327,7 @@ func (a *agent) Close() error {
// TODO(mafredri): Only run shutdown script if the agent is 'ready'?

lifecycleState := codersdk.WorkspaceAgentLifecycleOff
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok {
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok && metadata.ShutdownScript != "" {
scriptDone := make(chan error, 1)
scriptStart := time.Now()
go func() {
Expand Down