2024-10-15 05:55:13 -07:00
|
|
|
import { Flags } from '@oclif/core';
|
2024-02-06 09:09:50 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { Container } from 'typedi';
|
2024-01-22 09:25:36 -08:00
|
|
|
|
2024-08-22 02:10:37 -07:00
|
|
|
import { ActiveExecutions } from '@/active-executions';
|
2024-09-12 09:07:18 -07:00
|
|
|
import config from '@/config';
|
2024-10-04 00:28:53 -07:00
|
|
|
import { PubSubHandler } from '@/scaling/pubsub/pubsub-handler';
|
|
|
|
import { Subscriber } from '@/scaling/pubsub/subscriber.service';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { OrchestrationWebhookService } from '@/services/orchestration/webhook/orchestration.webhook.service';
|
2024-08-22 02:10:37 -07:00
|
|
|
import { WebhookServer } from '@/webhooks/webhook-server';
|
2024-01-22 09:25:36 -08:00
|
|
|
|
2024-09-12 09:07:18 -07:00
|
|
|
import { BaseCommand } from './base-command';
|
2021-02-09 14:32:40 -08:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
export class Webhook extends BaseCommand {
|
2021-02-09 14:32:40 -08:00
|
|
|
static description = 'Starts n8n webhook process. Intercepts only production URLs.';
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
static examples = ['$ n8n webhook'];
|
2021-02-09 14:32:40 -08:00
|
|
|
|
|
|
|
static flags = {
|
2024-01-22 09:25:36 -08:00
|
|
|
help: Flags.help({ char: 'h' }),
|
2021-02-09 14:32:40 -08:00
|
|
|
};
|
|
|
|
|
2023-12-22 02:39:58 -08:00
|
|
|
protected server = Container.get(WebhookServer);
|
2023-05-10 01:27:04 -07:00
|
|
|
|
2024-08-05 02:52:06 -07:00
|
|
|
override needsCommunityPackages = true;
|
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
/**
|
2022-11-08 08:06:00 -08:00
|
|
|
* Stops n8n in a graceful way.
|
2021-02-09 14:32:40 -08:00
|
|
|
* Make for example sure that all the webhooks from third party services
|
|
|
|
* get removed.
|
|
|
|
*/
|
2023-02-10 05:59:20 -08:00
|
|
|
async stopProcess() {
|
2023-10-25 07:35:22 -07:00
|
|
|
this.logger.info('\nStopping n8n...');
|
2021-02-09 14:32:40 -08:00
|
|
|
|
|
|
|
try {
|
2023-12-17 23:23:10 -08:00
|
|
|
await this.externalHooks?.run('n8n.stop', []);
|
2021-02-09 14:32:40 -08:00
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
await Container.get(ActiveExecutions).shutdown();
|
2021-02-09 14:32:40 -08:00
|
|
|
} catch (error) {
|
2023-02-10 05:59:20 -08:00
|
|
|
await this.exitWithCrash('There was an error shutting down n8n.', error);
|
2021-02-09 14:32:40 -08:00
|
|
|
}
|
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
await this.exitSuccessFully();
|
2021-02-09 14:32:40 -08:00
|
|
|
}
|
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async init() {
|
2023-01-04 02:38:48 -08:00
|
|
|
if (config.getEnv('executions.mode') !== 'queue') {
|
|
|
|
/**
|
|
|
|
* It is technically possible to run without queues but
|
|
|
|
* there are 2 known bugs when running in this mode:
|
|
|
|
* - Executions list will be problematic as the main process
|
|
|
|
* is not aware of current executions in the webhook processes
|
|
|
|
* and therefore will display all current executions as error
|
|
|
|
* as it is unable to determine if it is still running or crashed
|
|
|
|
* - You cannot stop currently executing jobs from webhook processes
|
|
|
|
* when running without queues as the main process cannot talk to
|
|
|
|
* the webhook processes to communicate workflow execution interruption.
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.error('Webhook processes can only run with execution mode as queue.');
|
|
|
|
}
|
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
await this.initCrashJournal();
|
2023-10-05 04:37:25 -07:00
|
|
|
this.logger.debug('Crash journal initialized');
|
2023-09-26 04:58:06 -07:00
|
|
|
|
2024-10-15 05:55:13 -07:00
|
|
|
this.logger.info('Starting n8n webhook process...');
|
|
|
|
this.logger.debug(`Host ID: ${this.instanceSettings.hostId}`);
|
2023-09-26 04:58:06 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
await super.init();
|
2021-02-09 14:32:40 -08:00
|
|
|
|
2023-09-26 04:58:06 -07:00
|
|
|
await this.initLicense();
|
2023-10-05 04:37:25 -07:00
|
|
|
this.logger.debug('License init complete');
|
2023-10-06 04:58:11 -07:00
|
|
|
await this.initOrchestration();
|
|
|
|
this.logger.debug('Orchestration init complete');
|
2023-09-22 08:22:12 -07:00
|
|
|
await this.initBinaryDataService();
|
2023-10-05 04:37:25 -07:00
|
|
|
this.logger.debug('Binary data service init complete');
|
2024-10-10 08:12:05 -07:00
|
|
|
await this.initDataDeduplicationService();
|
|
|
|
this.logger.debug('Data deduplication service init complete');
|
2023-02-10 05:59:20 -08:00
|
|
|
await this.initExternalHooks();
|
2023-10-05 04:37:25 -07:00
|
|
|
this.logger.debug('External hooks init complete');
|
2023-08-25 01:33:46 -07:00
|
|
|
await this.initExternalSecrets();
|
2024-08-07 01:23:44 -07:00
|
|
|
this.logger.debug('External secrets init complete');
|
2023-02-10 05:59:20 -08:00
|
|
|
}
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async run() {
|
2024-02-05 09:06:54 -08:00
|
|
|
if (config.getEnv('multiMainSetup.enabled')) {
|
|
|
|
throw new ApplicationError(
|
|
|
|
'Webhook process cannot be started when multi-main setup is enabled.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-08 08:35:17 -07:00
|
|
|
const { ScalingService } = await import('@/scaling/scaling.service');
|
2024-08-07 04:50:46 -07:00
|
|
|
await Container.get(ScalingService).setupQueue();
|
2023-05-10 01:27:04 -07:00
|
|
|
await this.server.start();
|
2023-02-10 05:59:20 -08:00
|
|
|
this.logger.info('Webhook listener waiting for requests.');
|
2023-02-11 07:46:10 -08:00
|
|
|
|
|
|
|
// Make sure that the process does not close
|
|
|
|
await new Promise(() => {});
|
2023-02-10 05:59:20 -08:00
|
|
|
}
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async catch(error: Error) {
|
|
|
|
await this.exitWithCrash('Exiting due to an error.', error);
|
2021-02-09 14:32:40 -08:00
|
|
|
}
|
2023-10-06 04:58:11 -07:00
|
|
|
|
|
|
|
async initOrchestration() {
|
|
|
|
await Container.get(OrchestrationWebhookService).init();
|
2024-10-04 00:28:53 -07:00
|
|
|
|
2024-10-07 07:19:58 -07:00
|
|
|
Container.get(PubSubHandler).init();
|
2024-10-11 01:31:33 -07:00
|
|
|
await Container.get(Subscriber).subscribe('n8n.commands');
|
2023-10-06 04:58:11 -07:00
|
|
|
}
|
2021-02-09 14:32:40 -08:00
|
|
|
}
|