mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Improve typing on Queue and Jobs (#3892)
also, move all things related to `bull` into a single place.
This commit is contained in:
parent
12507d39d6
commit
f5c6c21bf4
|
@ -13,23 +13,18 @@ import http from 'http';
|
||||||
import PCancelable from 'p-cancelable';
|
import PCancelable from 'p-cancelable';
|
||||||
|
|
||||||
import { Command, flags } from '@oclif/command';
|
import { Command, flags } from '@oclif/command';
|
||||||
import { BinaryDataManager, IBinaryDataConfig, UserSettings, WorkflowExecute } from 'n8n-core';
|
import { BinaryDataManager, UserSettings, WorkflowExecute } from 'n8n-core';
|
||||||
|
|
||||||
import { IExecuteResponsePromiseData, INodeTypes, IRun, Workflow, LoggerProxy } from 'n8n-workflow';
|
import { IExecuteResponsePromiseData, INodeTypes, IRun, Workflow, LoggerProxy } from 'n8n-workflow';
|
||||||
|
|
||||||
import { FindOneOptions, getConnectionManager } from 'typeorm';
|
import { FindOneOptions, getConnectionManager } from 'typeorm';
|
||||||
|
|
||||||
import Bull from 'bull';
|
|
||||||
import {
|
import {
|
||||||
CredentialsOverwrites,
|
CredentialsOverwrites,
|
||||||
CredentialTypes,
|
CredentialTypes,
|
||||||
Db,
|
Db,
|
||||||
ExternalHooks,
|
ExternalHooks,
|
||||||
GenericHelpers,
|
GenericHelpers,
|
||||||
IBullJobData,
|
|
||||||
IBullJobResponse,
|
|
||||||
IBullWebhookResponse,
|
|
||||||
IExecutionFlattedDb,
|
|
||||||
InternalHooksManager,
|
InternalHooksManager,
|
||||||
LoadNodesAndCredentials,
|
LoadNodesAndCredentials,
|
||||||
NodeTypes,
|
NodeTypes,
|
||||||
|
@ -64,7 +59,7 @@ export class Worker extends Command {
|
||||||
[key: string]: PCancelable<IRun>;
|
[key: string]: PCancelable<IRun>;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
static jobQueue: Bull.Queue;
|
static jobQueue: Queue.JobQueue;
|
||||||
|
|
||||||
static processExistCode = 0;
|
static processExistCode = 0;
|
||||||
// static activeExecutions = ActiveExecutions.getInstance();
|
// static activeExecutions = ActiveExecutions.getInstance();
|
||||||
|
@ -118,30 +113,28 @@ export class Worker extends Command {
|
||||||
process.exit(Worker.processExistCode);
|
process.exit(Worker.processExistCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
async runJob(job: Bull.Job, nodeTypes: INodeTypes): Promise<IBullJobResponse> {
|
async runJob(job: Queue.Job, nodeTypes: INodeTypes): Promise<Queue.JobResponse> {
|
||||||
const jobData = job.data as IBullJobData;
|
const { executionId, loadStaticData } = job.data;
|
||||||
const executionDb = await Db.collections.Execution.findOne(jobData.executionId);
|
const executionDb = await Db.collections.Execution.findOne(executionId);
|
||||||
|
|
||||||
if (!executionDb) {
|
if (!executionDb) {
|
||||||
LoggerProxy.error(
|
LoggerProxy.error(
|
||||||
`Worker failed to find data of execution "${jobData.executionId}" in database. Cannot continue.`,
|
`Worker failed to find data of execution "${executionId}" in database. Cannot continue.`,
|
||||||
{
|
{ executionId },
|
||||||
executionId: jobData.executionId,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Unable to find data of execution "${jobData.executionId}" in database. Aborting execution.`,
|
`Unable to find data of execution "${executionId}" in database. Aborting execution.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const currentExecutionDb = ResponseHelper.unflattenExecutionData(executionDb);
|
const currentExecutionDb = ResponseHelper.unflattenExecutionData(executionDb);
|
||||||
LoggerProxy.info(
|
LoggerProxy.info(
|
||||||
`Start job: ${job.id} (Workflow ID: ${currentExecutionDb.workflowData.id} | Execution: ${jobData.executionId})`,
|
`Start job: ${job.id} (Workflow ID: ${currentExecutionDb.workflowData.id} | Execution: ${executionId})`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const workflowOwner = await getWorkflowOwner(currentExecutionDb.workflowData.id!.toString());
|
const workflowOwner = await getWorkflowOwner(currentExecutionDb.workflowData.id!.toString());
|
||||||
|
|
||||||
let { staticData } = currentExecutionDb.workflowData;
|
let { staticData } = currentExecutionDb.workflowData;
|
||||||
if (jobData.loadStaticData) {
|
if (loadStaticData) {
|
||||||
const findOptions = {
|
const findOptions = {
|
||||||
select: ['id', 'staticData'],
|
select: ['id', 'staticData'],
|
||||||
} as FindOneOptions;
|
} as FindOneOptions;
|
||||||
|
@ -154,7 +147,7 @@ export class Worker extends Command {
|
||||||
'Worker execution failed because workflow could not be found in database.',
|
'Worker execution failed because workflow could not be found in database.',
|
||||||
{
|
{
|
||||||
workflowId: currentExecutionDb.workflowData.id,
|
workflowId: currentExecutionDb.workflowData.id,
|
||||||
executionId: jobData.executionId,
|
executionId,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -206,14 +199,15 @@ export class Worker extends Command {
|
||||||
|
|
||||||
additionalData.hooks.hookFunctions.sendResponse = [
|
additionalData.hooks.hookFunctions.sendResponse = [
|
||||||
async (response: IExecuteResponsePromiseData): Promise<void> => {
|
async (response: IExecuteResponsePromiseData): Promise<void> => {
|
||||||
await job.progress({
|
const progress: Queue.WebhookResponse = {
|
||||||
executionId: job.data.executionId as string,
|
executionId,
|
||||||
response: WebhookHelpers.encodeWebhookResponse(response),
|
response: WebhookHelpers.encodeWebhookResponse(response),
|
||||||
} as IBullWebhookResponse);
|
};
|
||||||
|
await job.progress(progress);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
additionalData.executionId = jobData.executionId;
|
additionalData.executionId = executionId;
|
||||||
|
|
||||||
let workflowExecute: WorkflowExecute;
|
let workflowExecute: WorkflowExecute;
|
||||||
let workflowRun: PCancelable<IRun>;
|
let workflowRun: PCancelable<IRun>;
|
||||||
|
|
|
@ -46,20 +46,6 @@ export interface IActivationError {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IBullJobData {
|
|
||||||
executionId: string;
|
|
||||||
loadStaticData: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBullJobResponse {
|
|
||||||
success: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IBullWebhookResponse {
|
|
||||||
executionId: string;
|
|
||||||
response: IExecuteResponsePromiseData;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICustomRequest extends Request {
|
export interface ICustomRequest extends Request {
|
||||||
parsedUrl: Url | undefined;
|
parsedUrl: Url | undefined;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,32 @@
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
||||||
import Bull from 'bull';
|
import Bull from 'bull';
|
||||||
|
import { IExecuteResponsePromiseData } from 'n8n-workflow';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
// eslint-disable-next-line import/no-cycle
|
// eslint-disable-next-line import/no-cycle
|
||||||
import { IBullJobData, IBullWebhookResponse } from './Interfaces';
|
|
||||||
// eslint-disable-next-line import/no-cycle
|
|
||||||
import * as ActiveExecutions from './ActiveExecutions';
|
import * as ActiveExecutions from './ActiveExecutions';
|
||||||
// eslint-disable-next-line import/no-cycle
|
// eslint-disable-next-line import/no-cycle
|
||||||
import * as WebhookHelpers from './WebhookHelpers';
|
import * as WebhookHelpers from './WebhookHelpers';
|
||||||
|
|
||||||
|
export type Job = Bull.Job<JobData>;
|
||||||
|
export type JobQueue = Bull.Queue<JobData>;
|
||||||
|
|
||||||
|
export interface JobData {
|
||||||
|
executionId: string;
|
||||||
|
loadStaticData: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface JobResponse {
|
||||||
|
success: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebhookResponse {
|
||||||
|
executionId: string;
|
||||||
|
response: IExecuteResponsePromiseData;
|
||||||
|
}
|
||||||
|
|
||||||
export class Queue {
|
export class Queue {
|
||||||
private activeExecutions: ActiveExecutions.ActiveExecutions;
|
private activeExecutions: ActiveExecutions.ActiveExecutions;
|
||||||
|
|
||||||
private jobQueue: Bull.Queue;
|
private jobQueue: JobQueue;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.activeExecutions = ActiveExecutions.getInstance();
|
this.activeExecutions = ActiveExecutions.getInstance();
|
||||||
|
@ -26,7 +41,7 @@ export class Queue {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.jobQueue = new Bull('jobs', { prefix, redis: redisOptions, enableReadyCheck: false });
|
this.jobQueue = new Bull('jobs', { prefix, redis: redisOptions, enableReadyCheck: false });
|
||||||
|
|
||||||
this.jobQueue.on('global:progress', (jobId, progress: IBullWebhookResponse) => {
|
this.jobQueue.on('global:progress', (jobId, progress: WebhookResponse) => {
|
||||||
this.activeExecutions.resolveResponsePromise(
|
this.activeExecutions.resolveResponsePromise(
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||||
progress.executionId,
|
progress.executionId,
|
||||||
|
@ -35,28 +50,28 @@ export class Queue {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async add(jobData: IBullJobData, jobOptions: object): Promise<Bull.Job> {
|
async add(jobData: JobData, jobOptions: object): Promise<Job> {
|
||||||
return this.jobQueue.add(jobData, jobOptions);
|
return this.jobQueue.add(jobData, jobOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getJob(jobId: Bull.JobId): Promise<Bull.Job | null> {
|
async getJob(jobId: Bull.JobId): Promise<Job | null> {
|
||||||
return this.jobQueue.getJob(jobId);
|
return this.jobQueue.getJob(jobId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getJobs(jobTypes: Bull.JobStatus[]): Promise<Bull.Job[]> {
|
async getJobs(jobTypes: Bull.JobStatus[]): Promise<Job[]> {
|
||||||
return this.jobQueue.getJobs(jobTypes);
|
return this.jobQueue.getJobs(jobTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
getBullObjectInstance(): Bull.Queue {
|
getBullObjectInstance(): JobQueue {
|
||||||
return this.jobQueue;
|
return this.jobQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param job A Bull.Job instance
|
* @param job A Job instance
|
||||||
* @returns boolean true if we were able to securely stop the job
|
* @returns boolean true if we were able to securely stop the job
|
||||||
*/
|
*/
|
||||||
async stopJob(job: Bull.Job): Promise<boolean> {
|
async stopJob(job: Job): Promise<boolean> {
|
||||||
if (await job.isActive()) {
|
if (await job.isActive()) {
|
||||||
// Job is already running so tell it to stop
|
// Job is already running so tell it to stop
|
||||||
await job.progress(-1);
|
await job.progress(-1);
|
||||||
|
|
|
@ -31,17 +31,13 @@ import PCancelable from 'p-cancelable';
|
||||||
import { join as pathJoin } from 'path';
|
import { join as pathJoin } from 'path';
|
||||||
import { fork } from 'child_process';
|
import { fork } from 'child_process';
|
||||||
|
|
||||||
import Bull from 'bull';
|
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
// eslint-disable-next-line import/no-cycle
|
// eslint-disable-next-line import/no-cycle
|
||||||
import {
|
import {
|
||||||
ActiveExecutions,
|
ActiveExecutions,
|
||||||
CredentialsOverwrites,
|
CredentialsOverwrites,
|
||||||
CredentialTypes,
|
|
||||||
Db,
|
Db,
|
||||||
ExternalHooks,
|
ExternalHooks,
|
||||||
IBullJobData,
|
|
||||||
IBullJobResponse,
|
|
||||||
ICredentialsOverwrite,
|
ICredentialsOverwrite,
|
||||||
ICredentialsTypeData,
|
ICredentialsTypeData,
|
||||||
IExecutionFlattedDb,
|
IExecutionFlattedDb,
|
||||||
|
@ -67,7 +63,7 @@ export class WorkflowRunner {
|
||||||
|
|
||||||
push: Push.Push;
|
push: Push.Push;
|
||||||
|
|
||||||
jobQueue: Bull.Queue;
|
jobQueue: Queue.JobQueue;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.push = Push.getInstance();
|
this.push = Push.getInstance();
|
||||||
|
@ -387,7 +383,7 @@ export class WorkflowRunner {
|
||||||
this.activeExecutions.attachResponsePromise(executionId, responsePromise);
|
this.activeExecutions.attachResponsePromise(executionId, responsePromise);
|
||||||
}
|
}
|
||||||
|
|
||||||
const jobData: IBullJobData = {
|
const jobData: Queue.JobData = {
|
||||||
executionId,
|
executionId,
|
||||||
loadStaticData: !!loadStaticData,
|
loadStaticData: !!loadStaticData,
|
||||||
};
|
};
|
||||||
|
@ -404,7 +400,7 @@ export class WorkflowRunner {
|
||||||
removeOnComplete: true,
|
removeOnComplete: true,
|
||||||
removeOnFail: true,
|
removeOnFail: true,
|
||||||
};
|
};
|
||||||
let job: Bull.Job;
|
let job: Queue.Job;
|
||||||
let hooks: WorkflowHooks;
|
let hooks: WorkflowHooks;
|
||||||
try {
|
try {
|
||||||
job = await this.jobQueue.add(jobData, jobOptions);
|
job = await this.jobQueue.add(jobData, jobOptions);
|
||||||
|
@ -455,11 +451,11 @@ export class WorkflowRunner {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
const jobData: Promise<IBullJobResponse> = job.finished();
|
const jobData: Promise<Queue.JobResponse> = job.finished();
|
||||||
|
|
||||||
const queueRecoveryInterval = config.getEnv('queue.bull.queueRecoveryInterval');
|
const queueRecoveryInterval = config.getEnv('queue.bull.queueRecoveryInterval');
|
||||||
|
|
||||||
const racingPromises: Array<Promise<IBullJobResponse | object>> = [jobData];
|
const racingPromises: Array<Promise<Queue.JobResponse | object>> = [jobData];
|
||||||
|
|
||||||
let clearWatchdogInterval;
|
let clearWatchdogInterval;
|
||||||
if (queueRecoveryInterval > 0) {
|
if (queueRecoveryInterval > 0) {
|
||||||
|
|
Loading…
Reference in a new issue