From 08a3a28acc5254990ce2c71b2ac60f0467115ecb Mon Sep 17 00:00:00 2001 From: f-w Date: Tue, 12 Dec 2023 11:57:55 -0800 Subject: [PATCH] set bottleneck job expiration --- .../buildTestPublishContainerDeploy.yml | 1 + package.json | 2 +- src/api/common/base.controller.ts | 25 ++++++++++++++----- src/config.ts | 8 ++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.github/workflows/buildTestPublishContainerDeploy.yml b/.github/workflows/buildTestPublishContainerDeploy.yml index 9ec36dd8a..856a43dec 100644 --- a/.github/workflows/buildTestPublishContainerDeploy.yml +++ b/.github/workflows/buildTestPublishContainerDeploy.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - expiration release: types: - published diff --git a/package.json b/package.json index ef28c96c1..82e624aea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "notify-bc", - "version": "5.0.7", + "version": "5.0.8", "dbSchemaVersion": "0.9.0", "description": "A versatile notification API server", "author": "f-w", diff --git a/src/api/common/base.controller.ts b/src/api/common/base.controller.ts index b0d60fe49..dcea33b14 100644 --- a/src/api/common/base.controller.ts +++ b/src/api/common/base.controller.ts @@ -45,6 +45,9 @@ export class BaseController { static smsClient: any; static smsLimiter: Bottleneck; + static smsJobExpiration; + static emailJobExpiration; + async sendSMS( to: string, textBody: string, @@ -52,7 +55,9 @@ export class BaseController { priority = 5, ) { if (!BaseController.smsLimiter && this.appConfig?.sms?.throttle?.enabled) { - const smsThrottleCfg = Object.assign({}, this.appConfig.sms.throttle); + let smsThrottleCfg; + ({ jobExpiration: BaseController.smsJobExpiration, ...smsThrottleCfg } = + this.appConfig.sms.throttle); delete smsThrottleCfg.enabled; BaseController.smsLimiter = new Bottleneck(smsThrottleCfg); } @@ -71,9 +76,10 @@ export class BaseController { } let req: any = fetch; if (BaseController.smsLimiter) { - req = BaseController.smsLimiter - .wrap(req) - .withOptions.bind(this, { priority }); + req = BaseController.smsLimiter.wrap(req).withOptions.bind(this, { + priority, + expiration: BaseController.smsJobExpiration, + }); } const res = await req(url, { method: 'POST', @@ -131,7 +137,11 @@ export class BaseController { !BaseController.emailLimiter && this.appConfig?.email?.throttle?.enabled ) { - const emailThrottleCfg = Object.assign({}, this.appConfig.email.throttle); + let emailThrottleCfg; + ({ + jobExpiration: BaseController.emailJobExpiration, + ...emailThrottleCfg + } = this.appConfig.email.throttle); delete emailThrottleCfg.enabled; BaseController.emailLimiter = new Bottleneck(emailThrottleCfg); } @@ -141,7 +151,10 @@ export class BaseController { if (BaseController.emailLimiter) { sendMail = BaseController.emailLimiter .wrap(sendMail) - .withOptions.bind(this.transport, { priority }); + .withOptions.bind(this.transport, { + priority, + expiration: BaseController.emailJobExpiration, + }); } info = await sendMail(mailOptions); if (info?.accepted?.length < 1) { diff --git a/src/config.ts b/src/config.ts index b5a4ed190..b57cfc4b4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -78,6 +78,10 @@ const config: Record = { id: 'notifyBCEmail', minTime: 250, maxConcurrent: 1, + + // jobExpiration corresponds to Bottleneck expiration job option + jobExpiration: 120000, + /* Redis clustering options */ // datastore: 'ioredis', // clientOptions: { @@ -98,6 +102,10 @@ const config: Record = { id: 'notifyBCSms', minTime: 250, maxConcurrent: 1, + + // jobExpiration corresponds to Bottleneck expiration job option + jobExpiration: 120000, + /* Redis clustering options */ // datastore: 'ioredis', // clientOptions: {