{"version":3,"sources":["../../../../../src/queues/operations/runJobs/runJSONJob/index.ts"],"sourcesContent":["import type { Job } from '../../../../index.js'\nimport type { PayloadRequest } from '../../../../types/index.js'\nimport type { WorkflowJSON, WorkflowStep } from '../../../config/types/workflowJSONTypes.js'\nimport type { WorkflowConfig } from '../../../config/types/workflowTypes.js'\nimport type { RunJobsSilent } from '../../../localAPI.js'\nimport type { UpdateJobFunction } from '../runJob/getUpdateJobFunction.js'\nimport type { JobRunStatus } from '../runJob/index.js'\n\nimport { handleWorkflowError } from '../../../errors/handleWorkflowError.js'\nimport { WorkflowError } from '../../../errors/index.js'\nimport { getCurrentDate } from '../../../utilities/getCurrentDate.js'\nimport { getRunTaskFunction } from '../runJob/getRunTaskFunction.js'\n\ntype Args = {\n job: Job\n req: PayloadRequest\n /**\n * If set to true, the job system will not log any output to the console (for both info and error logs).\n * Can be an option for more granular control over logging.\n *\n * This will not automatically affect user-configured logs (e.g. if you call `console.log` or `payload.logger.info` in your job code).\n *\n * @default false\n */\n silent?: RunJobsSilent\n updateJob: UpdateJobFunction\n workflowConfig: WorkflowConfig\n workflowHandler: WorkflowJSON\n}\n\nexport type RunJSONJobResult = {\n status: JobRunStatus\n}\n\nexport const runJSONJob = async ({\n job,\n req,\n silent = false,\n updateJob,\n workflowConfig,\n workflowHandler,\n}: Args): Promise => {\n const stepsToRun: WorkflowStep[] = []\n\n for (const step of workflowHandler) {\n if ('task' in step) {\n if (job?.taskStatus?.[step.task]?.[step.id]?.complete) {\n continue\n }\n } else {\n if (job?.taskStatus?.['inline']?.[step.id]?.complete) {\n continue\n }\n }\n if (step.condition && !step.condition({ job })) {\n continue\n }\n stepsToRun.push(step)\n }\n\n const tasks = getRunTaskFunction(job, workflowConfig, req, false, updateJob)\n const inlineTask = getRunTaskFunction(job, workflowConfig, req, true, updateJob)\n\n // Run the job\n try {\n await Promise.all(\n stepsToRun.map(async (step) => {\n if ('task' in step) {\n await tasks[step.task]!(step.id, {\n input: step.input ? step.input({ job }) : {},\n retries: step.retries,\n })\n } else {\n await inlineTask(step.id, {\n retries: step.retries,\n task: step.inlineTask as any, // TODO: Fix type\n })\n }\n }),\n )\n } catch (error) {\n const { hasFinalError } = await handleWorkflowError({\n error:\n error instanceof WorkflowError\n ? error\n : new WorkflowError({\n job,\n message:\n typeof error === 'object' && error && 'message' in error\n ? (error.message as string)\n : 'An unhandled error occurred',\n workflowConfig,\n }),\n silent,\n\n req,\n updateJob,\n })\n\n return {\n status: hasFinalError ? 'error-reached-max-retries' : 'error',\n }\n }\n\n // Check if workflow has completed\n let workflowCompleted = false\n for (const [slug, map] of Object.entries(job.taskStatus)) {\n for (const [id, taskStatus] of Object.entries(map)) {\n if (taskStatus.complete) {\n const step = workflowHandler.find((step) => {\n if ('task' in step) {\n return step.task === slug && step.id === id\n } else {\n return step.id === id && slug === 'inline'\n }\n })\n if (step?.completesJob) {\n workflowCompleted = true\n break\n }\n }\n }\n }\n\n if (workflowCompleted) {\n await updateJob({\n completedAt: getCurrentDate().toISOString(),\n processing: false,\n totalTried: (job.totalTried ?? 0) + 1,\n })\n\n return {\n status: 'success',\n }\n } else {\n // Retry the job - no need to bump processing or totalTried as this does not count as a retry. A condition of a different task might have just opened up!\n return await runJSONJob({\n job,\n req,\n updateJob,\n workflowConfig,\n workflowHandler,\n })\n }\n}\n"],"names":["handleWorkflowError","WorkflowError","getCurrentDate","getRunTaskFunction","runJSONJob","job","req","silent","updateJob","workflowConfig","workflowHandler","stepsToRun","step","taskStatus","task","id","complete","condition","push","tasks","inlineTask","Promise","all","map","input","retries","error","hasFinalError","message","status","workflowCompleted","slug","Object","entries","find","completesJob","completedAt","toISOString","processing","totalTried"],"mappings":"AAQA,SAASA,mBAAmB,QAAQ,yCAAwC;AAC5E,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,cAAc,QAAQ,uCAAsC;AACrE,SAASC,kBAAkB,QAAQ,kCAAiC;AAuBpE,OAAO,MAAMC,aAAa,OAAO,EAC/BC,GAAG,EACHC,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