fix(workflow-engine-inmemory): fix cron job schedule (#13151)
This commit is contained in:
1 parent
b7083b9f0f
commit
b1cba9fdeb
2 files changed
+22
-3
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflow-engine-inmemory": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(workflow-engine-inmemory): fix cron job schedule
|
||||||
+17
-3
@@ -28,27 +28,41 @@ import { WorkflowOrchestratorService } from "@services"
|
|||||||
import { type CronExpression, parseExpression } from "cron-parser"
|
import { type CronExpression, parseExpression } from "cron-parser"
|
||||||
import { WorkflowExecution } from "../models/workflow-execution"
|
import { WorkflowExecution } from "../models/workflow-execution"
|
||||||
|
|
||||||
|
function calculateDelayFromExpression(expression: CronExpression): number {
|
||||||
|
const nextTime = expression.next().getTime()
|
||||||
|
const now = Date.now()
|
||||||
|
const delay = nextTime - now
|
||||||
|
|
||||||
|
// If the calculated delay is negative or zero, get the next occurrence
|
||||||
|
if (delay <= 0) {
|
||||||
|
const nextNextTime = expression.next().getTime()
|
||||||
|
return Math.max(1, nextNextTime - now)
|
||||||
|
}
|
||||||
|
|
||||||
|
return delay
|
||||||
|
}
|
||||||
|
|
||||||
function parseNextExecution(
|
function parseNextExecution(
|
||||||
optionsOrExpression: SchedulerOptions | CronExpression | string | number
|
optionsOrExpression: SchedulerOptions | CronExpression | string | number
|
||||||
) {
|
) {
|
||||||
if (typeof optionsOrExpression === "object") {
|
if (typeof optionsOrExpression === "object") {
|
||||||
if ("cron" in optionsOrExpression) {
|
if ("cron" in optionsOrExpression) {
|
||||||
const expression = parseExpression(optionsOrExpression.cron)
|
const expression = parseExpression(optionsOrExpression.cron)
|
||||||
return expression.next().getTime() - Date.now()
|
return calculateDelayFromExpression(expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("interval" in optionsOrExpression) {
|
if ("interval" in optionsOrExpression) {
|
||||||
return optionsOrExpression.interval
|
return optionsOrExpression.interval
|
||||||
}
|
}
|
||||||
|
|
||||||
return optionsOrExpression.next().getTime() - Date.now()
|
return calculateDelayFromExpression(optionsOrExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = parseInt(`${optionsOrExpression}`)
|
const result = parseInt(`${optionsOrExpression}`)
|
||||||
|
|
||||||
if (isNaN(result)) {
|
if (isNaN(result)) {
|
||||||
const expression = parseExpression(`${optionsOrExpression}`)
|
const expression = parseExpression(`${optionsOrExpression}`)
|
||||||
return expression.next().getTime() - Date.now()
|
return calculateDelayFromExpression(expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in new issue
Block a user