This adds new `payload.jobs.cancel` and `payload.jobs.cancelByID` methods that allow you to cancel already-running jobs, or prevent queued jobs from running. While it's not possible to cancel a function mid-execution, this will stop job execution the next time the job makes a request to the db, which happens after every task.
23 lines
517 B
TypeScript
23 lines
517 B
TypeScript
import type { WorkflowConfig } from 'payload'
|
|
|
|
/**
|
|
* Should finish after 2 seconds
|
|
*/
|
|
export const longRunningWorkflow: WorkflowConfig<'longRunning'> = {
|
|
slug: 'longRunning',
|
|
inputSchema: [],
|
|
handler: async ({ inlineTask }) => {
|
|
for (let i = 0; i < 4; i += 1) {
|
|
await inlineTask(String(i), {
|
|
task: async () => {
|
|
// Wait 500ms
|
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
return {
|
|
output: {},
|
|
}
|
|
},
|
|
})
|
|
}
|
|
},
|
|
}
|