Skip to content

Commit 6877df3

Browse files
committed
cleanup some workflow stuff
1 parent d97b64f commit 6877df3

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

‎app/admin/snapshot/snapshot-panel.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ const BuildProgress = ({
178178
<div className="grid gap-3">
179179
{STEPS.map((step, index) => {
180180
const isErrorStep = errored && errorIndex === index;
181+
const isTerminalReadyStep =
182+
build.status === "ready" && step.key === "ready";
181183
let status: StepStatus;
182184
if (isErrorStep) {
183185
status = "error";
186+
} else if (isTerminalReadyStep) {
187+
status = "done";
184188
} else if (currentIndex > index) {
185189
status = "done";
186190
} else if (currentIndex === index) {

‎lib/workflows/build-snapshot-steps.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ export const stepSaveImageId = async (input: {
275275
const newId = input.snapshotImageId;
276276
const environment = SNAPSHOT_ENVIRONMENT;
277277
const result = await prisma.$transaction(async (tx) => {
278-
const currentBuild = await tx.snapshotBuild.findUnique({
279-
select: { statusTimeline: true },
280-
where: { id: input.buildId },
281-
});
282278
const existing = await tx.systemSnapshot.findUnique({
283279
select: { providerImageId: true },
284280
where: { environment },
@@ -295,14 +291,8 @@ export const stepSaveImageId = async (input: {
295291
});
296292
await tx.snapshotBuild.update({
297293
data: {
298-
finishedAt: new Date(),
299294
previousSnapshotId,
300295
snapshotId: newId,
301-
status: "ready",
302-
statusTimeline: withStatusTimestamp(
303-
currentBuild?.statusTimeline,
304-
"ready"
305-
),
306296
},
307297
where: { id: input.buildId },
308298
});
@@ -311,6 +301,15 @@ export const stepSaveImageId = async (input: {
311301
return result;
312302
};
313303

304+
export const stepMarkReady = async (input: {
305+
buildId: string;
306+
}): Promise<void> => {
307+
"use step";
308+
await setBuildStatus(input.buildId, "ready", {
309+
finishedAt: new Date(),
310+
});
311+
};
312+
314313
export const stepDeleteBuilder = async (input: {
315314
userId: string;
316315
providerBuilderId: string;

‎lib/workflows/build-snapshot.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
stepGetBuilderStatus,
1111
stepGetImageStatus,
1212
stepMarkFailed,
13+
stepMarkReady,
1314
stepNotifySuperAdmins,
1415
stepReadBuildState,
1516
stepSaveImageId,
@@ -101,6 +102,35 @@ const waitForSnapshotReady = async (
101102
throw new Error("Snapshot never became available (timed out)");
102103
};
103104

105+
const cleanupBuilderVm = async (
106+
providerBuilderId: string,
107+
userId: string
108+
): Promise<void> => {
109+
const maxAttempts = 4;
110+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
111+
const { deleted } = await stepDeleteBuilder({ providerBuilderId, userId });
112+
if (deleted) {
113+
return;
114+
}
115+
116+
const { status } = await stepGetBuilderStatus({
117+
providerBuilderId,
118+
userId,
119+
});
120+
if (status === "unknown") {
121+
return;
122+
}
123+
124+
if (attempt < maxAttempts) {
125+
await sleep(`${BUILD_POLL_SECONDS}s`);
126+
}
127+
}
128+
129+
throw new Error(
130+
`Builder VM cleanup did not complete for provider id ${providerBuilderId}`
131+
);
132+
};
133+
104134
export const buildSnapshot = async (input: {
105135
buildId: string;
106136
userId: string;
@@ -137,7 +167,7 @@ export const buildSnapshot = async (input: {
137167
userId,
138168
});
139169

140-
await stepDeleteBuilder({ providerBuilderId, userId });
170+
await cleanupBuilderVm(providerBuilderId, userId);
141171

142172
await stepDeletePreviousSnapshot({
143173
newSnapshotId: snapshotImageId,
@@ -152,6 +182,8 @@ export const buildSnapshot = async (input: {
152182
newSnapshotId: snapshotImageId,
153183
success: true,
154184
});
185+
186+
await stepMarkReady({ buildId });
155187
} catch (error) {
156188
// Normalise: the workflow runner may wrap errors in non-standard objects
157189
// after retry exhaustion — extract the message defensively.

0 commit comments

Comments
 (0)