From 8f35724bde471f3b6401992669f2680b4df88a1b Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 28 Mar 2026 20:09:32 -0400 Subject: [PATCH] fix: queue on-create jobs sequentially and fix history ordering runJobsOnCreate now awaits each job before starting the next, ensuring they don't stomp each other's DB writes in parallel. getInstanceHistory changed to ORDER BY changed_at ASC, id ASC so the creation event (lowest id) is always first regardless of same-second timestamps. Co-Authored-By: Claude Sonnet 4.6 --- server/db.js | 2 +- server/jobs.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/db.js b/server/db.js index 52ef555..2a4b6fa 100644 --- a/server/db.js +++ b/server/db.js @@ -227,7 +227,7 @@ export function importInstances(rows, historyRows = []) { export function getInstanceHistory(vmid) { return db.prepare( - 'SELECT * FROM instance_history WHERE vmid = ? ORDER BY changed_at DESC' + 'SELECT * FROM instance_history WHERE vmid = ? ORDER BY changed_at ASC, id ASC' ).all(vmid); } diff --git a/server/jobs.js b/server/jobs.js index f6b1a59..dc61d1f 100644 --- a/server/jobs.js +++ b/server/jobs.js @@ -132,7 +132,9 @@ const _intervals = new Map(); export async function runJobsOnCreate() { for (const job of getJobs()) { const cfg = JSON.parse(job.config || '{}'); - if (cfg.run_on_create) runJob(job.id).catch(e => console.error(`runJobsOnCreate job ${job.id}:`, e)); + if (cfg.run_on_create) { + try { await runJob(job.id); } catch (e) { console.error(`runJobsOnCreate job ${job.id}:`, e); } + } } }