refactor(core): Couple of refactors on WorkflowRunner and ActiveExecutions (no-changelog) (#8487)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-02-06 18:09:50 +01:00
committed by GitHub
parent dc068ce2e6
commit c04f92f7fd
6 changed files with 66 additions and 143 deletions

View File

@@ -4,7 +4,7 @@ import { Flags } from '@oclif/core';
import fs from 'fs';
import os from 'os';
import type { IRun, ITaskData } from 'n8n-workflow';
import { ApplicationError, jsonParse, sleep } from 'n8n-workflow';
import { ApplicationError, jsonParse } from 'n8n-workflow';
import { sep } from 'path';
import { diff } from 'json-diff';
import pick from 'lodash/pick';
@@ -118,28 +118,9 @@ export class ExecuteBatch extends BaseCommand {
}
ExecuteBatch.cancelled = true;
const activeExecutionsInstance = Container.get(ActiveExecutions);
const stopPromises = activeExecutionsInstance
.getActiveExecutions()
.map(async (execution) => await activeExecutionsInstance.stopExecution(execution.id));
await Promise.allSettled(stopPromises);
await Container.get(ActiveExecutions).shutdown(true);
setTimeout(() => process.exit(0), 30000);
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...`);
executingWorkflows.map((execution) => {
console.log(` - Execution ID ${execution.id}, workflow ID: ${execution.workflowId}`);
});
}
await sleep(500);
executingWorkflows = activeExecutionsInstance.getActiveExecutions();
}
// We may receive true but when called from `process.on`
// we get the signal (SIGINT, etc.)
if (skipExit !== true) {

View File

@@ -8,7 +8,7 @@ import { createReadStream, createWriteStream, existsSync } from 'fs';
import { pipeline } from 'stream/promises';
import replaceStream from 'replacestream';
import glob from 'fast-glob';
import { sleep, jsonParse } from 'n8n-workflow';
import { jsonParse } from 'n8n-workflow';
import config from '@/config';
import { ActiveExecutions } from '@/ActiveExecutions';
@@ -106,23 +106,7 @@ export class Start extends BaseCommand {
await Container.get(InternalHooks).onN8nStop();
// Wait for active workflow executions to finish
const activeExecutionsInstance = Container.get(ActiveExecutions);
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...`);
executingWorkflows.map((execution) => {
console.log(` - Execution ID ${execution.id}, workflow ID: ${execution.workflowId}`);
});
}
await sleep(500);
executingWorkflows = activeExecutionsInstance.getActiveExecutions();
}
await Container.get(ActiveExecutions).shutdown();
// Finally shut down Event Bus
await Container.get(MessageEventBus).close();

View File

@@ -1,6 +1,6 @@
import { Container } from 'typedi';
import { Flags, type Config } from '@oclif/core';
import { ApplicationError, sleep } from 'n8n-workflow';
import { ApplicationError } from 'n8n-workflow';
import config from '@/config';
import { ActiveExecutions } from '@/ActiveExecutions';
@@ -42,21 +42,7 @@ export class Webhook extends BaseCommand {
try {
await this.externalHooks?.run('n8n.stop', []);
// Wait for active workflow executions to finish
const activeExecutionsInstance = Container.get(ActiveExecutions);
let executingWorkflows = activeExecutionsInstance.getActiveExecutions();
let count = 0;
while (executingWorkflows.length !== 0) {
if (count++ % 4 === 0) {
this.logger.info(
`Waiting for ${executingWorkflows.length} active executions to finish...`,
);
}
await sleep(500);
executingWorkflows = activeExecutionsInstance.getActiveExecutions();
}
await Container.get(ActiveExecutions).shutdown();
} catch (error) {
await this.exitWithCrash('There was an error shutting down n8n.', error);
}