Repository: kibertoad/toad-scheduler (ts)

Issue:
How can I retrieve the TaskId or JobId in the handler?
I'd like to know what task or job is being executed in the handler.  Is that possible?

Pull request description:
Feature to return the current TaskId into the Tasks handler
Closes #179 

Patch:
diff --git a/lib/common/AsyncTask.ts b/lib/common/AsyncTask.ts
index 9543097..82476f5 100644
--- a/lib/common/AsyncTask.ts
+++ b/lib/common/AsyncTask.ts
@@ -4,19 +4,23 @@ import { isPromise } from './Utils'
 export class AsyncTask {
   public isExecuting: boolean
   private readonly id: string
-  private readonly handler: () => Promise<unknown>
+  private readonly handler: (taskId?: string, jobId?: string) => Promise<unknown>
   private readonly errorHandler: (err: Error) => void | Promise<void>
 
-  constructor(id: string, handler: () => Promise<unknown>, errorHandler?: (err: Error) => void) {
+  constructor(
+    id: string,
+    handler: (taskId?: string, jobId?: string) => Promise<unknown>,
+    errorHandler?: (err: Error) => void
+  ) {
     this.id = id
     this.handler = handler
     this.errorHandler = errorHandler || defaultErrorHandler(this.id)
     this.isExecuting = false
   }
 
-  execute(): void {
+  execute(jobId?: string): void {
     this.isExecuting = true
-    this.handler()
+    this.handler(this.id, jobId)
       .catch((err: Error) => {
         const errorHandleResult = this.errorHandler(err)
         if (isPromise(errorHandleResult)) {
diff --git a/lib/common/Task.ts b/lib/common/Task.ts
index e850feb..f82a44f 100644
--- a/lib/common/Task.ts
+++ b/lib/common/Task.ts
@@ -4,20 +4,24 @@ import { isPromise } from './Utils'
 export class Task {
   public isExecuting: boolean
   private readonly id: string
-  private readonly handler: () => void
+  private readonly handler: (taskId?: string, jobId?: string) => void
   private readonly errorHandler: (err: Error) => void | Promise<void>
 
-  constructor(id: string, handler: () => void, errorHandler?: (err: Error) => void) {
+  constructor(
+    id: string,
+    handler: (taskId?: string, jobId?: string) => void,
+    errorHandler?: (err: Error) => void
+  ) {
     this.id = id
     this.handler = handler
     this.errorHandler = errorHandler || defaultErrorHandler(this.id)
     this.isExecuting = false
   }
 
-  execute(): void {
+  execute(jobId?: string): void {
     this.isExecuting = true
     try {
-      this.handler()
+      this.handler(this.id, jobId)
     } catch (err: any) {
       const errorHandleResult = this.errorHandler(err)
       if (isPromise(errorHandleResult)) {
diff --git a/lib/engines/cron/CronJob.ts b/lib/engines/cron/CronJob.ts
index 74da3da..5e9f83d 100644
--- a/lib/engines/cron/CronJob.ts
+++ b/lib/engines/cron/CronJob.ts
@@ -59,7 +59,7 @@ export class CronJob extends Job {
       },
       () => {
         if (!this.task.isExecuting || !this.preventOverrun) {
-          this.task.execute()
+          this.task.execute(this.id)
         }
       }
     )
diff --git a/lib/engines/simple-interval/LongIntervalJob.ts b/lib/engines/simple-interval/LongIntervalJob.ts
index 5c2b485..3381356 100644
--- a/lib/engines/simple-interval/LongIntervalJob.ts
+++ b/lib/engines/simple-interval/LongIntervalJob.ts
@@ -54,7 +54,7 @@ export class LongIntervalJob extends Job {
           },
           new Task('Final mile task', () => {
             this.setTimeEatingJob(toMsecs(this.schedule))
-            return this.task.execute()
+            return this.task.execute(this.id)
           })
         )
         this.childJob.start()
@@ -83,12 +83,12 @@ export class LongIntervalJob extends Job {
     }
 
     if (this.schedule.runImmediately) {
-      this.task.execute()
+      this.task.execute(this.id)
     }
 
     this.timer = setInterval(() => {
       if (!this.task.isExecuting || !this.preventOverrun) {
-        this.task.execute()
+        this.task.execute(this.id)
       }
     }, time)
   }
diff --git a/lib/engines/simple-interval/SimpleIntervalJob.ts b/lib/engines/simple-interval/SimpleIntervalJob.ts
index 98e6f42..b14169a 100644
--- a/lib/engines/simple-interval/SimpleIntervalJob.ts
+++ b/lib/engines/simple-interval/SimpleIntervalJob.ts
@@ -37,12 +37,12 @@ export class SimpleIntervalJob extends Job {
     }
 
     if (this.schedule.runImmediately) {
-      this.task.execute()
+      this.task.execute(this.id)
     }
 
     this.timer = setInterval(() => {
       if (!this.task.isExecuting || !this.preventOverrun) {
-        this.task.execute()
+        this.task.execute(this.id)
       }
     }, time)
   }


Test patch:
diff --git a/test/AsyncTask.spec.ts b/test/AsyncTask.spec.ts
index d1dd1c8..46a7546 100644
--- a/test/AsyncTask.spec.ts
+++ b/test/AsyncTask.spec.ts
@@ -156,5 +156,68 @@ describe('ToadScheduler', () => {
         done()
       })
     })
+
+    it('correctly provide taskid', (done) => {
+      unMockTimers()
+      expectAssertions(1)
+
+      const scheduler = new ToadScheduler()
+      const task = new AsyncTask(
+        'async task',
+        (taskId) => {
+          expect(taskId).toBe('async task')
+          return Promise.resolve().then(() => {
+            return Promise.reject(new Error('kaboom2'))
+          })
+        },
+        () => {}
+      )
+      const job = new SimpleIntervalJob(
+        {
+          milliseconds: 5,
+        },
+        task
+      )
+
+      scheduler.addSimpleIntervalJob(job)
+
+      sleep(10).then(() => {
+        scheduler.stop()
+        done()
+      })
+    })
+
+    it('correctly provide taskid and jobid', (done) => {
+      unMockTimers()
+      expectAssertions(2)
+
+      const scheduler = new ToadScheduler()
+      const task = new AsyncTask(
+        'async task',
+        (taskId, jobId) => {
+          expect(taskId).toBe('async task')
+          expect(jobId).toBe('jobId')
+          Promise.resolve()
+          return Promise.reject(new Error('kaboom2'))
+        },
+        () => {}
+      )
+      const job = new SimpleIntervalJob(
+        {
+          milliseconds: 5,
+        },
+        task,
+        {
+          id: 'jobId',
+        }
+      )
+
+      scheduler.addSimpleIntervalJob(job)
+
+      sleep(10).then(() => {
+        scheduler.stop()
+        done()
+      })
+    })
   })
 })
diff --git a/test/Task.spec.ts b/test/Task.spec.ts
index 151a6b3..58ee9c3 100644
--- a/test/Task.spec.ts
+++ b/test/Task.spec.ts
@@ -78,5 +78,41 @@ describe('ToadScheduler', () => {
         done()
       })
     })
+
+    it('correctly provide taskid', (done) => {
+      unMockTimers()
+      expectAssertions(1)
+
+      const scheduler = new ToadScheduler()
+      const task = new Task(
+        'task',
+        (taskId) => {
+          expect(taskId).toBe('task')
+        },
+        () => {
+          return Promise.resolve()
+            .then(() => {
+              return 'dummy'
+            })
+            .then(() => {
+              throw new Error('Error while handling an error')
+            })
+        }
+      )
+      const job = new SimpleIntervalJob(
+        {
+          seconds: 1,
+          runImmediately: true,
+        },
+        task
+      )
+
+      scheduler.addSimpleIntervalJob(job)
+
+      sleep(5).then(() => {
+        scheduler.stop()
+        done()
+      })
+    })
   })
 })