fix: Remove scheduled workflows if they no longer exist when executed (#7673)
This commit is contained in:
@@ -6,7 +6,12 @@ import {
|
||||
WorkflowScheduler,
|
||||
} from "@medusajs/orchestration"
|
||||
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
|
||||
import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils"
|
||||
import {
|
||||
InjectSharedContext,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
isString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
MedusaWorkflow,
|
||||
ReturnWorkflow,
|
||||
@@ -108,7 +113,10 @@ export class WorkflowOrchestratorService {
|
||||
: workflowIdOrWorkflow.getName()
|
||||
|
||||
if (!workflowId) {
|
||||
throw new Error("Workflow ID is required")
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Workflow ID is required`
|
||||
)
|
||||
}
|
||||
|
||||
context ??= {}
|
||||
@@ -122,7 +130,10 @@ export class WorkflowOrchestratorService {
|
||||
|
||||
const exportedWorkflow: any = MedusaWorkflow.getWorkflow(workflowId)
|
||||
if (!exportedWorkflow) {
|
||||
throw new Error(`Workflow with id "${workflowId}" not found.`)
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Workflow with id "${workflowId}" not found.`
|
||||
)
|
||||
}
|
||||
|
||||
const flow = exportedWorkflow(container as MedusaContainer)
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
TransactionStep,
|
||||
} from "@medusajs/orchestration"
|
||||
import { ModulesSdkTypes } from "@medusajs/types"
|
||||
import { TransactionState } from "@medusajs/utils"
|
||||
import { MedusaError, TransactionState } from "@medusajs/utils"
|
||||
import { WorkflowOrchestratorService } from "@services"
|
||||
import { CronExpression, parseExpression } from "cron-parser"
|
||||
|
||||
@@ -266,9 +266,9 @@ export class InMemoryDistributedTransactionStorage
|
||||
}
|
||||
|
||||
async removeAll(): Promise<void> {
|
||||
this.scheduled.forEach((_, key) => {
|
||||
this.remove(key)
|
||||
})
|
||||
for (const [key, job] of this.scheduled) {
|
||||
await this.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
async jobHandler(jobId: string) {
|
||||
@@ -297,9 +297,22 @@ export class InMemoryDistributedTransactionStorage
|
||||
config: job.config,
|
||||
})
|
||||
|
||||
// With running the job after setting a new timer we basically allow for concurrent runs, unless we add idempotency keys once they are supported.
|
||||
await this.workflowOrchestratorService_.run(jobId, {
|
||||
throwOnError: false,
|
||||
})
|
||||
try {
|
||||
// With running the job after setting a new timer we basically allow for concurrent runs, unless we add idempotency keys once they are supported.
|
||||
await this.workflowOrchestratorService_.run(jobId, {
|
||||
throwOnError: false,
|
||||
})
|
||||
} catch (e) {
|
||||
if (e instanceof MedusaError && e.type === MedusaError.Types.NOT_FOUND) {
|
||||
console.warn(
|
||||
`Tried to execute a scheduled workflow with ID ${jobId} that does not exist, removing it from the scheduler.`
|
||||
)
|
||||
|
||||
await this.remove(jobId)
|
||||
return
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user