n8n/packages/core/src/ActiveWorkflows.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

234 lines
6.1 KiB
TypeScript
Raw Normal View History

import type {
IGetExecutePollFunctions,
IGetExecuteTriggerFunctions,
INode,
2019-06-23 03:35:23 -07:00
ITriggerResponse,
IWorkflowExecuteAdditionalData,
TriggerTime,
2019-06-23 03:35:23 -07:00
Workflow,
WorkflowActivateMode,
WorkflowExecuteMode,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow';
import {
ApplicationError,
ErrorReporterProxy as ErrorReporter,
LoggerProxy as Logger,
toCronExpression,
TriggerCloseError,
WorkflowActivationError,
WorkflowDeactivationError,
} from 'n8n-workflow';
import { Service } from 'typedi';
2019-06-23 03:35:23 -07:00
import type { IWorkflowData } from './Interfaces';
import { ScheduledTaskManager } from './ScheduledTaskManager';
:sparkles: Added logging to n8n (#1381) * Added logging to n8n This commit adds logging to n8n using the Winston library. For now, this commit only allows logging to console (default behavior) or file (need to pass in config via environment variables). Other logging methods can be further implemented using hooks. These were skipped for now as it would require adding more dependencies. Logging level is notice by default, meaning no additional messages would be displayed at the moment. Logging level can be set to info or debug as well to enrich the generated logs. The ILogger interface was added to the workflow project as it would make it available for all other projects but the implementation was done on the cli project. * Lint fixes and logging level naming. Also fixed the way we use the logger as it was not working previously * Improvements to logging framework Using appropriate single quotes Improving the way the logger is declared * Improved naming for Log Types * Removed logger global variable, replacing it by a proxy * Add logging to CLI commands * Remove unused GenericHelpers * Changed back some messages to console instead of logger and added npm shortcuts for worker and webhook * Fix typos * Adding basic file rotation to logs as suggested by @mutdmour * Fixed linting issues * Correcting comment to correctly reflect space usage * Added settings for log files rotation * Correcting config type from String to Number * Changed default file settings to number To reflect previous changes to the type * Changed the way log messages are added to be called statically. Also minor naming improvements * Applying latest corrections sent by @ivov * :zap: Some logging improvements * Saving logs to a folder inside n8n home instead of root * Fixed broken tests and linting * Changed some log messages to improve formatting * Adding quotes to names on log messages * Added execution and session IDs to logs. Also removed unnecessary line breaks * :zap: Added file caller to log messages (#1657) This is done using callsites library which already existed in the project as another library's dependency. So in fact it does not add any new dependency. * Adding logs to help debug Salesforce node * :zap: Add function name to logs and add more logs * :zap: Improve some error messages * :zap: Improve some more log messages * :zap: Rename logging env variables to match others Co-authored-by: dali <servfrdali@yahoo.fr> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-05-01 20:43:01 -07:00
@Service()
2019-06-23 03:35:23 -07:00
export class ActiveWorkflows {
constructor(private readonly scheduledTaskManager: ScheduledTaskManager) {}
private activeWorkflows: { [workflowId: string]: IWorkflowData } = {};
2019-06-23 03:35:23 -07:00
/**
* Returns if the workflow is active in memory.
2019-06-23 03:35:23 -07:00
*/
isActive(workflowId: string) {
return this.activeWorkflows.hasOwnProperty(workflowId);
2019-06-23 03:35:23 -07:00
}
/**
* Returns the IDs of the currently active workflows in memory.
2019-06-23 03:35:23 -07:00
*/
allActiveWorkflows() {
return Object.keys(this.activeWorkflows);
2019-06-23 03:35:23 -07:00
}
/**
* Returns the workflow data for the given ID if currently active in memory.
2019-06-23 03:35:23 -07:00
*/
get(workflowId: string) {
return this.activeWorkflows[workflowId];
2019-06-23 03:35:23 -07:00
}
/**
* Makes a workflow active
*
* @param {string} workflowId The id of the workflow to activate
2019-06-23 03:35:23 -07:00
* @param {Workflow} workflow The workflow to activate
* @param {IWorkflowExecuteAdditionalData} additionalData The additional data which is needed to run workflows
*/
async add(
workflowId: string,
workflow: Workflow,
additionalData: IWorkflowExecuteAdditionalData,
mode: WorkflowExecuteMode,
activation: WorkflowActivateMode,
getTriggerFunctions: IGetExecuteTriggerFunctions,
getPollFunctions: IGetExecutePollFunctions,
) {
this.activeWorkflows[workflowId] = {};
2019-06-23 03:35:23 -07:00
const triggerNodes = workflow.getTriggerNodes();
let triggerResponse: ITriggerResponse | undefined;
this.activeWorkflows[workflowId].triggerResponses = [];
2019-06-23 03:35:23 -07:00
for (const triggerNode of triggerNodes) {
try {
triggerResponse = await workflow.runTrigger(
triggerNode,
getTriggerFunctions,
additionalData,
mode,
activation,
);
if (triggerResponse !== undefined) {
// If a response was given save it
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
this.activeWorkflows[workflowId].triggerResponses!.push(triggerResponse);
}
} catch (e) {
const error = e instanceof Error ? e : new Error(`${e}`);
throw new WorkflowActivationError(
`There was a problem activating the workflow: "${error.message}"`,
{ cause: error, node: triggerNode },
);
2019-06-23 03:35:23 -07:00
}
}
const pollingNodes = workflow.getPollNodes();
if (pollingNodes.length === 0) return;
for (const pollNode of pollingNodes) {
try {
await this.activatePolling(
pollNode,
workflow,
additionalData,
getPollFunctions,
mode,
activation,
);
} catch (e) {
const error = e instanceof Error ? e : new Error(`${e}`);
throw new WorkflowActivationError(
`There was a problem activating the workflow: "${error.message}"`,
{ cause: error, node: pollNode },
);
}
}
2019-06-23 03:35:23 -07:00
}
/**
* Activates polling for the given node
*/
async activatePolling(
node: INode,
workflow: Workflow,
additionalData: IWorkflowExecuteAdditionalData,
getPollFunctions: IGetExecutePollFunctions,
mode: WorkflowExecuteMode,
activation: WorkflowActivateMode,
): Promise<void> {
const pollFunctions = getPollFunctions(workflow, node, additionalData, mode, activation);
const pollTimes = pollFunctions.getNodeParameter('pollTimes') as unknown as {
item: TriggerTime[];
};
// Get all the trigger times
const cronTimes = (pollTimes.item || []).map(toCronExpression);
// The trigger function to execute when the cron-time got reached
const executeTrigger = async (testingTrigger = false) => {
Logger.debug(`Polling trigger initiated for workflow "${workflow.name}"`, {
:sparkles: Added logging to n8n (#1381) * Added logging to n8n This commit adds logging to n8n using the Winston library. For now, this commit only allows logging to console (default behavior) or file (need to pass in config via environment variables). Other logging methods can be further implemented using hooks. These were skipped for now as it would require adding more dependencies. Logging level is notice by default, meaning no additional messages would be displayed at the moment. Logging level can be set to info or debug as well to enrich the generated logs. The ILogger interface was added to the workflow project as it would make it available for all other projects but the implementation was done on the cli project. * Lint fixes and logging level naming. Also fixed the way we use the logger as it was not working previously * Improvements to logging framework Using appropriate single quotes Improving the way the logger is declared * Improved naming for Log Types * Removed logger global variable, replacing it by a proxy * Add logging to CLI commands * Remove unused GenericHelpers * Changed back some messages to console instead of logger and added npm shortcuts for worker and webhook * Fix typos * Adding basic file rotation to logs as suggested by @mutdmour * Fixed linting issues * Correcting comment to correctly reflect space usage * Added settings for log files rotation * Correcting config type from String to Number * Changed default file settings to number To reflect previous changes to the type * Changed the way log messages are added to be called statically. Also minor naming improvements * Applying latest corrections sent by @ivov * :zap: Some logging improvements * Saving logs to a folder inside n8n home instead of root * Fixed broken tests and linting * Changed some log messages to improve formatting * Adding quotes to names on log messages * Added execution and session IDs to logs. Also removed unnecessary line breaks * :zap: Added file caller to log messages (#1657) This is done using callsites library which already existed in the project as another library's dependency. So in fact it does not add any new dependency. * Adding logs to help debug Salesforce node * :zap: Add function name to logs and add more logs * :zap: Improve some error messages * :zap: Improve some more log messages * :zap: Rename logging env variables to match others Co-authored-by: dali <servfrdali@yahoo.fr> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-05-01 20:43:01 -07:00
workflowName: workflow.name,
workflowId: workflow.id,
});
try {
const pollResponse = await workflow.runPoll(node, pollFunctions);
if (pollResponse !== null) {
pollFunctions.__emit(pollResponse);
}
} catch (error) {
// If the poll function fails in the first activation
// throw the error back so we let the user know there is
// an issue with the trigger.
if (testingTrigger) {
throw error;
}
pollFunctions.__emitError(error as Error);
}
};
// Execute the trigger directly to be able to know if it works
await executeTrigger(true);
for (const cronTime of cronTimes) {
const cronTimeParts = cronTime.split(' ');
if (cronTimeParts.length > 0 && cronTimeParts[0].includes('*')) {
throw new ApplicationError(
'The polling interval is too short. It has to be at least a minute.',
);
}
this.scheduledTaskManager.registerCron(workflow, cronTime, executeTrigger);
}
}
2019-06-23 03:35:23 -07:00
/**
* Makes a workflow inactive in memory.
2019-06-23 03:35:23 -07:00
*/
async remove(workflowId: string) {
if (!this.isActive(workflowId)) {
Logger.warn(`Cannot deactivate already inactive workflow ID "${workflowId}"`);
return false;
2019-06-23 03:35:23 -07:00
}
this.scheduledTaskManager.deregisterCrons(workflowId);
2019-06-23 03:35:23 -07:00
const w = this.activeWorkflows[workflowId];
for (const r of w.triggerResponses ?? []) {
await this.closeTrigger(r, workflowId);
}
delete this.activeWorkflows[workflowId];
return true;
2019-06-23 03:35:23 -07:00
}
async removeAllTriggerAndPollerBasedWorkflows() {
for (const workflowId of Object.keys(this.activeWorkflows)) {
await this.remove(workflowId);
}
}
private async closeTrigger(response: ITriggerResponse, workflowId: string) {
if (!response.closeFunction) return;
try {
await response.closeFunction();
} catch (e) {
if (e instanceof TriggerCloseError) {
Logger.error(
`There was a problem calling "closeFunction" on "${e.node.name}" in workflow "${workflowId}"`,
);
ErrorReporter.error(e, { extra: { workflowId } });
return;
}
const error = e instanceof Error ? e : new Error(`${e}`);
throw new WorkflowDeactivationError(
`Failed to deactivate trigger of workflow ID "${workflowId}": "${error.message}"`,
{ cause: error, workflowId },
);
}
}
2019-06-23 03:35:23 -07:00
}