Skip to content

Commit

Permalink
feat: add queue size to running stats
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Oct 12, 2020
1 parent ef85fc5 commit 6271781
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/JobDbRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export class JobDbRepository {
return this.collection.deleteMany(query);
}

async getQueueSize(): Promise<number> {
return this.collection.countDocuments({ nextRunAt: { $lt: new Date() } });
}

/**
* Internal method to unlock jobs so that they can be re-run
*/
async unlockJobs(jobIds: ObjectId[]) {
await this.collection.updateMany({ _id: { $in: jobIds } }, { $set: { lockedAt: null } });
}
Expand Down
1 change: 1 addition & 0 deletions src/JobProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class JobProcessor {

async getStatus() {
return {
queueSize: await this.agenda.db.getQueueSize(),
jobStatus: this.jobStatus,
runningJobs: this.runningJobs.length,
lockedJobs: this.lockedJobs.length,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export class Agenda extends EventEmitter {
private ready: Promise<unknown>;

getRunningStats() {
return this.jobProcessor?.getStatus();
if (!this.jobProcessor) {
throw new Error('agenda not running!');
}
return this.jobProcessor.getStatus();
}

constructor(
Expand Down

0 comments on commit 6271781

Please sign in to comment.