Skip to content

Commit d843e72

Browse files
committed
fix(api): standardize updatedAt handling in cron trigger queries and improve date comparison logic
1 parent 4166743 commit d843e72

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

apps/api/src/db/queries.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,13 +1120,17 @@ export async function upsertCronTrigger(
11201120
db: ReturnType<typeof createDatabase>,
11211121
values: CronTriggerInsert
11221122
): Promise<CronTriggerRow> {
1123+
const now = new Date();
11231124
const updateSet = {
11241125
...values,
1125-
updatedAt: new Date(),
1126+
updatedAt: now,
11261127
};
11271128
const [upsertedCron] = await db
11281129
.insert(cronTriggers)
1129-
.values(values)
1130+
.values({
1131+
...values,
1132+
updatedAt: now,
1133+
})
11301134
.onConflictDoUpdate({
11311135
target: cronTriggers.workflowId,
11321136
set: updateSet,
@@ -1151,12 +1155,13 @@ export async function updateCronTriggerRunTimes(
11511155
nextRunAt: Date,
11521156
lastRun: Date
11531157
): Promise<CronTriggerRow | undefined> {
1158+
const now = new Date();
11541159
const [updatedTrigger] = await db
11551160
.update(cronTriggers)
11561161
.set({
11571162
lastRun: lastRun,
11581163
nextRunAt: nextRunAt,
1159-
updatedAt: new Date(),
1164+
updatedAt: now,
11601165
})
11611166
.where(eq(cronTriggers.workflowId, workflowId))
11621167
.returning();
@@ -1173,13 +1178,11 @@ export async function updateCronTriggerRunTimes(
11731178
export async function getDueCronTriggers(
11741179
db: ReturnType<typeof createDatabase>,
11751180
now: Date
1176-
): Promise<
1177-
{
1178-
cronTrigger: CronTriggerRow;
1179-
workflow: WorkflowRow;
1180-
deployment: DeploymentRow | null;
1181-
}[]
1182-
> {
1181+
): Promise<{
1182+
cronTrigger: CronTriggerRow;
1183+
workflow: WorkflowRow;
1184+
deployment: DeploymentRow | null;
1185+
}[]> {
11831186
const latestByWorkflow = db
11841187
.select({
11851188
workflowId: deployments.workflowId,
@@ -1202,7 +1205,12 @@ export async function getDueCronTriggers(
12021205
selectedDeployment: selectedDeployment,
12031206
})
12041207
.from(cronTriggers)
1205-
.where(and(eq(cronTriggers.active, true), lte(cronTriggers.nextRunAt, now)))
1208+
.where(
1209+
and(
1210+
eq(cronTriggers.active, true),
1211+
sql`${cronTriggers.nextRunAt} <= ${now.getTime()}`
1212+
)
1213+
)
12061214
.innerJoin(workflows, eq(workflows.id, cronTriggers.workflowId))
12071215
.innerJoin(latestByWorkflow, eq(latestByWorkflow.workflowId, workflows.id))
12081216
.innerJoin(

0 commit comments

Comments
 (0)