Make sure that n8n stops even if there are unforeseen errors

This commit is contained in:
Jan Oberhauser 2020-10-14 16:41:42 +02:00
parent 28074d94a4
commit df020136d3

View file

@ -70,43 +70,44 @@ export class Start extends Command {
static async stopProcess() { static async stopProcess() {
console.log(`\nStopping n8n...`); console.log(`\nStopping n8n...`);
const externalHooks = ExternalHooks();
try { try {
const externalHooks = ExternalHooks();
await externalHooks.run('n8n.stop', []); await externalHooks.run('n8n.stop', []);
} catch (error) {
console.error('There was an error in the "n8n.stop" hook.', error);
}
setTimeout(() => { setTimeout(() => {
// In case that something goes wrong with shutdown we // In case that something goes wrong with shutdown we
// kill after max. 30 seconds no matter what // kill after max. 30 seconds no matter what
process.exit(processExistCode); process.exit(processExistCode);
}, 30000); }, 30000);
const removePromises = []; const removePromises = [];
if (activeWorkflowRunner !== undefined) { if (activeWorkflowRunner !== undefined) {
removePromises.push(activeWorkflowRunner.removeAll()); removePromises.push(activeWorkflowRunner.removeAll());
}
// Remove all test webhooks
const testWebhooks = TestWebhooks.getInstance();
removePromises.push(testWebhooks.removeAll());
await Promise.all(removePromises);
// Wait for active workflow executions to finish
const activeExecutionsInstance = ActiveExecutions.getInstance();
let executingWorkflows = activeExecutionsInstance.getActiveExecutions();
let count = 0;
while (executingWorkflows.length !== 0) {
if (count++ % 4 === 0) {
console.log(`Waiting for ${executingWorkflows.length} active executions to finish...`);
} }
await new Promise((resolve) => {
setTimeout(resolve, 500); // Remove all test webhooks
}); const testWebhooks = TestWebhooks.getInstance();
executingWorkflows = activeExecutionsInstance.getActiveExecutions(); removePromises.push(testWebhooks.removeAll());
await Promise.all(removePromises);
// Wait for active workflow executions to finish
const activeExecutionsInstance = ActiveExecutions.getInstance();
let executingWorkflows = activeExecutionsInstance.getActiveExecutions();
let count = 0;
while (executingWorkflows.length !== 0) {
if (count++ % 4 === 0) {
console.log(`Waiting for ${executingWorkflows.length} active executions to finish...`);
}
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
executingWorkflows = activeExecutionsInstance.getActiveExecutions();
}
} catch (error) {
console.error('There was an error shutting down n8n.', error);
} }
process.exit(processExistCode); process.exit(processExistCode);