Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reopen] Add API for checking the task is existing without throwing error #437

Merged
merged 2 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/scheduler.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ export class SchedulerRegistry {
private readonly timeouts = new Map<string, any>();
private readonly intervals = new Map<string, any>();

doesExists(type: 'cron' | 'timeout' | 'interval', name: string) {
switch (type) {
case 'cron':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi :)
What about using enum for type? Maybe it will be easier to support it.
Cause if someone will add new type, or rename existing, it'll be additional work to change in all files instead of one string in enum?

Copy link
Contributor Author

@amokio amokio Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here is unnecessary to use enum. Since the type of the task/job is already limited. There may only have interval, timeout, and cronjob.

Anyway, thanks for the code review. If the creator really want to use enum, I will update it and push it again

return this.cronJobs.has(name);
case 'interval':
return this.intervals.has(name);
case 'timeout':
return this.timeouts.has(name);
default:
return false;
}
}

getCronJob(name: string) {
const ref = this.cronJobs.get(name);
if (!ref) {
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/cron-jobs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ describe('Cron', () => {
expect(clock.countTimers()).toBe(0);
});

it('should return true for dynamic cron job', async () => {
const service: CronService = app.get(CronService);
await app.init();

service.addCronJob();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic cron job', async () => {
const service: CronService = app.get(CronService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/interval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('Interval', () => {
expect(jest.getTimerCount()).toBe(0);
});

it('should return true for dynamic interval', async () => {
const service: IntervalService = app.get(IntervalService);
await app.init();

service.addInterval();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic interval', async () => {
const service: IntervalService = app.get(IntervalService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('Timeout', () => {
expect(jest.getTimerCount()).toBe(0);
});

it('should return true for dynamic timeout', async () => {
const service: TimeoutService = app.get(TimeoutService);
await app.init();

service.addTimeout();
expect(service.doesExists('dynamic')).toEqual(true);
});

it('should return false for dynamic timeout', async () => {
const service: TimeoutService = app.get(TimeoutService);
await app.init();
expect(service.doesExists('dynamic')).toEqual(false);
});

afterEach(async () => {
await app.close();
});
Expand Down
4 changes: 4 additions & 0 deletions tests/src/cron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ export class CronService {
this.schedulerRegistry.addCronJob('dynamic', job);
return job;
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('cron', name);
}
}
4 changes: 4 additions & 0 deletions tests/src/interval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class IntervalService {
(intervalRef as unknown) as number,
);
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('interval', name);
}
}
4 changes: 4 additions & 0 deletions tests/src/timeout.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ export class TimeoutService {
(timeoutRef as unknown) as number,
);
}

doesExists(name: string): boolean {
return this.schedulerRegistry.doesExists('timeout', name);
}
}