mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
* Unify execution ID across executions * Fix indentation and improved comments * WIP: saving data after each node execution * Added on/off to save data after each step, saving initial data and retries working * Fixing lint issues * Fixing more lint issues * ✨ Add bull to execute workflows * 👕 Fix lint issue * ⚡ Add graceful shutdown to worker * ⚡ Add loading staticData to worker * 👕 Fix lint issue * ⚡ Fix import * Changed tables metadata to add nullable to stoppedAt * Reload database on migration run * Fixed reloading database schema for sqlite by reconnecting and fixing postgres migration * Added checks to Redis and exiting process if connection is unavailable * Fixing error with new installations * Fix issue with data not being sent back to browser on manual executions with defined destination * Merging bull and unify execution id branch fixes * Main process will now get execution success from database instead of redis * Omit execution duration if execution did not stop * Fix issue with execution list displaying inconsistant information information while a workflow is running * Remove unused hooks to clarify for developers that these wont run in queue mode * Added active pooling to help recover from Redis crashes * Lint issues * Changing default polling interval to 60 seconds * Removed unnecessary attributes from bull job * Added webhooks service and setting to disable webhooks from main process * Fixed executions list when running with queues. Now we get the list of actively running workflows from bull. * Add option to disable deregistration of webhooks on shutdown * Rename WEBHOOK_TUNNEL_URL to WEBHOOK_URL keeping backwards compat. * Added auto refresh to executions list * Improvements to workflow stop process when running with queues * Refactor queue system to use a singleton and avoid code duplication * Improve comments and remove unnecessary commits * Remove console.log from vue file * Blocking webhook process to run without queues * Handling execution stop graciously when possible * Removing initialization of all workflows from webhook process * Refactoring code to remove code duplication for job stop * Improved execution list to be more fluid and less intrusive * Fixing workflow name for current executions when auto updating * ⚡ Right align autorefresh checkbox Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
196 lines
4.9 KiB
TypeScript
196 lines
4.9 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import { parse, stringify } from 'flatted';
|
|
|
|
import {
|
|
IExecutionDb,
|
|
IExecutionFlatted,
|
|
IExecutionFlattedDb,
|
|
IExecutionResponse,
|
|
IWorkflowDb,
|
|
} from './';
|
|
|
|
/**
|
|
* Special Error which allows to return also an error code and http status code
|
|
*
|
|
* @export
|
|
* @class ResponseError
|
|
* @extends {Error}
|
|
*/
|
|
export class ResponseError extends Error {
|
|
|
|
// The HTTP status code of response
|
|
httpStatusCode?: number;
|
|
|
|
// The error code in the resonse
|
|
errorCode?: number;
|
|
|
|
/**
|
|
* Creates an instance of ResponseError.
|
|
* @param {string} message The error message
|
|
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
|
|
* @param {number} [httpStatusCode] The HTTP status code the response should have
|
|
* @memberof ResponseError
|
|
*/
|
|
constructor(message: string, errorCode?: number, httpStatusCode?: number) {
|
|
super(message);
|
|
this.name = 'ResponseError';
|
|
|
|
if (errorCode) {
|
|
this.errorCode = errorCode;
|
|
}
|
|
if (httpStatusCode) {
|
|
this.httpStatusCode = httpStatusCode;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function basicAuthAuthorizationError(resp: Response, realm: string, message?: string) {
|
|
resp.statusCode = 401;
|
|
resp.setHeader('WWW-Authenticate', `Basic realm="${realm}"`);
|
|
resp.json({code: resp.statusCode, message});
|
|
}
|
|
|
|
export function jwtAuthAuthorizationError(resp: Response, message?: string) {
|
|
resp.statusCode = 403;
|
|
resp.json({code: resp.statusCode, message});
|
|
}
|
|
|
|
|
|
export function sendSuccessResponse(res: Response, data: any, raw?: boolean, responseCode?: number) { // tslint:disable-line:no-any
|
|
if (responseCode !== undefined) {
|
|
res.status(responseCode);
|
|
}
|
|
|
|
if (raw === true) {
|
|
if (typeof data === 'string') {
|
|
res.send(data);
|
|
} else {
|
|
res.json(data);
|
|
}
|
|
} else {
|
|
res.json({
|
|
data,
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
export function sendErrorResponse(res: Response, error: ResponseError) {
|
|
let httpStatusCode = 500;
|
|
if (error.httpStatusCode) {
|
|
httpStatusCode = error.httpStatusCode;
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
console.error('ERROR RESPONSE');
|
|
console.error(error);
|
|
}
|
|
|
|
const response = {
|
|
code: 0,
|
|
message: 'Unknown error',
|
|
};
|
|
|
|
if (error.errorCode) {
|
|
response.code = error.errorCode;
|
|
}
|
|
if (error.message) {
|
|
response.message = error.message;
|
|
}
|
|
if (error.stack && process.env.NODE_ENV !== 'production') {
|
|
// @ts-ignore
|
|
response.stack = error.stack;
|
|
}
|
|
|
|
res.status(httpStatusCode).json(response);
|
|
}
|
|
|
|
|
|
/**
|
|
* A helper function which does not just allow to return Promises it also makes sure that
|
|
* all the responses have the same format
|
|
*
|
|
*
|
|
* @export
|
|
* @param {(req: Request, res: Response) => Promise<any>} processFunction The actual function to process the request
|
|
* @returns
|
|
*/
|
|
|
|
export function send(processFunction: (req: Request, res: Response) => Promise<any>) { // tslint:disable-line:no-any
|
|
|
|
return async (req: Request, res: Response) => {
|
|
try {
|
|
const data = await processFunction(req, res);
|
|
|
|
// Success response
|
|
sendSuccessResponse(res, data);
|
|
} catch (error) {
|
|
// Error response
|
|
sendErrorResponse(res, error);
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* Flattens the Execution data.
|
|
* As it contains a lot of references which normally would be saved as duplicate data
|
|
* with regular JSON.stringify it gets flattened which keeps the references in place.
|
|
*
|
|
* @export
|
|
* @param {IExecutionDb} fullExecutionData The data to flatten
|
|
* @returns {IExecutionFlatted}
|
|
*/
|
|
export function flattenExecutionData(fullExecutionData: IExecutionDb): IExecutionFlatted {
|
|
// Flatten the data
|
|
const returnData: IExecutionFlatted = Object.assign({}, {
|
|
data: stringify(fullExecutionData.data),
|
|
mode: fullExecutionData.mode,
|
|
startedAt: fullExecutionData.startedAt,
|
|
stoppedAt: fullExecutionData.stoppedAt,
|
|
finished: fullExecutionData.finished ? fullExecutionData.finished : false,
|
|
workflowId: fullExecutionData.workflowId,
|
|
workflowData: fullExecutionData.workflowData!,
|
|
});
|
|
|
|
if (fullExecutionData.id !== undefined) {
|
|
returnData.id = fullExecutionData.id!.toString();
|
|
}
|
|
|
|
if (fullExecutionData.retryOf !== undefined) {
|
|
returnData.retryOf = fullExecutionData.retryOf!.toString();
|
|
}
|
|
|
|
if (fullExecutionData.retrySuccessId !== undefined) {
|
|
returnData.retrySuccessId = fullExecutionData.retrySuccessId!.toString();
|
|
}
|
|
|
|
return returnData;
|
|
}
|
|
|
|
|
|
/**
|
|
* Unflattens the Execution data.
|
|
*
|
|
* @export
|
|
* @param {IExecutionFlattedDb} fullExecutionData The data to unflatten
|
|
* @returns {IExecutionResponse}
|
|
*/
|
|
export function unflattenExecutionData(fullExecutionData: IExecutionFlattedDb): IExecutionResponse {
|
|
|
|
const returnData: IExecutionResponse = Object.assign({}, {
|
|
id: fullExecutionData.id.toString(),
|
|
workflowData: fullExecutionData.workflowData as IWorkflowDb,
|
|
data: parse(fullExecutionData.data),
|
|
mode: fullExecutionData.mode,
|
|
startedAt: fullExecutionData.startedAt,
|
|
stoppedAt: fullExecutionData.stoppedAt,
|
|
finished: fullExecutionData.finished ? fullExecutionData.finished : false,
|
|
workflowId: fullExecutionData.workflowId,
|
|
});
|
|
|
|
return returnData;
|
|
}
|