Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: clear the timer a re-armed CoS schedule replaces (#5651)

Adam Eivybd73c4049f43
diff --git a/server/services/eventScheduler.js b/server/services/eventScheduler.js
index 0d5e230a3a..d04c6c6f3d 100644
--- a/server/services/eventScheduler.js
+++ b/server/services/eventScheduler.js
@@ -516,6 +516,9 @@ function scheduleNextRun(event) {
return
}
+ // Drop any handle this arm replaces (e.g. triggerNow re-arming ahead of the
+ // pending deadline) so the old timeout can't fire a duplicate run later.
+ clearPendingTimer(event.id)
const timer = createSafeTimer(() => runEventFloating(event), delay, event.id)
activeTimers.set(event.id, timer)
}
@@ -533,13 +536,23 @@ function runEventFloating(event) {
})
}
+/**
+ * Clear an event's pending timeout and forget its handle.
+ * @param {string} id - Event identifier
+ */
+function clearPendingTimer(id) {
+ const timer = activeTimers.get(id)
+ if (timer) clearTimeout(timer.timerId)
+ activeTimers.delete(id)
+}
+
/**
* Stop an event and drop its pending timer handle.
* @param {Object} event - Event object
*/
function deactivate(event) {
event.active = false
- activeTimers.delete(event.id)
+ clearPendingTimer(event.id)
}
/**
@@ -547,9 +560,10 @@ function deactivate(event) {
*
* Runs from runEvent's `finally`, so a throw anywhere earlier in the run can
* never leave a recurring schedule un-armed but still listed as active. A next
- * run time that cannot be computed (malformed cron, recurrence past its `until`)
- * deactivates the event with one error line rather than leaving the CoS Schedule
- * tab showing an armed schedule that will never fire again.
+ * run time that cannot be computed (malformed cron, a recurrence with no
+ * occurrence left in the search horizon) deactivates the event with an error
+ * line naming the reason, rather than leaving the CoS Schedule tab showing an
+ * armed schedule that will never fire again.
*
* @param {Object} event - Event object
*/
@@ -671,12 +685,7 @@ function cancel(id) {
if (!event) return false
event.active = false
-
- const timer = activeTimers.get(id)
- if (timer) {
- clearTimeout(timer.timerId)
- activeTimers.delete(id)
- }
+ clearPendingTimer(id)
scheduledEvents.delete(id)
console.log(`📅 Event cancelled: ${id}`)
@@ -695,12 +704,7 @@ function pause(id) {
if (!event) return false
event.active = false
-
- const timer = activeTimers.get(id)
- if (timer) {
- clearTimeout(timer.timerId)
- activeTimers.delete(id)
- }
+ clearPendingTimer(id)
console.log(`⏸️ Event paused: ${id}`)
return true
diff --git a/server/services/eventScheduler.test.js b/server/services/eventScheduler.test.js
index 4fe577da87..c4fd21dc81 100644
--- a/server/services/eventScheduler.test.js
+++ b/server/services/eventScheduler.test.js
@@ -375,6 +375,20 @@ describe('schedule() lifecycle with fake timers', () => {
expect(handler).toHaveBeenCalledTimes(1);
});
+ it('triggerNow() replaces the pending timer instead of leaving a duplicate run armed', async () => {
+ const handler = vi.fn();
+ schedule({ id: 'trigger-dup', type: 'interval', intervalMs: 60000, handler });
+
+ expect(await triggerNow('trigger-dup')).toBe(true);
+ expect(handler).toHaveBeenCalledTimes(1);
+ expect(getStats().activeTimers).toBe(1);
+
+ // The original 60s deadline must have been cleared by the re-arm, so only the
+ // fresh 60s-from-now timer fires in the next minute.
+ await vi.advanceTimersByTimeAsync(60000);
+ expect(handler).toHaveBeenCalledTimes(2);
+ });
+
it('triggerNow() returns false for an unknown id', async () => {
expect(await triggerNow('does-not-exist')).toBe(false);
});
@@ -431,7 +445,7 @@ describe('schedule() lifecycle with fake timers', () => {
}
});
- it('stops a cron event whose expression became unparseable, with one error log', async () => {
+ it('stops a cron event whose expression became unparseable, naming the reason', async () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
@@ -456,7 +470,7 @@ describe('schedule() lifecycle with fake timers', () => {
}
});
- it('stops a recurrence event that yields no next occurrence, with one error log', async () => {
+ it('stops a recurrence event that yields no next occurrence, naming the reason', async () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
try {