-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueues.js
More file actions
112 lines (92 loc) · 3.05 KB
/
Copy pathqueues.js
File metadata and controls
112 lines (92 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Queue from 'bull';
// Reads Bull queue job counts from Redis for the dashboard. The browser can't
// reach Redis, so admin/server.js (and the Vite dev middleware) expose this over
// HTTP at /api/queues. Connection details come from the same REDIS_URL the
// queue workers use; with no REDIS_URL (e.g. local dev) it returns [] so the
// dashboard renders an empty queue state instead of erroring.
// How long to wait on a single queue before reporting it as unreachable, so a
// down Redis can't hang the HTTP request.
const QUEUE_TIMEOUT_MS = 3000;
// Cache the Bull queue instances (each holds a Redis connection) keyed by the
// resolved config, rebuilt only if REDIS_URL / QUEUE_NAMES change.
let queueCache = null;
const resolveConfig = function resolveConfig () {
return {
names: ( process.env.QUEUE_NAMES || 'posts,users' )
.split( ',' )
.map( ( name ) => {
return name.trim();
} )
.filter( Boolean ),
url: process.env.REDIS_URL || '',
};
};
const getQueues = function getQueues () {
const { names, url } = resolveConfig();
if ( !url ) {
return [];
}
const cacheKey = `${ url }|${ names.join( ',' ) }`;
if ( queueCache && queueCache.key === cacheKey ) {
return queueCache.queues;
}
const queues = names.map( ( name ) => {
const queue = new Queue( name, url );
// Surface connection problems per-request (below) rather than as
// unhandled error events / log spam.
queue.on( 'error', () => {} );
return {
name: name,
queue: queue,
};
} );
queueCache = {
key: cacheKey,
queues: queues,
};
return queues;
};
const withTimeout = function withTimeout ( promise, milliseconds ) {
return Promise.race( [
promise,
new Promise( ( resolve ) => {
setTimeout( () => {
resolve( null );
}, milliseconds );
} ),
] );
};
// The raw Bull queue instances, sharing the cached Redis connections above so
// Bull Board (admin/bullBoard.js) doesn't open a second set of clients. Empty
// when no REDIS_URL is configured.
export const getQueueInstances = function getQueueInstances () {
return getQueues().map( ( { queue } ) => {
return queue;
} );
};
export const getQueueCounts = async function getQueueCounts () {
const queues = getQueues();
if ( queues.length === 0 ) {
return [];
}
return Promise.all( queues.map( async ( { name, queue } ) => {
try {
const counts = await withTimeout( queue.getJobCounts(), QUEUE_TIMEOUT_MS );
if ( !counts ) {
return {
error: 'timeout',
name: name,
};
}
return {
counts: counts,
name: name,
};
} catch ( countError ) {
return {
error: countError.message,
name: name,
};
}
} ) );
};