Skip to content

Commit 1140e29

Browse files
authored
chore: autobuild/executor: refactor big switch statement for legibility (coder#3116)
1 parent ef7d357 commit 1140e29

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

coderd/autobuild/executor/lifecycle_executor.go

+39-39
Original file line numberDiff line numberDiff line change
@@ -113,46 +113,11 @@ func (e *Executor) runOnce(t time.Time) Stats {
113113
continue
114114
}
115115

116-
if !priorJob.CompletedAt.Valid || priorJob.Error.String != "" {
117-
e.log.Debug(e.ctx, "last workspace build did not complete successfully, skipping",
118-
slog.F("workspace_id", ws.ID),
119-
slog.F("error", priorJob.Error.String),
120-
)
121-
continue
122-
}
123-
124-
var validTransition database.WorkspaceTransition
125-
var nextTransition time.Time
126-
switch priorHistory.Transition {
127-
case database.WorkspaceTransitionStart:
128-
validTransition = database.WorkspaceTransitionStop
129-
if priorHistory.Deadline.IsZero() {
130-
e.log.Debug(e.ctx, "latest workspace build has zero deadline, skipping",
131-
slog.F("workspace_id", ws.ID),
132-
slog.F("workspace_build_id", priorHistory.ID),
133-
)
134-
continue
135-
}
136-
// For stopping, do not truncate. This is inconsistent with autostart, but
137-
// it ensures we will not stop too early.
138-
nextTransition = priorHistory.Deadline
139-
case database.WorkspaceTransitionStop:
140-
validTransition = database.WorkspaceTransitionStart
141-
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
142-
if err != nil {
143-
e.log.Debug(e.ctx, "workspace has invalid autostart schedule, skipping",
144-
slog.F("workspace_id", ws.ID),
145-
slog.F("autostart_schedule", ws.AutostartSchedule.String),
146-
)
147-
continue
148-
}
149-
// Round down to the nearest minute, as this is the finest granularity cron supports.
150-
// Truncate is probably not necessary here, but doing it anyway to be sure.
151-
nextTransition = sched.Next(priorHistory.CreatedAt).Truncate(time.Minute)
152-
default:
153-
e.log.Debug(e.ctx, "last transition not valid for autostart or autostop",
116+
validTransition, nextTransition, err := getNextTransition(ws, priorHistory, priorJob)
117+
if err != nil {
118+
e.log.Debug(e.ctx, "skipping workspace",
119+
slog.Error(err),
154120
slog.F("workspace_id", ws.ID),
155-
slog.F("latest_build_transition", priorHistory.Transition),
156121
)
157122
continue
158123
}
@@ -186,6 +151,41 @@ func (e *Executor) runOnce(t time.Time) Stats {
186151
return stats
187152
}
188153

154+
func getNextTransition(
155+
ws database.Workspace,
156+
priorHistory database.WorkspaceBuild,
157+
priorJob database.ProvisionerJob,
158+
) (
159+
validTransition database.WorkspaceTransition,
160+
nextTransition time.Time,
161+
err error,
162+
) {
163+
if !priorJob.CompletedAt.Valid || priorJob.Error.String != "" {
164+
return "", time.Time{}, xerrors.Errorf("last workspace build did not complete successfully")
165+
}
166+
167+
switch priorHistory.Transition {
168+
case database.WorkspaceTransitionStart:
169+
if priorHistory.Deadline.IsZero() {
170+
return "", time.Time{}, xerrors.Errorf("latest workspace build has zero deadline")
171+
}
172+
// For stopping, do not truncate. This is inconsistent with autostart, but
173+
// it ensures we will not stop too early.
174+
return database.WorkspaceTransitionStop, priorHistory.Deadline, nil
175+
case database.WorkspaceTransitionStop:
176+
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
177+
if err != nil {
178+
return "", time.Time{}, xerrors.Errorf("workspace has invalid autostart schedule: %w", err)
179+
}
180+
// Round down to the nearest minute, as this is the finest granularity cron supports.
181+
// Truncate is probably not necessary here, but doing it anyway to be sure.
182+
nextTransition = sched.Next(priorHistory.CreatedAt).Truncate(time.Minute)
183+
return database.WorkspaceTransitionStart, nextTransition, nil
184+
default:
185+
return "", time.Time{}, xerrors.Errorf("last transition not valid for autostart or autostop")
186+
}
187+
}
188+
189189
// TODO(cian): this function duplicates most of api.postWorkspaceBuilds. Refactor.
190190
// See: https://github.com/coder/coder/issues/1401
191191
func build(ctx context.Context, store database.Store, workspace database.Workspace, trans database.WorkspaceTransition, priorHistory database.WorkspaceBuild, priorJob database.ProvisionerJob) error {

0 commit comments

Comments
 (0)