Repository: kibertoad/toad-scheduler
Pull request #15 (2021-05-16 21:00:47)

Implement support for starting the job by id


Patch:
diff --git a/README.md b/README.md
index c0b0698..d8c77c7 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/lib/toadScheduler.ts b/lib/toadScheduler.ts
index 54c1731..da73409 100644
--- a/lib/toadScheduler.ts
+++ b/lib/toadScheduler.ts
@@ -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()
+  }
 }


Test patch:
diff --git a/test/toadScheduler.spec.ts b/test/toadScheduler.spec.ts
index cc0390d..2da5699 100644
--- a/test/toadScheduler.spec.ts
+++ b/test/toadScheduler.spec.ts
@@ -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()
@@ -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()
     })