feat: Implement auto-run interval for triggers of completed flows

This commit is contained in:
Faruk AYDIN
2022-03-23 17:32:13 +03:00
committed by Ömer Faruk Aydın
parent 3715291df7
commit 22e1fe5c44
13 changed files with 134 additions and 37 deletions

View File

@@ -14,7 +14,7 @@ const executeFlow = async (
params: Params,
context: Context
) => {
const step = await context.currentUser
const untilStep = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('connection')
.findOne({
@@ -22,20 +22,18 @@ const executeFlow = async (
})
.throwIfNotFound();
const flow = await step.$relatedQuery('flow');
const data = await new Processor(flow, step, { testRun: true }).run();
const flow = await untilStep.$relatedQuery('flow');
// TODO: Use this snippet to execute flows with the background job.
// const data = processorQueue.add('processorJob', {
// flowId: flow.id,
// stepId: step.id,
// });
const data = await new Processor(flow, {
untilStep,
testRun: true,
}).run();
await step.$query().patch({
await untilStep.$query().patch({
status: 'completed',
});
return { data, step };
return { data, step: untilStep };
};
export default executeFlow;

View File

@@ -1,4 +1,5 @@
import Context from '../../types/express/context';
import processorQueue from '../../queues/processor';
type Params = {
input: {
@@ -7,6 +8,11 @@ type Params = {
};
};
const JOB_NAME = 'processorJob';
const REPEAT_OPTIONS = {
every: 60000, // 1 minute
};
const updateFlowStatus = async (
_parent: unknown,
params: Params,
@@ -23,10 +29,23 @@ const updateFlowStatus = async (
return flow;
}
flow = await flow.$query().patchAndFetch({
flow = await flow.$query().withGraphFetched('steps').patchAndFetch({
active: params.input.active,
});
if (flow.active) {
await processorQueue.add(
JOB_NAME,
{ flowId: flow.id },
{
repeat: REPEAT_OPTIONS,
jobId: flow.id,
}
);
} else {
await processorQueue.removeRepeatable(JOB_NAME, REPEAT_OPTIONS, flow.id);
}
return flow;
};