From 1bd381d1babf898f8381b9fc45dcdd85018d2838 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Fri, 7 Jun 2024 07:29:11 -0400 Subject: [PATCH] fix(dobj): support updatedAt as Date or iso String --- src/domain/objects/AsyncTask.ts | 2 +- src/logic/withAsyncTaskExecutionLifecycleExecute.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/domain/objects/AsyncTask.ts b/src/domain/objects/AsyncTask.ts index c80851d..c2a2829 100644 --- a/src/domain/objects/AsyncTask.ts +++ b/src/domain/objects/AsyncTask.ts @@ -53,6 +53,6 @@ export enum AsyncTaskStatus { */ export interface AsyncTask { uuid?: string; - updatedAt?: string; + updatedAt?: string | Date; status: AsyncTaskStatus; } diff --git a/src/logic/withAsyncTaskExecutionLifecycleExecute.ts b/src/logic/withAsyncTaskExecutionLifecycleExecute.ts index dc0858e..e27caf2 100644 --- a/src/logic/withAsyncTaskExecutionLifecycleExecute.ts +++ b/src/logic/withAsyncTaskExecutionLifecycleExecute.ts @@ -92,8 +92,17 @@ export const withAsyncTaskExecutionLifecycleExecute = < // if the task was updated less than 15 minutes ago, then it may still be being attempted, so throw an error so this message will get retried eventually const attemptTimeoutSeconds = options?.attempt?.timeout.seconds ?? 15 * 60; + const updatedAtLast = + typeof foundTask.updatedAt === 'string' + ? parseISO(foundTask.updatedAt) + : foundTask.updatedAt; + if (!updatedAtLast) + throw new UnexpectedCodePathError( + 'task did not have an .updatedAt attribute. this is required for reliable async-tasks', + { foundTask }, + ); const attemptTimeoutAt = addSeconds( - parseISO(foundTask.updatedAt!), + updatedAtLast, attemptTimeoutSeconds, // default to 15 min ); const now = new Date();