Repository: kibertoad/toad-scheduler (ts)

Issue:
List all running tasks
Hello  
Is there a way to list all running tasks or is it something that can be added as a feature?
It would be helpful to see all running tasks and we can do whatever with them.

Thanks in advance

Pull request:
Implement support for retrieving a list of jobs
fixes #181

Patch:
diff --git a/README.md b/README.md
index 9f1623d..815e4d6 100644
--- a/README.md
+++ b/README.md
@@ -160,7 +160,7 @@ to prevent parallel execution of jobs in clustered environment, it is highly rec
 Here is an example:
 
 ```ts
-import { randomUUID } from 'crypto'
+import { randomUUID } from 'node:crypto'
 
 import type { Redis } from 'ioredis'
 import type { LockOptions } from 'redis-semaphore'
@@ -303,6 +303,8 @@ function createTask(job: AbstractBackgroundJob): AsyncTask {
 * `stop(): void` - stops all jobs, registered in the scheduler;
 * `getById(id: string): Job` - returns the job with a given id.
 * `existsById(id: string): boolean` - returns true if job with given id exists, false otherwise.
+* `getAllJobs(): Job[]` - returns all registered jobs.
+* `getAllJobsByStatus(status: JobStatus): Job[]` - returns all registered jobs with a given status.
 * `stopById(id: string): void` - stops the job with a given id.
 * `removeById(id: string): Job | undefined` - stops the job with a given id and removes it from the scheduler. If no such job exists, returns `undefined`, otherwise returns the job.
 * `startById(id: string): void` - starts, or restarts (if it's already running) the job with a given id.
diff --git a/lib/toadScheduler.ts b/lib/toadScheduler.ts
index 654ff49..39cef3c 100644
--- a/lib/toadScheduler.ts
+++ b/lib/toadScheduler.ts
@@ -1,6 +1,6 @@
 import { SimpleIntervalEngine } from './engines/simple-interval/SimpleIntervalEngine'
 import { SimpleIntervalJob } from './engines/simple-interval/SimpleIntervalJob'
-import { Job } from './common/Job'
+import { Job, JobStatus } from './common/Job'
 import { LongIntervalJob } from './engines/simple-interval/LongIntervalJob'
 import { CronJob } from './engines/cron/CronJob'
 import { CronJobEngine } from './engines/cron/CronJobEngine'
@@ -96,4 +96,14 @@ export class ToadScheduler {
     const job = this.getById(id)
     job.start()
   }
+
+  getAllJobs(): Job[] {
+    return Object.values(this.jobRegistry)
+  }
+
+  getAllJobsByStatus(status: JobStatus): Job[] {
+    return Object.values(this.jobRegistry).filter((value) => {
+      return value.getStatus() === status
+    })
+  }
 }


Test patch:
diff --git a/test/toadScheduler.spec.ts b/test/toadScheduler.spec.ts
index 9d2ffed..74f2e7c 100644
--- a/test/toadScheduler.spec.ts
+++ b/test/toadScheduler.spec.ts
@@ -3,6 +3,8 @@ import { SimpleIntervalJob } from '../lib/engines/simple-interval/SimpleInterval
 import { Task } from '../lib/common/Task'
 import { NoopTask } from './utils/testTasks'
 import { advanceTimersByTime, mockTimers, unMockTimers } from './utils/timerUtils'
+import { JobStatus } from '../lib/common/Job'
+import { expectToMatchObject } from './utils/assertUtils'
 
 describe('ToadScheduler', () => {
   beforeEach(() => {
@@ -219,6 +221,71 @@ describe('ToadScheduler', () => {
     })
   })
 
+  describe('listJobs', () => {
+    it('returns all jobs', () => {
+      const scheduler = new ToadScheduler()
+      const task = new NoopTask()
+      const job = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id' }
+      )
+      const job2 = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id2' }
+      )
+      scheduler.addSimpleIntervalJob(job)
+      scheduler.addSimpleIntervalJob(job2)
+      job.stop()
+
+      const retrievedJobs = scheduler.getAllJobs()
+      expectToMatchObject(retrievedJobs, [
+        {
+          id: 'id',
+        },
+        {
+          id: 'id2',
+        },
+      ])
+    })
+  })
+
+  describe('listJobs', () => {
+    it('returns jobs filtered by status', () => {
+      const scheduler = new ToadScheduler()
+      const task = new NoopTask()
+      const job = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id' }
+      )
+      const job2 = new SimpleIntervalJob(
+        {
+          seconds: 20,
+        },
+        task,
+        { id: 'id2' }
+      )
+      scheduler.addSimpleIntervalJob(job)
+      scheduler.addSimpleIntervalJob(job2)
+      job.stop()
+
+      const retrievedJobs = scheduler.getAllJobsByStatus(JobStatus.STOPPED)
+      expectToMatchObject(retrievedJobs, [
+        {
+          id: 'id',
+        },
+      ])
+    })
+  })
+
   describe('common', () => {
     it('throws an error when duplicate id is registered', () => {
       const scheduler = new ToadScheduler()
diff --git a/test/utils/assertUtils.ts b/test/utils/assertUtils.ts
index ae90470..1757680 100644
--- a/test/utils/assertUtils.ts
+++ b/test/utils/assertUtils.ts
@@ -5,3 +5,14 @@ export function expectAssertions(count: number) {
     expect.assertions(count)
   }
 }
+
+export function expectToMatchObject(actual: unknown[], expected: any[]) {
+  if (isJest) {
+    expect(actual).toMatchObject(expected)
+  } else {
+    for (const element of expected) {
+      expect(actual.length).toBe(expected.length)
+      expect(actual).toContain(jasmine.objectContaining(element))
+    }
+  }
+}