2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
Db,
|
|
|
|
IExecutionDb,
|
|
|
|
IExecutionFlattedDb,
|
|
|
|
IPushDataExecutionFinished,
|
|
|
|
IWorkflowBase,
|
2019-08-08 11:38:25 -07:00
|
|
|
IWorkflowExecutionDataProcess,
|
2019-06-23 03:35:23 -07:00
|
|
|
Push,
|
|
|
|
ResponseHelper,
|
|
|
|
WebhookHelpers,
|
|
|
|
WorkflowHelpers,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
import {
|
|
|
|
UserSettings,
|
2019-09-19 05:14:37 -07:00
|
|
|
} from 'n8n-core';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
import {
|
2019-08-08 11:38:25 -07:00
|
|
|
IDataObject,
|
2019-06-23 03:35:23 -07:00
|
|
|
IRun,
|
|
|
|
ITaskData,
|
2019-08-08 11:38:25 -07:00
|
|
|
IWorkflowCredentials,
|
2019-06-23 03:35:23 -07:00
|
|
|
IWorkflowExecuteAdditionalData,
|
2019-08-08 11:38:25 -07:00
|
|
|
IWorkflowExecuteHooks,
|
2019-06-23 03:35:23 -07:00
|
|
|
WorkflowExecuteMode,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2019-07-21 10:47:41 -07:00
|
|
|
import * as config from '../config';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if there was an error and if errorWorkflow is defined. If so it collects
|
|
|
|
* all the data and executes it
|
|
|
|
*
|
|
|
|
* @param {IWorkflowBase} workflowData The workflow which got executed
|
|
|
|
* @param {IRun} fullRunData The run which produced the error
|
|
|
|
* @param {WorkflowExecuteMode} mode The mode in which the workflow which did error got started in
|
|
|
|
* @param {string} [executionId] The id the execution got saved as
|
|
|
|
*/
|
2019-09-11 08:48:14 -07:00
|
|
|
function executeErrorWorkflow(workflowData: IWorkflowBase, fullRunData: IRun, mode: WorkflowExecuteMode, executionId?: string, retryOf?: string): void {
|
2019-09-23 13:05:09 -07:00
|
|
|
if (mode === 'manual') {
|
|
|
|
// Do not call error workflow when executed manually
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
// Check if there was an error and if so if an errorWorkflow is set
|
|
|
|
if (fullRunData.data.resultData.error !== undefined && workflowData.settings !== undefined && workflowData.settings.errorWorkflow) {
|
|
|
|
const workflowErrorData = {
|
|
|
|
execution: {
|
|
|
|
id: executionId,
|
|
|
|
error: fullRunData.data.resultData.error,
|
|
|
|
lastNodeExecuted: fullRunData.data.resultData.lastNodeExecuted!,
|
|
|
|
mode,
|
2019-09-11 08:48:14 -07:00
|
|
|
retryOf,
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
workflow: {
|
|
|
|
id: workflowData.id !== undefined ? workflowData.id.toString() as string : undefined,
|
|
|
|
name: workflowData.name,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Run the error workflow
|
|
|
|
WorkflowHelpers.executeErrorWorkflow(workflowData.settings.errorWorkflow as string, workflowErrorData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-24 05:25:30 -07:00
|
|
|
/**
|
|
|
|
* Pushes the execution out to all connected clients
|
|
|
|
*
|
|
|
|
* @param {IRun} fullRunData The RunData of the finished execution
|
|
|
|
* @param {string} executionIdActive The id of the finished execution
|
|
|
|
* @param {string} [executionIdDb] The database id of finished execution
|
|
|
|
*/
|
2019-08-08 22:37:10 -07:00
|
|
|
export function pushExecutionFinished(fullRunData: IRun, executionIdActive: string, executionIdDb?: string, retryOf?: string) {
|
2019-07-24 05:25:30 -07:00
|
|
|
// Clone the object except the runData. That one is not supposed
|
|
|
|
// to be send. Because that data got send piece by piece after
|
|
|
|
// each node which finished executing
|
|
|
|
const pushRunData = {
|
|
|
|
...fullRunData,
|
|
|
|
data: {
|
|
|
|
...fullRunData.data,
|
|
|
|
resultData: {
|
|
|
|
...fullRunData.data.resultData,
|
|
|
|
runData: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Push data to editor-ui once workflow finished
|
|
|
|
const sendData: IPushDataExecutionFinished = {
|
|
|
|
executionIdActive,
|
|
|
|
executionIdDb,
|
|
|
|
data: pushRunData,
|
2019-08-08 22:37:10 -07:00
|
|
|
retryOf,
|
2019-07-24 05:25:30 -07:00
|
|
|
};
|
|
|
|
|
2019-08-28 06:28:47 -07:00
|
|
|
const pushInstance = Push.getInstance();
|
2019-07-24 05:25:30 -07:00
|
|
|
pushInstance.send('executionFinished', sendData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
/**
|
|
|
|
* Returns the workflow execution hooks
|
|
|
|
*
|
|
|
|
* @param {WorkflowExecuteMode} mode
|
|
|
|
* @param {IWorkflowBase} workflowData
|
|
|
|
* @param {string} executionId
|
|
|
|
* @param {string} [sessionId]
|
|
|
|
* @param {string} [retryOf]
|
|
|
|
* @returns {IWorkflowExecuteHooks}
|
|
|
|
*/
|
|
|
|
const hooks = (mode: WorkflowExecuteMode, workflowData: IWorkflowBase, executionId: string, sessionId?: string, retryOf?: string): IWorkflowExecuteHooks => {
|
2019-06-23 03:35:23 -07:00
|
|
|
return {
|
|
|
|
nodeExecuteBefore: [
|
2019-08-08 11:38:25 -07:00
|
|
|
async (nodeName: string): Promise<void> => {
|
|
|
|
// Push data to session which started workflow before each
|
|
|
|
// node which starts rendering
|
2019-06-23 03:35:23 -07:00
|
|
|
if (sessionId === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-28 06:28:47 -07:00
|
|
|
const pushInstance = Push.getInstance();
|
2019-08-08 11:38:25 -07:00
|
|
|
pushInstance.send('nodeExecuteBefore', {
|
2019-06-23 03:35:23 -07:00
|
|
|
executionId,
|
|
|
|
nodeName,
|
2019-08-08 11:38:25 -07:00
|
|
|
}, sessionId);
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
nodeExecuteAfter: [
|
2019-08-08 11:38:25 -07:00
|
|
|
async (nodeName: string, data: ITaskData): Promise<void> => {
|
|
|
|
// Push data to session which started workflow after each rendered node
|
2019-06-23 03:35:23 -07:00
|
|
|
if (sessionId === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-28 06:28:47 -07:00
|
|
|
const pushInstance = Push.getInstance();
|
2019-08-08 11:38:25 -07:00
|
|
|
pushInstance.send('nodeExecuteAfter', {
|
2019-06-23 03:35:23 -07:00
|
|
|
executionId,
|
|
|
|
nodeName,
|
|
|
|
data,
|
2019-08-08 11:38:25 -07:00
|
|
|
}, sessionId);
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
2019-07-24 05:25:30 -07:00
|
|
|
workflowExecuteBefore: [
|
2019-08-08 11:38:25 -07:00
|
|
|
async (): Promise<void> => {
|
2019-07-24 05:25:30 -07:00
|
|
|
// Push data to editor-ui once workflow finished
|
2019-08-28 06:28:47 -07:00
|
|
|
const pushInstance = Push.getInstance();
|
2019-08-08 11:38:25 -07:00
|
|
|
pushInstance.send('executionStarted', {
|
2019-07-24 05:25:30 -07:00
|
|
|
executionId,
|
|
|
|
mode,
|
|
|
|
startedAt: new Date(),
|
|
|
|
retryOf,
|
|
|
|
workflowId: workflowData.id as string,
|
|
|
|
workflowName: workflowData.name,
|
2019-08-08 11:38:25 -07:00
|
|
|
});
|
2019-07-24 05:25:30 -07:00
|
|
|
}
|
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
workflowExecuteAfter: [
|
2019-08-08 11:38:25 -07:00
|
|
|
async (fullRunData: IRun, newStaticData: IDataObject): Promise<void> => {
|
2019-06-23 03:35:23 -07:00
|
|
|
try {
|
2019-08-08 11:38:25 -07:00
|
|
|
if (WorkflowHelpers.isWorkflowIdValid(workflowData.id as string) === true) {
|
|
|
|
// Workflow is saved so update in database
|
|
|
|
try {
|
|
|
|
await WorkflowHelpers.saveStaticDataById(workflowData.id as string, newStaticData);
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: Add proper logging!
|
|
|
|
console.error(`There was a problem saving the workflow with id "${workflowData.id}" to save changed staticData: ${e.message}`);
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-07-21 10:47:41 -07:00
|
|
|
let saveManualExecutions = config.get('executions.saveDataManualExecutions') as boolean;
|
2019-08-08 11:38:25 -07:00
|
|
|
if (workflowData.settings !== undefined && workflowData.settings.saveManualExecutions !== undefined) {
|
2019-06-23 03:35:23 -07:00
|
|
|
// Apply to workflow override
|
2019-08-08 11:38:25 -07:00
|
|
|
saveManualExecutions = workflowData.settings.saveManualExecutions as boolean;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2019-07-10 09:06:26 -07:00
|
|
|
if (mode === 'manual' && saveManualExecutions === false) {
|
2019-08-08 22:37:10 -07:00
|
|
|
pushExecutionFinished(fullRunData, executionId, undefined, retryOf);
|
2019-09-11 08:48:14 -07:00
|
|
|
executeErrorWorkflow(workflowData, fullRunData, mode, undefined, retryOf);
|
2019-07-10 11:53:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check config to know if execution should be saved or not
|
2019-07-21 10:47:41 -07:00
|
|
|
let saveDataErrorExecution = config.get('executions.saveDataOnError') as string;
|
|
|
|
let saveDataSuccessExecution = config.get('executions.saveDataOnSuccess') as string;
|
2019-08-08 11:38:25 -07:00
|
|
|
if (workflowData.settings !== undefined) {
|
|
|
|
saveDataErrorExecution = (workflowData.settings.saveDataErrorExecution as string) || saveDataErrorExecution;
|
|
|
|
saveDataSuccessExecution = (workflowData.settings.saveDataSuccessExecution as string) || saveDataSuccessExecution;
|
2019-07-10 11:53:13 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-07-10 11:53:13 -07:00
|
|
|
const workflowDidSucceed = !fullRunData.data.resultData.error;
|
|
|
|
if (workflowDidSucceed === true && saveDataSuccessExecution === 'none' ||
|
|
|
|
workflowDidSucceed === false && saveDataErrorExecution === 'none'
|
|
|
|
) {
|
2019-08-08 22:37:10 -07:00
|
|
|
pushExecutionFinished(fullRunData, executionId, undefined, retryOf);
|
2019-09-11 08:48:14 -07:00
|
|
|
executeErrorWorkflow(workflowData, fullRunData, mode, undefined, retryOf);
|
2019-06-23 03:35:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fullExecutionData: IExecutionDb = {
|
|
|
|
data: fullRunData.data,
|
|
|
|
mode: fullRunData.mode,
|
|
|
|
finished: fullRunData.finished ? fullRunData.finished : false,
|
|
|
|
startedAt: fullRunData.startedAt,
|
|
|
|
stoppedAt: fullRunData.stoppedAt,
|
|
|
|
workflowData,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (retryOf !== undefined) {
|
|
|
|
fullExecutionData.retryOf = retryOf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (workflowData.id !== undefined && WorkflowHelpers.isWorkflowIdValid(workflowData.id.toString()) === true) {
|
|
|
|
fullExecutionData.workflowId = workflowData.id.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const executionData = ResponseHelper.flattenExecutionData(fullExecutionData);
|
|
|
|
|
|
|
|
// Save the Execution in DB
|
|
|
|
const executionResult = await Db.collections.Execution!.save(executionData as IExecutionFlattedDb);
|
|
|
|
|
|
|
|
if (fullRunData.finished === true && retryOf !== undefined) {
|
|
|
|
// If the retry was successful save the reference it on the original execution
|
|
|
|
// await Db.collections.Execution!.save(executionData as IExecutionFlattedDb);
|
|
|
|
await Db.collections.Execution!.update(retryOf, { retrySuccessId: executionResult.id });
|
|
|
|
}
|
|
|
|
|
2019-08-08 22:37:10 -07:00
|
|
|
pushExecutionFinished(fullRunData, executionId, executionResult.id as string, retryOf);
|
2019-09-11 08:48:14 -07:00
|
|
|
executeErrorWorkflow(workflowData, fullRunData, mode, executionResult ? executionResult.id as string : undefined, retryOf);
|
2019-06-23 03:35:23 -07:00
|
|
|
} catch (error) {
|
2019-08-08 22:37:10 -07:00
|
|
|
pushExecutionFinished(fullRunData, executionId, undefined, retryOf);
|
2019-09-11 08:48:14 -07:00
|
|
|
executeErrorWorkflow(workflowData, fullRunData, mode, undefined, retryOf);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
/**
|
|
|
|
* Returns the base additional data without webhooks
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {WorkflowExecuteMode} mode
|
|
|
|
* @param {IWorkflowCredentials} credentials
|
|
|
|
* @returns {Promise<IWorkflowExecuteAdditionalData>}
|
|
|
|
*/
|
|
|
|
export async function getBase(mode: WorkflowExecuteMode, credentials: IWorkflowCredentials): Promise<IWorkflowExecuteAdditionalData> {
|
2019-06-23 03:35:23 -07:00
|
|
|
const urlBaseWebhook = WebhookHelpers.getWebhookBaseUrl();
|
|
|
|
|
2019-07-21 10:47:41 -07:00
|
|
|
const timezone = config.get('generic.timezone') as string;
|
|
|
|
const webhookBaseUrl = urlBaseWebhook + config.get('endpoints.webhook') as string;
|
|
|
|
const webhookTestBaseUrl = urlBaseWebhook + config.get('endpoints.webhookTest') as string;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
const encryptionKey = await UserSettings.getEncryptionKey();
|
|
|
|
if (encryptionKey === undefined) {
|
|
|
|
throw new Error('No encryption key got found to decrypt the credentials!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-08-08 11:38:25 -07:00
|
|
|
credentials,
|
2019-06-23 03:35:23 -07:00
|
|
|
encryptionKey,
|
|
|
|
timezone,
|
|
|
|
webhookBaseUrl,
|
|
|
|
webhookTestBaseUrl,
|
|
|
|
};
|
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the workflow hooks
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {IWorkflowExecutionDataProcess} data
|
|
|
|
* @param {string} executionId
|
|
|
|
* @returns {IWorkflowExecuteHooks}
|
|
|
|
*/
|
|
|
|
export function getHookMethods(data: IWorkflowExecutionDataProcess, executionId: string): IWorkflowExecuteHooks {
|
|
|
|
return hooks(data.executionMode, data.workflowData, executionId, data.sessionId, data.retryOf as string | undefined);
|
|
|
|
}
|