-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathqueueHelpers.js
More file actions
99 lines (84 loc) · 2.76 KB
/
queueHelpers.js
File metadata and controls
99 lines (84 loc) · 2.76 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
const _ = require('lodash');
/**
* Formats the number into "human readable" number/
*
* @param {Number} num The number to format.
* @returns {string} The number as a string or error text if we couldn't
* format it.
*/
function formatBytes(num) {
if (!Number.isFinite(num)) {
return 'Could not retrieve value';
}
const UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
const neg = num < 0;
if (neg) num = -num;
if (num < 1) {
return (neg ? '-' : '') + num + ' B';
}
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), UNITS.length - 1);
const numStr = Number((num / Math.pow(1024, exponent)).toPrecision(3));
const unit = UNITS[exponent];
return (neg ? '-' : '') + numStr + ' ' + unit;
}
/**
* Parses a Redis server info string as returned by `INFO`, generating an
* output object populated with fields based upon key-value pairs read.
*
* @param {string} rawServerInfo - Redis server info as returned from the
* `INFO` command.
*
* @returns {object} Returns a dictionary with key-value pairs based upon
* the parsed server info.
*/
function parseRedisServerInfo(rawServerInfo) {
// Return just an empty object if we've not been given a proper string.
if (typeof rawServerInfo !== 'string') {
return {};
}
const out = {};
const lines = rawServerInfo.split('\r\n');
for (const line of lines) {
// Skip section names and empty lines.
if (line.startsWith('#') || !line.length) {
continue;
}
// Split our line into key:value components. Note that it is possible
// for the value to contain the colon character, so we must take care
// to have all potential components joined rather than assuming there
// is only one.
const [key, ...valueComponents] = line.split(':');
const value = valueComponents.join(':');
if (value) {
out[key] = value;
}
}
return out;
}
const Helpers = {
getStats: async function(queue) {
const rawServerInfo = await queue.client.info();
const serverInfo = parseRedisServerInfo(rawServerInfo);
const stats = _.pickBy(serverInfo, (value, key) => _.includes(this._usefulMetrics, key));
stats.used_memory = formatBytes(parseInt(stats.used_memory, 10));
stats.total_system_memory = formatBytes(parseInt(stats.total_system_memory, 10));
return stats;
},
_usefulMetrics: [
'redis_version',
'total_system_memory',
'used_memory',
'mem_fragmentation_ratio',
'connected_clients',
'blocked_clients'
],
/**
* Valid states for a job in bee queue
*/
BEE_STATES: ['waiting', 'active', 'succeeded', 'failed', 'delayed'],
/**
* Valid states for a job in bull queue
*/
BULL_STATES: ['waiting', 'active', 'completed', 'failed', 'delayed']
};
module.exports = Helpers;