From d17ab8e9e571bae404b08a8d2a5468d8bb994c13 Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Tue, 31 Aug 2021 11:55:06 +0200 Subject: [PATCH] :zap: Allow webhook processes to wake up waiting executions (#2153) --- packages/cli/src/Server.ts | 85 ----------------------------- packages/cli/src/WebhookServer.ts | 88 +++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 85 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 1b424c007f..aaec3bf56d 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -118,7 +118,6 @@ import { Push, ResponseHelper, TestWebhooks, - WaitingWebhooks, WaitTracker, WaitTrackerClass, WebhookHelpers, @@ -2487,90 +2486,6 @@ class App { WebhookServer.registerProductionWebhooks.apply(this); } - // ---------------------------------------- - // Waiting Webhooks - // ---------------------------------------- - - const waitingWebhooks = new WaitingWebhooks(); - - // HEAD webhook-waiting requests - this.app.head( - `/${this.endpointWebhookWaiting}/*`, - async (req: express.Request, res: express.Response) => { - // Cut away the "/webhook-waiting/" to get the registred part of the url - const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( - this.endpointWebhookWaiting.length + 2, - ); - - let response; - try { - response = await waitingWebhooks.executeWebhook('HEAD', requestUrl, req, res); - } catch (error) { - ResponseHelper.sendErrorResponse(res, error); - return; - } - - if (response.noWebhookResponse === true) { - // Nothing else to do as the response got already sent - return; - } - - ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); - }, - ); - - // GET webhook-waiting requests - this.app.get( - `/${this.endpointWebhookWaiting}/*`, - async (req: express.Request, res: express.Response) => { - // Cut away the "/webhook-waiting/" to get the registred part of the url - const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( - this.endpointWebhookWaiting.length + 2, - ); - - let response; - try { - response = await waitingWebhooks.executeWebhook('GET', requestUrl, req, res); - } catch (error) { - ResponseHelper.sendErrorResponse(res, error); - return; - } - - if (response.noWebhookResponse === true) { - // Nothing else to do as the response got already sent - return; - } - - ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); - }, - ); - - // POST webhook-waiting requests - this.app.post( - `/${this.endpointWebhookWaiting}/*`, - async (req: express.Request, res: express.Response) => { - // Cut away the "/webhook-waiting/" to get the registred part of the url - const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( - this.endpointWebhookWaiting.length + 2, - ); - - let response; - try { - response = await waitingWebhooks.executeWebhook('POST', requestUrl, req, res); - } catch (error) { - ResponseHelper.sendErrorResponse(res, error); - return; - } - - if (response.noWebhookResponse === true) { - // Nothing else to do as the response got already sent - return; - } - - ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); - }, - ); - // HEAD webhook requests (test for UI) this.app.head( `/${this.endpointWebhookTest}/*`, diff --git a/packages/cli/src/WebhookServer.ts b/packages/cli/src/WebhookServer.ts index 95ad977a0e..4cf3afc7b4 100644 --- a/packages/cli/src/WebhookServer.ts +++ b/packages/cli/src/WebhookServer.ts @@ -27,6 +27,7 @@ import { IExternalHooksClass, IPackageVersions, ResponseHelper, + WaitingWebhooks, } from '.'; import * as config from '../config'; @@ -143,6 +144,90 @@ export function registerProductionWebhooks() { ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); }, ); + + // ---------------------------------------- + // Waiting Webhooks + // ---------------------------------------- + + const waitingWebhooks = new WaitingWebhooks(); + + // HEAD webhook-waiting requests + this.app.head( + `/${this.endpointWebhookWaiting}/*`, + async (req: express.Request, res: express.Response) => { + // Cut away the "/webhook-waiting/" to get the registred part of the url + const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( + this.endpointWebhookWaiting.length + 2, + ); + + let response; + try { + response = await waitingWebhooks.executeWebhook('HEAD', requestUrl, req, res); + } catch (error) { + ResponseHelper.sendErrorResponse(res, error); + return; + } + + if (response.noWebhookResponse === true) { + // Nothing else to do as the response got already sent + return; + } + + ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); + }, + ); + + // GET webhook-waiting requests + this.app.get( + `/${this.endpointWebhookWaiting}/*`, + async (req: express.Request, res: express.Response) => { + // Cut away the "/webhook-waiting/" to get the registred part of the url + const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( + this.endpointWebhookWaiting.length + 2, + ); + + let response; + try { + response = await waitingWebhooks.executeWebhook('GET', requestUrl, req, res); + } catch (error) { + ResponseHelper.sendErrorResponse(res, error); + return; + } + + if (response.noWebhookResponse === true) { + // Nothing else to do as the response got already sent + return; + } + + ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); + }, + ); + + // POST webhook-waiting requests + this.app.post( + `/${this.endpointWebhookWaiting}/*`, + async (req: express.Request, res: express.Response) => { + // Cut away the "/webhook-waiting/" to get the registred part of the url + const requestUrl = (req as ICustomRequest).parsedUrl!.pathname!.slice( + this.endpointWebhookWaiting.length + 2, + ); + + let response; + try { + response = await waitingWebhooks.executeWebhook('POST', requestUrl, req, res); + } catch (error) { + ResponseHelper.sendErrorResponse(res, error); + return; + } + + if (response.noWebhookResponse === true) { + // Nothing else to do as the response got already sent + return; + } + + ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode); + }, + ); } class App { @@ -152,6 +237,8 @@ class App { endpointWebhook: string; + endpointWebhookWaiting: string; + endpointPresetCredentials: string; externalHooks: IExternalHooksClass; @@ -186,6 +273,7 @@ class App { this.app = express(); this.endpointWebhook = config.get('endpoints.webhook') as string; + this.endpointWebhookWaiting = config.get('endpoints.webhookWaiting') as string; this.saveDataErrorExecution = config.get('executions.saveDataOnError') as string; this.saveDataSuccessExecution = config.get('executions.saveDataOnSuccess') as string; this.saveManualExecutions = config.get('executions.saveDataManualExecutions') as boolean;