refactor: Restructure workers to work with services

This commit is contained in:
Faruk AYDIN
2022-10-14 22:33:26 +02:00
parent 628f872180
commit 237ab48d33
10 changed files with 81 additions and 114 deletions

View File

@@ -1,14 +1,9 @@
import { Worker } from 'bullmq';
import redisConfig from '../config/redis';
import Flow from '../models/flow';
import logger from '../helpers/logger';
import globalVariable from '../helpers/global-variable';
import { IGlobalVariable } from '@automatisch/types';
import Execution from '../models/execution';
import Processor from '../services/processor';
import ExecutionStep from '../models/execution-step';
import Step from '../models/step';
import actionQueue from '../queues/action';
import { processAction } from '../services/action';
type JobData = {
flowId: string;
@@ -19,64 +14,21 @@ type JobData = {
export const worker = new Worker(
'action',
async (job) => {
const { flowId, stepId, executionId } = job.data as JobData;
const step = await Step.query().findById(stepId).throwIfNotFound();
const execution = await Execution.query()
.findById(executionId)
.throwIfNotFound();
const $ = await globalVariable({
flow: await Flow.query().findById(flowId).throwIfNotFound(),
app: await step.getApp(),
step: step,
connection: await step.$relatedQuery('connection'),
execution: execution,
});
const priorExecutionSteps = await ExecutionStep.query().where({
execution_id: $.execution.id,
});
const computedParameters = Processor.computeParameters(
$.step.parameters,
priorExecutionSteps
const { stepId, flowId, executionId } = await processAction(
job.data as JobData
);
const actionCommand = await step.getActionCommand();
const step = await Step.query().findById(stepId).throwIfNotFound();
const nextStep = await step.getNextStep();
$.step.parameters = computedParameters;
const actionDataItem = await actionCommand.run($);
if (!nextStep) return;
await execution.$relatedQuery('executionSteps').insertAndFetch({
stepId: $.step.id,
status: 'success',
dataIn: computedParameters,
dataOut: actionDataItem.data.raw,
});
// TODO: Add until step id logic here!
// TODO: Change job name for the action data item!
const jobName = `${$.step.appKey}-sample`;
if (!$.nextStep.id) return;
const nextStep = await Step.query()
.findById($.nextStep.id)
.throwIfNotFound();
console.log('hello world');
const variable = await globalVariable({
flow: await Flow.query().findById($.flow.id),
app: await nextStep.getApp(),
step: nextStep,
connection: await nextStep.$relatedQuery('connection'),
execution: execution,
});
const jobName = `${executionId}-${nextStep.id}`;
const jobPayload = {
$: variable,
flowId,
executionId,
stepId: nextStep.id,
};
await actionQueue.add(jobName, jobPayload);