refactor: Extract processor job into separate background jobs

This commit is contained in:
Faruk AYDIN
2022-10-13 18:45:01 +02:00
parent 3c3bb82e97
commit 56a9aeece7
17 changed files with 374 additions and 213 deletions

View File

@@ -1,7 +1,6 @@
import Context from '../../types/express/context';
import Processor from '../../services/processor';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import processorQueue from '../../queues/processor';
import flowQueue from '../../queues/flow';
type Params = {
input: {
@@ -14,30 +13,25 @@ const executeFlow = async (
params: Params,
context: Context
) => {
const untilStep = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('connection')
.findOne({
'steps.id': params.input.stepId,
})
.throwIfNotFound();
const flow = await untilStep.$relatedQuery('flow');
const executionStep = await new Processor(flow, {
untilStep,
testRun: true,
}).run();
await untilStep.$query().patch({
status: 'completed',
});
if (executionStep.errorDetails) {
throw new Error(JSON.stringify(executionStep.errorDetails));
}
return { data: executionStep.dataOut, step: untilStep };
// const untilStep = await context.currentUser
// .$relatedQuery('steps')
// .withGraphFetched('connection')
// .findOne({
// 'steps.id': params.input.stepId,
// })
// .throwIfNotFound();
// const flow = await untilStep.$relatedQuery('flow');
// const executionStep = await new Processor(flow, {
// untilStep,
// testRun: true,
// }).run();
// await untilStep.$query().patch({
// status: 'completed',
// });
// if (executionStep.errorDetails) {
// throw new Error(JSON.stringify(executionStep.errorDetails));
// }
// return { data: executionStep.dataOut, step: untilStep };
};
export default executeFlow;

View File

@@ -1,5 +1,5 @@
import Context from '../../types/express/context';
import processorQueue from '../../queues/processor';
import flowQueue from '../../queues/flow';
type Params = {
input: {
@@ -9,7 +9,7 @@ type Params = {
};
const JOB_NAME = 'processorJob';
const EVERY_15_MINUTES_CRON = '*/15 * * * *';
const EVERY_15_MINUTES_CRON = '*/1 * * * *';
const updateFlowStatus = async (
_parent: unknown,
@@ -32,7 +32,7 @@ const updateFlowStatus = async (
});
const triggerStep = await flow.getTriggerStep();
const trigger = await triggerStep.getTrigger();
const trigger = await triggerStep.getTriggerCommand();
const interval = trigger.getInterval?.(triggerStep.parameters);
const repeatOptions = {
cron: interval || EVERY_15_MINUTES_CRON,
@@ -43,7 +43,7 @@ const updateFlowStatus = async (
published_at: new Date().toISOString(),
});
await processorQueue.add(
await flowQueue.add(
JOB_NAME,
{ flowId: flow.id },
{
@@ -52,10 +52,10 @@ const updateFlowStatus = async (
}
);
} else {
const repeatableJobs = await processorQueue.getRepeatableJobs();
const repeatableJobs = await flowQueue.getRepeatableJobs();
const job = repeatableJobs.find((job) => job.id === flow.id);
await processorQueue.removeRepeatableByKey(job.key);
await flowQueue.removeRepeatableByKey(job.key);
}
return flow;