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

@@ -2,23 +2,37 @@ import createHttpClient from './http-client';
import Connection from '../models/connection';
import Flow from '../models/flow';
import Step from '../models/step';
import { IJSONObject, IApp, IGlobalVariable } from '@automatisch/types';
import Execution from '../models/execution';
import {
IJSONObject,
IApp,
IGlobalVariable,
ITriggerDataItem,
} from '@automatisch/types';
import triggerQueue from '../queues/trigger';
type GlobalVariableOptions = {
connection?: Connection;
app: IApp;
flow?: Flow;
step?: Step;
execution?: Execution;
};
const globalVariable = async (
options: GlobalVariableOptions
): Promise<IGlobalVariable> => {
const { connection, app, flow, step } = options;
const { connection, app, flow, step, execution } = options;
const lastInternalId = await flow?.lastInternalId();
return {
const trigger = await step?.getTriggerCommand();
const nextStep = await flow
?.$relatedQuery('steps')
.where({ position: step.position + 1 })
.first();
const variable: IGlobalVariable = {
auth: {
set: async (args: IJSONObject) => {
if (connection) {
@@ -37,12 +51,45 @@ const globalVariable = async (
app: app,
http: createHttpClient({ baseURL: app.baseUrl }),
flow: {
id: flow?.id,
lastInternalId,
},
step: {
id: step?.id,
appKey: step?.appKey,
parameters: step?.parameters || {},
},
nextStep: {
id: nextStep?.id,
appKey: nextStep?.appKey,
parameters: nextStep?.parameters || {},
},
execution: {
id: execution?.id,
},
};
variable.process = async (triggerDataItem: ITriggerDataItem) => {
const jobName = `${step.appKey}-${triggerDataItem.meta.internalId}`;
const jobPayload = {
$: variable,
triggerDataItem,
};
await triggerQueue.add(jobName, jobPayload);
};
if (trigger && trigger.dedupeStrategy === 'unique') {
const lastInternalIds = await flow?.lastInternalIds();
const isAlreadyProcessed = (internalId: string) => {
return lastInternalIds?.includes(internalId);
};
variable.flow.isAlreadyProcessed = isAlreadyProcessed;
}
return variable;
};
export default globalVariable;