88 * Modifications Copyright (c) absurd-sqlite contributors.
99 */
1010import * as os from "node:os" ;
11+ import { Temporal } from "temporal-polyfill" ;
1112
1213/**
1314 * Minimal query interface compatible with Absurd's database operations.
@@ -39,8 +40,8 @@ export interface RetryStrategy {
3940}
4041
4142export interface CancellationPolicy {
42- maxDuration ?: number ;
43- maxDelay ?: number ;
43+ maxDuration ?: Temporal . Duration ;
44+ maxDelay ?: Temporal . Duration ;
4445}
4546
4647export interface SpawnOptions {
@@ -315,27 +316,29 @@ export class TaskContext {
315316 }
316317
317318 /**
318- * Suspends the task until the given duration (seconds) elapses.
319+ * Suspends the task until the given duration elapses.
319320 * @param stepName Checkpoint name for this wait.
320- * @param duration Duration to wait in seconds .
321+ * @param duration Duration to wait.
321322 */
322- async sleepFor ( stepName : string , duration : number ) : Promise < void > {
323- return await this . sleepUntil ( stepName , new Date ( Date . now ( ) + duration * 1000 ) ) ;
323+ async sleepFor ( stepName : string , duration : Temporal . Duration ) : Promise < void > {
324+ const now = Temporal . Now . instant ( ) ;
325+ const wakeAt = now . add ( duration ) ;
326+ return await this . sleepUntil ( stepName , wakeAt ) ;
324327 }
325328
326329 /**
327330 * Suspends the task until the specified time.
328331 * @param stepName Checkpoint name for this wait.
329332 * @param wakeAt Absolute time when the task should resume.
330333 */
331- async sleepUntil ( stepName : string , wakeAt : Date ) : Promise < void > {
334+ async sleepUntil ( stepName : string , wakeAt : Temporal . Instant ) : Promise < void > {
332335 const checkpointName = this . getCheckpointName ( stepName ) ;
333336 const state = await this . lookupCheckpoint ( checkpointName ) ;
334- const actualWakeAt = typeof state === "string" ? new Date ( state ) : wakeAt ;
337+ const actualWakeAt = typeof state === "string" ? Temporal . Instant . from ( state ) : wakeAt ;
335338 if ( ! state ) {
336- await this . persistCheckpoint ( checkpointName , wakeAt . toISOString ( ) ) ;
339+ await this . persistCheckpoint ( checkpointName , wakeAt . toString ( ) ) ;
337340 }
338- if ( Date . now ( ) < actualWakeAt . getTime ( ) ) {
341+ if ( Temporal . Instant . compare ( Temporal . Now . instant ( ) , actualWakeAt ) < 0 ) {
339342 await this . scheduleRun ( actualWakeAt ) ;
340343 throw new SuspendTask ( ) ;
341344 }
@@ -389,7 +392,7 @@ export class TaskContext {
389392 this . recordLeaseExtension ( this . claimTimeout ) ;
390393 }
391394
392- private async scheduleRun ( wakeAt : Date ) : Promise < void > {
395+ private async scheduleRun ( wakeAt : Temporal . Instant ) : Promise < void > {
393396 await this . con . query ( `SELECT absurd.schedule_run($1, $2, $3)` , [
394397 this . queueName ,
395398 this . task . run_id ,
@@ -398,24 +401,20 @@ export class TaskContext {
398401 }
399402
400403 /**
401- * Waits for an event by name and returns its payload; optionally sets a custom step name and timeout (seconds) .
404+ * Waits for an event by name and returns its payload; optionally sets a custom step name and timeout.
402405 * @param eventName Event identifier to wait for.
403406 * @param options.stepName Optional checkpoint name (defaults to $awaitEvent:<eventName>).
404- * @param options.timeout Optional timeout in seconds .
407+ * @param options.timeout Optional timeout duration .
405408 * @throws TimeoutError If the event is not received before the timeout.
406409 */
407410 async awaitEvent (
408411 eventName : string ,
409- options ?: { stepName ?: string ; timeout ?: number }
412+ options ?: { stepName ?: string ; timeout ?: Temporal . Duration }
410413 ) : Promise < JsonValue > {
411414 const stepName = options ?. stepName || `$awaitEvent:${ eventName } ` ;
412415 let timeout : number | null = null ;
413- if (
414- options ?. timeout !== undefined &&
415- Number . isFinite ( options ?. timeout ) &&
416- options ?. timeout >= 0
417- ) {
418- timeout = Math . floor ( options ?. timeout ) ;
416+ if ( options ?. timeout !== undefined ) {
417+ timeout = Math . floor ( options . timeout . total ( "seconds" ) ) ;
419418 }
420419 const checkpointName = this . getCheckpointName ( stepName ) ;
421420 const cached = await this . lookupCheckpoint ( checkpointName ) ;
@@ -1022,10 +1021,10 @@ function normalizeCancellation(
10221021 }
10231022 const normalized : JsonObject = { } ;
10241023 if ( policy . maxDuration !== undefined ) {
1025- normalized . max_duration = policy . maxDuration ;
1024+ normalized . max_duration = Math . floor ( policy . maxDuration . total ( "seconds" ) ) ;
10261025 }
10271026 if ( policy . maxDelay !== undefined ) {
1028- normalized . max_delay = policy . maxDelay ;
1027+ normalized . max_delay = Math . floor ( policy . maxDelay . total ( "seconds" ) ) ;
10291028 }
10301029 return Object . keys ( normalized ) . length > 0 ? normalized : undefined ;
10311030}
0 commit comments