feat: skip processing tasks over task quota

This commit is contained in:
Ali BARIN
2023-03-08 20:24:59 +00:00
parent 92d1ed65ff
commit 54e68f6252
5 changed files with 40 additions and 6 deletions

View File

@@ -14,6 +14,11 @@ export default async (request: IRequest, response: Response) => {
.throwIfNotFound(); .throwIfNotFound();
const testRun = !flow.active; const testRun = !flow.active;
if (!testRun) {
await flow.throwIfQuotaExceeded();
}
const triggerStep = await flow.getTriggerStep(); const triggerStep = await flow.getTriggerStep();
const triggerCommand = await triggerStep.getTriggerCommand(); const triggerCommand = await triggerStep.getTriggerCommand();
const app = await triggerStep.getApp(); const app = await triggerStep.getApp();

View File

@@ -1,5 +1,6 @@
import { ValidationError } from 'objection'; import { ValidationError } from 'objection';
import type { ModelOptions, QueryContext } from 'objection'; import type { ModelOptions, QueryContext } from 'objection';
import appConfig from '../config/app';
import ExtendedQueryBuilder from './query-builder'; import ExtendedQueryBuilder from './query-builder';
import Base from './base'; import Base from './base';
import Step from './step'; import Step from './step';
@@ -129,6 +130,21 @@ class Flow extends Base {
type: 'trigger', type: 'trigger',
}); });
} }
async throwIfQuotaExceeded() {
if (!appConfig.isCloud) return;
const user = await this.$relatedQuery('user');
const usageData = await user.$relatedQuery('usageData');
const hasExceeded = await usageData.checkIfLimitExceeded();
if (hasExceeded) {
throw new Error('The allowed task quota has been exhausted!');
}
return this;
}
} }
export default Flow; export default Flow;

View File

@@ -21,7 +21,7 @@ const exposeError = (handler: RequestHandler) => async (req: IRequest, res: Resp
try { try {
await handler(req, res, next); await handler(req, res, next);
} catch (err) { } catch (err) {
res.send(err); res.status(422).send(err);
} }
} }

View File

@@ -1,3 +1,4 @@
import appConfig from '../config/app';
import Step from '../models/step'; import Step from '../models/step';
import Flow from '../models/flow'; import Flow from '../models/flow';
import Execution from '../models/execution'; import Execution from '../models/execution';
@@ -17,17 +18,23 @@ type ProcessActionOptions = {
export const processAction = async (options: ProcessActionOptions) => { export const processAction = async (options: ProcessActionOptions) => {
const { flowId, stepId, executionId } = options; const { flowId, stepId, executionId } = options;
const step = await Step.query().findById(stepId).throwIfNotFound(); const flow = await Flow.query().findById(flowId).throwIfNotFound();
const execution = await Execution.query() const execution = await Execution.query()
.findById(executionId) .findById(executionId)
.throwIfNotFound(); .throwIfNotFound();
if (!execution.testRun) {
await flow.throwIfQuotaExceeded();
}
const step = await Step.query().findById(stepId).throwIfNotFound();
const $ = await globalVariable({ const $ = await globalVariable({
flow: await Flow.query().findById(flowId).throwIfNotFound(), flow,
app: await step.getApp(), app: await step.getApp(),
step: step, step: step,
connection: await step.$relatedQuery('connection'), connection: await step.$relatedQuery('connection'),
execution: execution, execution,
}); });
const priorExecutionSteps = await ExecutionStep.query().where({ const priorExecutionSteps = await ExecutionStep.query().where({

View File

@@ -1,3 +1,4 @@
import appConfig from '../config/app';
import Flow from '../models/flow'; import Flow from '../models/flow';
import globalVariable from '../helpers/global-variable'; import globalVariable from '../helpers/global-variable';
import EarlyExitError from '../errors/early-exit'; import EarlyExitError from '../errors/early-exit';
@@ -10,7 +11,12 @@ type ProcessFlowOptions = {
}; };
export const processFlow = async (options: ProcessFlowOptions) => { export const processFlow = async (options: ProcessFlowOptions) => {
const flow = await Flow.query().findById(options.flowId).throwIfNotFound(); const { testRun, flowId } = options;
const flow = await Flow.query().findById(flowId).throwIfNotFound();
if (!testRun) {
await flow.throwIfQuotaExceeded();
}
const triggerStep = await flow.getTriggerStep(); const triggerStep = await flow.getTriggerStep();
const triggerCommand = await triggerStep.getTriggerCommand(); const triggerCommand = await triggerStep.getTriggerCommand();
@@ -20,7 +26,7 @@ export const processFlow = async (options: ProcessFlowOptions) => {
connection: await triggerStep.$relatedQuery('connection'), connection: await triggerStep.$relatedQuery('connection'),
app: await triggerStep.getApp(), app: await triggerStep.getApp(),
step: triggerStep, step: triggerStep,
testRun: options.testRun, testRun,
}); });
try { try {