8000 refactor: replace startup script logs EOF with starting/ready time by mafredri · Pull Request #8082 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

refactor: replace startup script logs EOF with starting/ready time #8082

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 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
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
PR comment fixes
  • Loading branch information
mafredri committed Jun 20, 2023
commit 43a0f87dc84fb04a692b4b69290db01336d34cc8
19 changes: 9 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,28 +837,27 @@ func (a *agent) runShutdownScript(ctx context.Context, script string) error {
}

func (a *agent) runScript(ctx context.Context, lifecycle, script string) (err error) {
logger := a.logger.With(slog.F("lifecycle", lifecycle))

if script == "" {
logger.Info(ctx, "not running empty script")
return nil
}

logger.Info(ctx, "running script", slog.F("script", script))
logger := a.logger.With(slog.F("lifecycle", lifecycle))

logger.Info(ctx, fmt.Sprintf("running %s script", lifecycle), slog.F("script", script))
fileWriter, err := a.filesystem.OpenFile(filepath.Join(a.logDir, fmt.Sprintf("coder-%s-script.log", lifecycle)), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return xerrors.Errorf("open %s script log file: %w", lifecycle, err)
}
defer func() {
err := fileWriter.Close()
if err != nil {
logger.Warn(ctx, "close script log file", slog.Error(err))
logger.Warn(ctx, fmt.Sprintf("close %s script log file", lifecycle), slog.Error(err))
}
}()

cmdPty, err := a.sshServer.CreateCommand(ctx, script, nil)
if err != nil {
return xerrors.Errorf("create command: %w", err)
return xerrors.Errorf("%s script: create command: %w", lifecycle, err)
}
cmd := cmdPty.AsExec()

Expand All @@ -872,7 +871,7 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) (err er
writer = io.MultiWriter(fileWriter, logsWriter)
flushedLogs, err := a.trackScriptLogs(ctx, logsReader)
if err != nil {
return xerrors.Errorf("track script logs: %w", err)
return xerrors.Errorf("track %s script logs: %w", lifecycle, err)
}
defer func() {
_ = logsWriter.Close()
Expand All @@ -894,9 +893,9 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) (err er
if xerrors.As(err, &exitError) {
exitCode = exitError.ExitCode()
}
logger.Warn(ctx, "script failed", slog.F("execution_time", execTime), slog.F("exit_code", exitCode), slog.Error(err))
logger.Warn(ctx, fmt.Sprintf("%s script failed", lifecycle), slog.F("execution_time", execTime), slog.F("exit_code", exitCode), slog.Error(err))
} else {
logger.Info(ctx, "script completed", slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
logger.Info(ctx, fmt.Sprintf("%s script completed", lifecycle), slog.F("execution_time", execTime), slog.F("exit_code", exitCode))
}
}()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Small cleanup to unify logging between startup and shutdown scripts.


Expand All @@ -907,7 +906,7 @@ func (a *agent) runScript(ctx context.Context, lifecycle, script string) (err er
return ctx.Err()
}

return xerrors.Errorf("run: %w", err)
return xerrors.Errorf("%s script: run: %w", lifecycle, err)
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ALTER TABLE workspace_agents
DROP COLUMN started_at,
DROP COLUMN ready_at;

-- We won't bring back log entries where eof = TRUE, but this doesn't matter
-- as the implementation doesn't require it and hasn't been part of a release.
ALTER TABLE workspace_agent_startup_logs ADD COLUMN eof boolean NOT NULL DEFAULT false;

COMMENT ON COLUMN workspace_agent_startup_logs.eof IS 'End of file reached';
Expand Down
6 changes: 3 additions & 3 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ func (api *API) patchWorkspaceAgentStartupLogs(rw http.ResponseWriter, r *http.R
// Ensure logs are not written after script ended.
scriptEndedError := xerrors.New("startup script has ended")
err := api.Database.InTx(func(db database.Store) error {
ss, err := db.GetWorkspaceAgentLifecycleStateByID(ctx, workspaceAgent.ID)
state, err := db.GetWorkspaceAgentLifecycleStateByID(ctx, workspaceAgent.ID)
if err != nil {
return xerrors.Errorf("workspace agent startup script status: %w", err)
}

if ss.ReadyAt.Valid {
if state.ReadyAt.Valid {
// The agent startup script has already ended, so we don't want to
// process any more logs.
return scriptEndedError
Expand Down Expand Up @@ -1626,7 +1626,7 @@ func (api *API) workspaceAgentReportLifecycle(rw http.ResponseWriter, r *http.Re
ReadyAt: readyAt,
})
if err != nil {
logger.Warn(ctx, "failed to update lifecycle state", slog.Error(err))
logger.Error(ctx, "failed to update lifecycle state", slog.Error(err))
httpapi.InternalServerError(rw, err)
return
}
Expand Down
3 changes: 0 additions & 3 deletions scripts/coder-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ if [[ ! -x "${CODER_DEV_BIN}" ]]; then
exit 1
fi

if [[ -x /tmp/coder ]]; then
CODER_DEV_BIN=/tmp/coder
fi
exec "${CODER_DEV_BIN}" --global-config "${CODER_DEV_DIR}" "$@"
0