Skip to content

Commit

Permalink
set bottleneck job expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Dec 12, 2023
1 parent 4e9f95e commit 08a3a28
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildTestPublishContainerDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- expiration
release:
types:
- published
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 19 additions & 6 deletions src/api/common/base.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ export class BaseController {

static smsClient: any;
static smsLimiter: Bottleneck;
static smsJobExpiration;
static emailJobExpiration;

async sendSMS(
to: string,
textBody: string,
subscription: Partial<Subscription>,
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);
}
Expand All @@ -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',
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const config: Record<string, any> = {
id: 'notifyBCEmail',
minTime: 250,
maxConcurrent: 1,

// jobExpiration corresponds to Bottleneck expiration job option
jobExpiration: 120000,

/* Redis clustering options */
// datastore: 'ioredis',
// clientOptions: {
Expand All @@ -98,6 +102,10 @@ const config: Record<string, any> = {
id: 'notifyBCSms',
minTime: 250,
maxConcurrent: 1,

// jobExpiration corresponds to Bottleneck expiration job option
jobExpiration: 120000,

/* Redis clustering options */
// datastore: 'ioredis',
// clientOptions: {
Expand Down

0 comments on commit 08a3a28

Please sign in to comment.