fix: prevent cancelled jobs from running and startup race with new requests

Atomic claim in resolveQueuedDeployment only transitions deployments
still in queued status, so a cancelled record cannot be set back to
running. Move initCancelDeployments before server.listen so no HTTP
requests can arrive during stale-record cleanup.
This commit is contained in:
Yash Kumar 2026-09-11 23:12:42 +05:30
parent dfc3a5ffd3
commit 337f47b51c
2 changed files with 25 additions and 6 deletions

View File

@ -56,17 +56,18 @@ void app.prepare().then(async () => {
setupDockerStatsMonitoringSocketServer(server);
}
server.listen(PORT, HOST);
console.log(`Server Started on: http://${HOST}:${PORT}`);
if (process.env.NODE_ENV === "production" && !IS_CLOUD) {
await initCancelDeployments();
createDefaultMiddlewares();
await initializeNetwork();
await initCronJobs();
await initSchedules();
await initCancelDeployments();
await initVolumeBackupsCronJobs();
await sendDokployRestartNotifications();
}
server.listen(PORT, HOST);
console.log(`Server Started on: http://${HOST}:${PORT}`);
await initEnterpriseBackupCronJobs();
if (!IS_CLOUD) {

View File

@ -134,10 +134,28 @@ export const resolveQueuedDeployment = async <
return createNew();
}
try {
const deployment = await findDeploymentById(deploymentId);
await updateDeploymentStatus(deployment.deploymentId, "running");
const claimed = await db
.update(deployments)
.set({
status: "running",
startedAt: new Date().toISOString(),
})
.where(
and(
eq(deployments.deploymentId, deploymentId),
eq(deployments.status, "queued"),
),
)
.returning();
if (claimed.length === 0) {
console.log(`Deployment ${deploymentId} is no longer queued, skipping`);
await resetStatus().catch(console.error);
return null;
}
await setRunning();
return deployment as unknown as T;
return claimed[0] as unknown as T;
} catch (error) {
console.error(`Deployment ${deploymentId} lookup failed, skipping`, error);
await updateDeploymentStatus(deploymentId, "error").catch(console.error);