Skip to content

Commit

Permalink
Merge branch 'bugfix/shutdown-cleanup' of https://github.com/Nosfisti…
Browse files Browse the repository at this point in the history
…s/schedule into Nosfistis-bugfix/shutdown-cleanup
  • Loading branch information
kamilmysliwiec committed Sep 23, 2020
2 parents ef8614e + df49d6c commit 0ef089f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/scheduler.orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SchedulerOrchestrator

mountIntervals() {
const intervalKeys = Object.keys(this.intervals);
intervalKeys.forEach(key => {
intervalKeys.forEach((key) => {
const options = this.intervals[key];
const intervalRef = setInterval(options.target, options.timeout);

Expand All @@ -54,7 +54,7 @@ export class SchedulerOrchestrator

mountTimeouts() {
const timeoutKeys = Object.keys(this.timeouts);
timeoutKeys.forEach(key => {
timeoutKeys.forEach((key) => {
const options = this.timeouts[key];
const timeoutRef = setTimeout(options.target, options.timeout);

Expand All @@ -65,7 +65,7 @@ export class SchedulerOrchestrator

mountCron() {
const cronKeys = Object.keys(this.cronJobs);
cronKeys.forEach(key => {
cronKeys.forEach((key) => {
const { options, target } = this.cronJobs[key];
const cronJob = new CronJob(
options.cronTime,
Expand All @@ -86,18 +86,21 @@ export class SchedulerOrchestrator
}

clearTimeouts() {
const keys = Object.keys(this.timeouts);
keys.forEach(key => clearTimeout(this.timeouts[key].ref));
Array.from(this.schedulerRegistry.getTimeouts()).forEach((key) =>
this.schedulerRegistry.deleteTimeout(key),
);
}

clearIntervals() {
const keys = Object.keys(this.intervals);
keys.forEach(key => clearInterval(this.intervals[key].ref));
Array.from(this.schedulerRegistry.getIntervals()).forEach((key) =>
this.schedulerRegistry.deleteInterval(key),
);
}

closeCronJobs() {
const keys = Object.keys(this.cronJobs);
keys.forEach(key => this.cronJobs[key].ref!.stop());
Array.from(this.schedulerRegistry.getCronJobs().keys()).forEach((key) =>
this.schedulerRegistry.deleteCronJob(key),
);
}

addTimeout(methodRef: Function, timeout: number, name: string = v4()) {
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/cron-jobs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ describe('Cron', () => {
expect(instance).toBeDefined();
});

it('should clean up dynamic cron jobs on application shutdown', async () => {
const service = app.get(CronService);
await app.init();
service.addCronJob();

const registry = app.get(SchedulerRegistry);

await app.close();

expect(registry.getCronJobs().size).toBe(0);
expect(clock.countTimers()).toBe(0);
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/interval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ describe('Interval', () => {
expect(instance).toBeDefined();
});

it('should clean up dynamic intervals on application shutdown', async () => {
const service = app.get(IntervalService);
await app.init();
service.addInterval();

const registry = app.get(SchedulerRegistry);

await app.close();

expect(registry.getIntervals().length).toBe(0);
expect(jest.getTimerCount()).toBe(0);
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ describe('Timeout', () => {
expect(instance).toBeDefined();
});

it('should clean up dynamic timeouts on application shutdown', async () => {
const service = app.get(TimeoutService);
await app.init();
service.addTimeout();

const registry = app.get(SchedulerRegistry);

await app.close();

expect(registry.getTimeouts().length).toBe(0);
expect(jest.getTimerCount()).toBe(0);
});

afterEach(async () => {
await app.close();
});
Expand Down

0 comments on commit 0ef089f

Please sign in to comment.