Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,13 @@ class Manager extends EventEmitter {

const { rows } = await this.db.executeSql(sql)

return rows.at(0) || null
return rows.at(0) || {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this result should be merged into the result of getQueueCache(name) so it can overwrite cached values found. This would also remove the need of introducing a new type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timgit I prepared af4583f , would you need it ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already included this post-merge. Thanks

name,
deferredCount: 0,
queuedCount: 0,
activeCount: 0,
totalCount: 0
}
}

async getJobById (name, id, options = {}) {
Expand Down
60 changes: 60 additions & 0 deletions test/queueTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('node:assert')
const helper = require('./testHelper')
const { states } = require('../src')
const { delay } = require('../src/tools')
const { randomUUID } = require('node:crypto')

describe('queues', function () {
it('should create a queue', async function () {
Expand Down Expand Up @@ -620,4 +621,63 @@ describe('queues', function () {
const [job3] = await boss.fetch(queue, { includeMetadata: true })
assert.strictEqual(job3.singletonKey, 'a')
})

describe('stats', function () {
let boss
const queue1 = randomUUID()
const queue2 = randomUUID()

before(async function () {
boss = this.test.boss = await helper.start(this.test.bossConfig)

await boss.createQueue(queue1)
await boss.createQueue(queue2)

await boss.send(queue1)
await boss.send(queue1)
await boss.send(queue2)
await boss.send(queue2)
})

it('should get accurate stats', async function () {
const queueData = await boss.getQueueStats(queue1)
assert.notEqual(queueData, undefined)

const {
name,
deferredCount,
queuedCount,
activeCount,
totalCount
} = queueData

assert.equal(name, queue1)
assert.equal(deferredCount, 0)
assert.equal(queuedCount, 2)
assert.equal(activeCount, 0)
assert.equal(totalCount, 2)
})

it('should get accurate stats on an empty queue', async function () {
const queue3 = randomUUID()
await boss.createQueue(queue3)

const queueData = await boss.getQueueStats(queue3)
assert.notEqual(queueData, undefined)

const {
name,
deferredCount,
queuedCount,
activeCount,
totalCount
} = queueData

assert.equal(name, queue3)
assert.equal(deferredCount, 0)
assert.equal(queuedCount, 0)
assert.equal(activeCount, 0)
assert.equal(totalCount, 0)
})
})
})
10 changes: 9 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ declare namespace PgBoss {
updatedOn: Date;
}

type QueueStatsResult = {
name: string,
deferredCount: number,
queuedCount: number,
activeCount: number,
totalCount: number
}

type ScheduleOptions = SendOptions & { tz?: string, key?: string }

interface JobPollingOptions {
Expand Down Expand Up @@ -302,7 +310,7 @@ declare class PgBoss extends EventEmitter {
deleteQueue(name: string): Promise<void>;
getQueues(): Promise<PgBoss.QueueResult[]>;
getQueue(name: string): Promise<PgBoss.QueueResult | null>;
getQueueStats(name: string): Promise<number>;
getQueueStats(name: string): Promise<PgBoss.QueueStatsResult>;

supervise(name?: string): Promise<void>;
isInstalled(): Promise<Boolean>;
Expand Down