mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
relay: re-arm the renewal from the wall clock on a resume before it is due
A sleep that ends before the renewal is due still cost the timer clock the whole sleep, so the renewal fired that much late. On resume a task not yet due is re-armed for the wall-clock time left.
This commit is contained in:
@@ -43,16 +43,21 @@ describe('RelayWallClockTimer', () => {
|
||||
expect(source.count()).toBe(0)
|
||||
})
|
||||
|
||||
it('keeps waiting when the machine resumes before the task is due', () => {
|
||||
it('re-arms from the wall clock when the machine resumes before the task is due', () => {
|
||||
vi.useFakeTimers()
|
||||
let wall = 1_000_000
|
||||
const source = resumeSource()
|
||||
const timer = new RelayWallClockTimer(() => wall, source.subscribe)
|
||||
const task = vi.fn()
|
||||
timer.schedule(180_000, task)
|
||||
// A 60 s sleep: the timer clock lost it, so 120 s of wall time are left.
|
||||
wall += 60_000
|
||||
source.resume()
|
||||
expect(task).not.toHaveBeenCalled()
|
||||
vi.advanceTimersByTime(119_000)
|
||||
expect(task).not.toHaveBeenCalled()
|
||||
vi.advanceTimersByTime(1_000)
|
||||
expect(task).toHaveBeenCalledTimes(1)
|
||||
vi.advanceTimersByTime(180_000)
|
||||
expect(task).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
@@ -8,7 +8,7 @@ const subscribeResume: SubscribeResume = (onResume) =>
|
||||
// A one-shot timer due at a wall-clock time. Node timers run on a clock that stops while the
|
||||
// machine sleeps, so a relay token renewal due during a sleep would fire that much later after
|
||||
// wake, after the token's wall-clock expiry: the relay then closes the still-live control 4401.
|
||||
// On resume, a task already due runs at once.
|
||||
// On resume, a task already due runs at once; one not yet due is re-armed from the wall clock.
|
||||
export class RelayWallClockTimer {
|
||||
private timer: ReturnType<typeof setTimeout> | null = null
|
||||
private unsubscribe: (() => void) | null = null
|
||||
@@ -24,12 +24,20 @@ export class RelayWallClockTimer {
|
||||
this.cancel()
|
||||
this.dueAt = this.now() + delayMs
|
||||
this.task = task
|
||||
this.timer = setTimeout(() => this.fire(), delayMs)
|
||||
// The lifecycle reports the current state on subscribe; only later resumes count.
|
||||
this.arm(delayMs)
|
||||
// The lifecycle reports the current state on subscribe; only later resumes count. On each
|
||||
// resume a due task runs, and one not yet due is re-armed for the time left on the wall clock
|
||||
// (the timer clock lost the whole sleep).
|
||||
let subscribed = false
|
||||
this.unsubscribe = this.onResume(() => {
|
||||
if (subscribed && this.task === task && this.now() >= this.dueAt) {
|
||||
if (!subscribed || this.task !== task) {
|
||||
return
|
||||
}
|
||||
const left = this.dueAt - this.now()
|
||||
if (left <= 0) {
|
||||
this.fire()
|
||||
} else {
|
||||
this.arm(left)
|
||||
}
|
||||
})
|
||||
subscribed = true
|
||||
@@ -45,6 +53,13 @@ export class RelayWallClockTimer {
|
||||
this.task = null
|
||||
}
|
||||
|
||||
private arm(delayMs: number): void {
|
||||
if (this.timer) {
|
||||
clearTimeout(this.timer)
|
||||
}
|
||||
this.timer = setTimeout(() => this.fire(), delayMs)
|
||||
}
|
||||
|
||||
private fire(): void {
|
||||
const task = this.task
|
||||
this.cancel()
|
||||
|
||||
Reference in New Issue
Block a user