Skip to content

Commit 6271781

Browse files
committed
feat: add queue size to running stats
1 parent ef85fc5 commit 6271781

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/JobDbRepository.ts

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export class JobDbRepository {
5757
return this.collection.deleteMany(query);
5858
}
5959

60+
async getQueueSize(): Promise<number> {
61+
return this.collection.countDocuments({ nextRunAt: { $lt: new Date() } });
62+
}
63+
64+
/**
65+
* Internal method to unlock jobs so that they can be re-run
66+
*/
6067
async unlockJobs(jobIds: ObjectId[]) {
6168
await this.collection.updateMany({ _id: { $in: jobIds } }, { $set: { lockedAt: null } });
6269
}

src/JobProcessor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class JobProcessor {
2323

2424
async getStatus() {
2525
return {
26+
queueSize: await this.agenda.db.getQueueSize(),
2627
jobStatus: this.jobStatus,
2728
runningJobs: this.runningJobs.length,
2829
lockedJobs: this.lockedJobs.length,

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export class Agenda extends EventEmitter {
5050
private ready: Promise<unknown>;
5151

5252
getRunningStats() {
53-
return this.jobProcessor?.getStatus();
53+
if (!this.jobProcessor) {
54+
throw new Error('agenda not running!');
55+
}
56+
return this.jobProcessor.getStatus();
5457
}
5558

5659
constructor(

0 commit comments

Comments
 (0)