mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
c972f3dd50
* 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 * ⚡ 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 * ⚡ 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 * ⚡ Add function name to logs and add more logs * ⚡ Improve some error messages * ⚡ Improve some more log messages * ⚡ Rename logging env variables to match others Co-authored-by: dali <servfrdali@yahoo.fr> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
240 lines
6.7 KiB
TypeScript
240 lines
6.7 KiB
TypeScript
import { CronJob } from 'cron';
|
|
|
|
import {
|
|
IGetExecutePollFunctions,
|
|
IGetExecuteTriggerFunctions,
|
|
INode,
|
|
IPollResponse,
|
|
ITriggerResponse,
|
|
IWorkflowExecuteAdditionalData,
|
|
LoggerProxy as Logger,
|
|
Workflow,
|
|
WorkflowActivateMode,
|
|
WorkflowExecuteMode,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
ITriggerTime,
|
|
IWorkflowData,
|
|
} from './';
|
|
|
|
|
|
export class ActiveWorkflows {
|
|
private workflowData: {
|
|
[key: string]: IWorkflowData;
|
|
} = {};
|
|
|
|
|
|
/**
|
|
* Returns if the workflow is active
|
|
*
|
|
* @param {string} id The id of the workflow to check
|
|
* @returns {boolean}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
isActive(id: string): boolean {
|
|
return this.workflowData.hasOwnProperty(id);
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns the ids of the currently active workflows
|
|
*
|
|
* @returns {string[]}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
allActiveWorkflows(): string[] {
|
|
return Object.keys(this.workflowData);
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns the Workflow data for the workflow with
|
|
* the given id if it is currently active
|
|
*
|
|
* @param {string} id
|
|
* @returns {(WorkflowData | undefined)}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
get(id: string): IWorkflowData | undefined {
|
|
return this.workflowData[id];
|
|
}
|
|
|
|
|
|
/**
|
|
* Makes a workflow active
|
|
*
|
|
* @param {string} id The id of the workflow to activate
|
|
* @param {Workflow} workflow The workflow to activate
|
|
* @param {IWorkflowExecuteAdditionalData} additionalData The additional data which is needed to run workflows
|
|
* @returns {Promise<void>}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
async add(id: string, workflow: Workflow, additionalData: IWorkflowExecuteAdditionalData, mode: WorkflowExecuteMode, activation: WorkflowActivateMode, getTriggerFunctions: IGetExecuteTriggerFunctions, getPollFunctions: IGetExecutePollFunctions): Promise<void> {
|
|
this.workflowData[id] = {};
|
|
const triggerNodes = workflow.getTriggerNodes();
|
|
|
|
let triggerResponse: ITriggerResponse | undefined;
|
|
this.workflowData[id].triggerResponses = [];
|
|
for (const triggerNode of triggerNodes) {
|
|
triggerResponse = await workflow.runTrigger(triggerNode, getTriggerFunctions, additionalData, mode, activation);
|
|
if (triggerResponse !== undefined) {
|
|
// If a response was given save it
|
|
this.workflowData[id].triggerResponses!.push(triggerResponse);
|
|
}
|
|
}
|
|
|
|
const pollNodes = workflow.getPollNodes();
|
|
if (pollNodes.length) {
|
|
this.workflowData[id].pollResponses = [];
|
|
for (const pollNode of pollNodes) {
|
|
this.workflowData[id].pollResponses!.push(await this.activatePolling(pollNode, workflow, additionalData, getPollFunctions, mode, activation));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Activates polling for the given node
|
|
*
|
|
* @param {INode} node
|
|
* @param {Workflow} workflow
|
|
* @param {IWorkflowExecuteAdditionalData} additionalData
|
|
* @param {IGetExecutePollFunctions} getPollFunctions
|
|
* @returns {Promise<IPollResponse>}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
async activatePolling(node: INode, workflow: Workflow, additionalData: IWorkflowExecuteAdditionalData, getPollFunctions: IGetExecutePollFunctions, mode: WorkflowExecuteMode, activation: WorkflowActivateMode): Promise<IPollResponse> {
|
|
const pollFunctions = getPollFunctions(workflow, node, additionalData, mode, activation);
|
|
|
|
const pollTimes = pollFunctions.getNodeParameter('pollTimes') as unknown as {
|
|
item: ITriggerTime[];
|
|
};
|
|
|
|
// Define the order the cron-time-parameter appear
|
|
const parameterOrder = [
|
|
'second', // 0 - 59
|
|
'minute', // 0 - 59
|
|
'hour', // 0 - 23
|
|
'dayOfMonth', // 1 - 31
|
|
'month', // 0 - 11(Jan - Dec)
|
|
'weekday', // 0 - 6(Sun - Sat)
|
|
];
|
|
|
|
// Get all the trigger times
|
|
const cronTimes: string[] = [];
|
|
let cronTime: string[];
|
|
let parameterName: string;
|
|
if (pollTimes.item !== undefined) {
|
|
for (const item of pollTimes.item) {
|
|
cronTime = [];
|
|
if (item.mode === 'custom') {
|
|
cronTimes.push((item.cronExpression as string).trim());
|
|
continue;
|
|
}
|
|
if (item.mode === 'everyMinute') {
|
|
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} * * * * *`);
|
|
continue;
|
|
}
|
|
if (item.mode === 'everyX') {
|
|
if (item.unit === 'minutes') {
|
|
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} */${item.value} * * * *`);
|
|
} else if (item.unit === 'hours') {
|
|
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} 0 */${item.value} * * *`);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
for (parameterName of parameterOrder) {
|
|
if (item[parameterName] !== undefined) {
|
|
// Value is set so use it
|
|
cronTime.push(item[parameterName] as string);
|
|
} else if (parameterName === 'second') {
|
|
// For seconds we use by default a random one to make sure to
|
|
// balance the load a little bit over time
|
|
cronTime.push(Math.floor(Math.random() * 60).toString());
|
|
} else {
|
|
// For all others set "any"
|
|
cronTime.push('*');
|
|
}
|
|
}
|
|
|
|
cronTimes.push(cronTime.join(' '));
|
|
}
|
|
}
|
|
|
|
// The trigger function to execute when the cron-time got reached
|
|
const executeTrigger = async () => {
|
|
Logger.info(`Polling trigger initiated for workflow "${workflow.name}"`, {workflowName: workflow.name, workflowId: workflow.id});
|
|
const pollResponse = await workflow.runPoll(node, pollFunctions);
|
|
|
|
if (pollResponse !== null) {
|
|
pollFunctions.__emit(pollResponse);
|
|
}
|
|
};
|
|
|
|
// Execute the trigger directly to be able to know if it works
|
|
await executeTrigger();
|
|
|
|
const timezone = pollFunctions.getTimezone();
|
|
|
|
// Start the cron-jobs
|
|
const cronJobs: CronJob[] = [];
|
|
for (const cronTime of cronTimes) {
|
|
const cronTimeParts = cronTime.split(' ');
|
|
if (cronTimeParts.length > 0 && cronTimeParts[0].includes('*')) {
|
|
throw new Error('The polling interval is too short. It has to be at least a minute!');
|
|
}
|
|
|
|
cronJobs.push(new CronJob(cronTime, executeTrigger, undefined, true, timezone));
|
|
}
|
|
|
|
// Stop the cron-jobs
|
|
async function closeFunction() {
|
|
for (const cronJob of cronJobs) {
|
|
cronJob.stop();
|
|
}
|
|
}
|
|
|
|
return {
|
|
closeFunction,
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* Makes a workflow inactive
|
|
*
|
|
* @param {string} id The id of the workflow to deactivate
|
|
* @returns {Promise<void>}
|
|
* @memberof ActiveWorkflows
|
|
*/
|
|
async remove(id: string): Promise<void> {
|
|
if (!this.isActive(id)) {
|
|
// Workflow is currently not registered
|
|
throw new Error(`The workflow with the id "${id}" is currently not active and can so not be removed`);
|
|
}
|
|
|
|
const workflowData = this.workflowData[id];
|
|
|
|
if (workflowData.triggerResponses) {
|
|
for (const triggerResponse of workflowData.triggerResponses) {
|
|
if (triggerResponse.closeFunction) {
|
|
await triggerResponse.closeFunction();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (workflowData.pollResponses) {
|
|
for (const pollResponse of workflowData.pollResponses) {
|
|
if (pollResponse.closeFunction) {
|
|
await pollResponse.closeFunction();
|
|
}
|
|
}
|
|
}
|
|
|
|
delete this.workflowData[id];
|
|
}
|
|
|
|
}
|