Skip to content

Commit

Permalink
Implement support for starting the job by id (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed May 16, 2021
1 parent 98cf5c0 commit 6088813
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Note that in order to avoid memory leaks, it is recommended to use promise chain

* `addSimpleIntervalJob(job: SimpleIntervalJob): void` - registers and starts a new job;
* `stop(): void` - stops all jobs, registered in the scheduler;
* `stopByID(id: string): void` - stops the job with a given id.
* `stopById(id: string): void` - stops the job with a given id.
* `startById(id: string): void` - starts, or restarts (if it's already running) the job with a given id.

[npm-image]: https://img.shields.io/npm/v/toad-scheduler.svg
[npm-url]: https://npmjs.org/package/toad-scheduler
Expand Down
10 changes: 9 additions & 1 deletion lib/toadScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ export class ToadScheduler {
}
}

stopByID(id: string): void {
stopById(id: string): void {
const job = this.jobRegistry[id]
if (!job) {
throw new Error(`Job with an id ${id} is not registered.`)
}
job.stop()
}

startById(id: string): void {
const job = this.jobRegistry[id]
if (!job) {
throw new Error(`Job with an id ${id} is not registered.`)
}
job.start()
}
}
99 changes: 77 additions & 22 deletions test/toadScheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,67 @@ describe('ToadScheduler', () => {
jest.useRealTimers()
})

describe('common', () => {
it('correctly stops without any jobs', () => {
const scheduler = new ToadScheduler()
scheduler.stop()
})

it('correctly stops even when some engines are not initialized', () => {
describe('stopById', () => {
it('throws an error when non-existent id is stopped', () => {
const scheduler = new ToadScheduler()

scheduler['engines'].simpleIntervalEngine = undefined
scheduler.stop()
expect(() => {
scheduler.stopById('dummy')
}).toThrow(/Job with an id dummy is not registered./)
})

it('throws an error when duplicate id is registered', () => {
it('correctly stops job by id', () => {
let counter = 0
let counter2 = 0
const scheduler = new ToadScheduler()
const task = new Task('simple task', () => {
counter++
})
const task2 = new Task('simple task2', () => {
counter2++
})
const job = new SimpleIntervalJob(
{
milliseconds: 1,
},
new NoopTask(),
task,
'job1'
)
const job2 = new SimpleIntervalJob(
{
milliseconds: 10,
},
new NoopTask(),
'job1'
task2,
'job2'
)

scheduler.addSimpleIntervalJob(job)
expect(() => {
scheduler.addSimpleIntervalJob(job2)
}).toThrow(/Job with an id job1 is already registered/)
scheduler.addSimpleIntervalJob(job2)

expect(counter).toBe(0)
expect(counter2).toBe(0)

scheduler.stopById('job2')

jest.advanceTimersByTime(2)
expect(counter).toBe(2)
expect(counter2).toBe(0)
jest.advanceTimersByTime(10)
expect(counter).toBe(12)
expect(counter2).toBe(0)

scheduler.stop()
})
})

it('throws an error when non-existent id is stopped', () => {
describe('startById', () => {
it('throws an error when non-existent id is started', () => {
const scheduler = new ToadScheduler()
expect(() => {
scheduler.stopByID('dummy')
scheduler.startById('dummy')
}).toThrow(/Job with an id dummy is not registered./)
})

it('correctly stops job by id', () => {
it('correctly starts job by id', () => {
let counter = 0
let counter2 = 0
const scheduler = new ToadScheduler()
Expand Down Expand Up @@ -91,15 +108,53 @@ describe('ToadScheduler', () => {
expect(counter).toBe(0)
expect(counter2).toBe(0)

scheduler.stopByID('job2')
scheduler.stopById('job2')
scheduler.startById('job2')

jest.advanceTimersByTime(2)
expect(counter).toBe(2)
expect(counter2).toBe(0)
jest.advanceTimersByTime(10)
expect(counter).toBe(12)
expect(counter2).toBe(0)
expect(counter2).toBe(1)

scheduler.stop()
})
})

describe('common', () => {
it('throws an error when duplicate id is registered', () => {
const scheduler = new ToadScheduler()
const job = new SimpleIntervalJob(
{
milliseconds: 1,
},
new NoopTask(),
'job1'
)
const job2 = new SimpleIntervalJob(
{
milliseconds: 10,
},
new NoopTask(),
'job1'
)

scheduler.addSimpleIntervalJob(job)
expect(() => {
scheduler.addSimpleIntervalJob(job2)
}).toThrow(/Job with an id job1 is already registered/)
})

it('correctly stops without any jobs', () => {
const scheduler = new ToadScheduler()
scheduler.stop()
})

it('correctly stops even when some engines are not initialized', () => {
const scheduler = new ToadScheduler()

scheduler['engines'].simpleIntervalEngine = undefined
scheduler.stop()
})

Expand Down

0 comments on commit 6088813

Please sign in to comment.