Skip to content

Commit

Permalink
bug fixing for clearRedisDatastore
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Jul 26, 2021
1 parent 9569420 commit acfaa41
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
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": "4.1.0",
"version": "4.1.1",
"dbSchemaVersion": "0.8.0",
"description": "A versatile notification API server",
"keywords": [
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/acceptance/cron.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {ApplicationConfig, CoreBindings} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {Client, expect, SinonSpy} from '@loopback/testlab';
import {fail} from 'assert';
import _ from 'lodash';
import sinon from 'sinon';
import {NotifyBcApplication} from '../..';
import {BaseController} from '../../controllers/base.controller';
Expand Down Expand Up @@ -642,14 +644,26 @@ describe('CRON reDispatchBroadcastPushNotifications', function () {
describe('CRON clearRedisDatastore', function () {
let ready: sinon.SinonStub;
let disconnect: sinon.SinonStub;
let origConfig: ApplicationConfig;
beforeEach(async function () {
origConfig = await app.get(CoreBindings.APPLICATION_CONFIG);
app.bind(CoreBindings.APPLICATION_CONFIG).to(
_.merge({}, origConfig, {
email: {throttle: {enabled: true, datastore: 'ioredis'}},
sms: {throttle: {enabled: true, datastore: 'ioredis'}},
}),
);
ready = sinon.stub().resolves(null);
disconnect = sinon.stub().resolves(null);
sinon.stub(cronTasks, 'Bottleneck').returns({
ready,
disconnect,
});
});
afterEach(async function () {
app.bind(CoreBindings.APPLICATION_CONFIG).to(origConfig);
});

it('should not clear datastore when there is sending notification', async function () {
await Promise.all([
notificationRepository.create({
Expand Down
18 changes: 12 additions & 6 deletions src/observers/cron-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,18 @@ module.exports.clearRedisDatastore = (app: Application) => {
'repositories.NotificationRepository',
);
for (const channel of ['sms', 'email']) {
const throttleConfig = <Bottleneck.ConstructorOptions>(
await app.getConfig(
CoreBindings.APPLICATION_INSTANCE,
channel + '.throttle',
)
);
if (
!throttleConfig.enabled ||
throttleConfig.clearDatastore === true ||
throttleConfig.datastore !== 'ioredis'
)
continue;
const sendingNotification = await notificationRepository.findOne(
{
where: {
Expand All @@ -590,12 +602,6 @@ module.exports.clearRedisDatastore = (app: Application) => {
undefined,
);
if (sendingNotification) continue;
const throttleConfig = <Bottleneck.ConstructorOptions>(
await app.getConfig(
CoreBindings.APPLICATION_INSTANCE,
channel + '.throttle',
)
);
const newThrottleConfig = Object.assign({}, throttleConfig, {
clearDatastore: true,
});
Expand Down
6 changes: 4 additions & 2 deletions src/observers/cron.observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ export class CronObserver implements LifeCycleObserver {
(await this.app.getConfig(CoreBindings.APPLICATION_INSTANCE)) ?? {};

if (
(appConfig.sms?.throttle?.clearDatastore !== true &&
(appConfig.sms?.throttle?.enabled &&
appConfig.sms?.throttle?.clearDatastore !== true &&
appConfig.sms?.throttle?.datastore !== 'local') ||
(appConfig.email?.throttle?.clearDatastore !== true &&
(appConfig.email?.throttle?.enabled &&
appConfig.email?.throttle?.clearDatastore !== true &&
appConfig.email?.throttle?.datastore !== 'local')
) {
const clearRedisDatastore = cronConfig.clearRedisDatastore || {};
Expand Down

0 comments on commit acfaa41

Please sign in to comment.