@drod915/q9s 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +192 -3
- package/dist/cli.js +3089 -0
- package/dist/index.d.ts +294 -0
- package/dist/index.js +3137 -0
- package/package.json +62 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,3137 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var ReadOnlyError = class extends Error {
|
|
3
|
+
code = "Q9S_READ_ONLY";
|
|
4
|
+
constructor(action) {
|
|
5
|
+
super(
|
|
6
|
+
`Refused write action "${action}" in read-only mode. Re-run with --allow-write to permit mutations.`
|
|
7
|
+
);
|
|
8
|
+
this.name = "ReadOnlyError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/clients/resp.ts
|
|
13
|
+
import net from "node:net";
|
|
14
|
+
import tls from "node:tls";
|
|
15
|
+
|
|
16
|
+
// src/config/contexts.ts
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import os from "node:os";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
var VALID_BACKENDS = /* @__PURE__ */ new Set(["bullmq", "river", "pg-boss"]);
|
|
21
|
+
var SENSITIVE_QUERY_KEYS = /* @__PURE__ */ new Set([
|
|
22
|
+
"password",
|
|
23
|
+
"pass",
|
|
24
|
+
"passwd",
|
|
25
|
+
"pwd",
|
|
26
|
+
"token",
|
|
27
|
+
"secret",
|
|
28
|
+
"apikey",
|
|
29
|
+
"api_key",
|
|
30
|
+
"auth"
|
|
31
|
+
]);
|
|
32
|
+
function redactUrl(rawUrl) {
|
|
33
|
+
try {
|
|
34
|
+
const parsed = new URL(rawUrl);
|
|
35
|
+
const hasRedactedPassword = parsed.password.length > 0;
|
|
36
|
+
const searchParams = new URLSearchParams(parsed.search);
|
|
37
|
+
let modifiedQuery = false;
|
|
38
|
+
for (const [key] of searchParams) {
|
|
39
|
+
if (SENSITIVE_QUERY_KEYS.has(key.toLowerCase())) {
|
|
40
|
+
searchParams.set(key, "***");
|
|
41
|
+
modifiedQuery = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const protocol = parsed.protocol;
|
|
45
|
+
const authPart = (() => {
|
|
46
|
+
if (parsed.username && hasRedactedPassword) {
|
|
47
|
+
return `${parsed.username}:***@`;
|
|
48
|
+
}
|
|
49
|
+
if (hasRedactedPassword) {
|
|
50
|
+
return `:***@`;
|
|
51
|
+
}
|
|
52
|
+
if (parsed.username) {
|
|
53
|
+
return `${parsed.username}@`;
|
|
54
|
+
}
|
|
55
|
+
return "";
|
|
56
|
+
})();
|
|
57
|
+
const hostPart = parsed.port ? `${parsed.hostname}:${parsed.port}` : parsed.hostname;
|
|
58
|
+
const queryStr = modifiedQuery ? `?${searchParams.toString()}` : parsed.search;
|
|
59
|
+
return `${protocol}//${authPart}${hostPart}${parsed.pathname}${queryStr}`;
|
|
60
|
+
} catch {
|
|
61
|
+
let redacted = rawUrl.replace(/(:\/\/[^:@/?#\s]*:)([^/?#]+)(@)/g, "$1***$3");
|
|
62
|
+
redacted = redacted.replace(
|
|
63
|
+
/([?&](?:password|pass|passwd|pwd|token|secret|apikey|api_key|auth)=)([^&#\s]*)/gi,
|
|
64
|
+
"$1***"
|
|
65
|
+
);
|
|
66
|
+
return redacted;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function redactContext(ctx) {
|
|
70
|
+
return {
|
|
71
|
+
...ctx,
|
|
72
|
+
url: redactUrl(ctx.url)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function stripInlineYamlComment(raw) {
|
|
76
|
+
let inSingle = false;
|
|
77
|
+
let inDouble = false;
|
|
78
|
+
for (let i = 0; i < raw.length; i++) {
|
|
79
|
+
const ch = raw[i];
|
|
80
|
+
if (ch === "'" && !inDouble) {
|
|
81
|
+
inSingle = !inSingle;
|
|
82
|
+
} else if (ch === '"' && !inSingle && raw[i - 1] !== "\\") {
|
|
83
|
+
inDouble = !inDouble;
|
|
84
|
+
} else if (ch === "#" && !inSingle && !inDouble) {
|
|
85
|
+
if (i === 0 || /\s/.test(raw[i - 1])) {
|
|
86
|
+
return raw.slice(0, i);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return raw;
|
|
91
|
+
}
|
|
92
|
+
function unquoteYamlValue(raw) {
|
|
93
|
+
const trimmed = raw.trim();
|
|
94
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2 || trimmed.startsWith("'") && trimmed.endsWith("'") && trimmed.length >= 2) {
|
|
95
|
+
return trimmed.slice(1, -1);
|
|
96
|
+
}
|
|
97
|
+
return trimmed;
|
|
98
|
+
}
|
|
99
|
+
function parseBool(raw, fieldName = "readOnly") {
|
|
100
|
+
if (raw === void 0 || raw === null) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
if (typeof raw === "boolean") {
|
|
104
|
+
return raw;
|
|
105
|
+
}
|
|
106
|
+
if (typeof raw === "string") {
|
|
107
|
+
const lower = raw.trim().toLowerCase();
|
|
108
|
+
if (lower === "") {
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
if (lower === "true" || lower === "yes" || lower === "1") {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
if (lower === "false" || lower === "no" || lower === "0") {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`Invalid boolean value "${raw}" for "${fieldName}" in contexts config.`);
|
|
118
|
+
}
|
|
119
|
+
throw new Error(`Invalid boolean value for "${fieldName}" in contexts config.`);
|
|
120
|
+
}
|
|
121
|
+
function normalizeBackend(raw) {
|
|
122
|
+
if (raw === void 0 || raw === null || raw === "") {
|
|
123
|
+
return void 0;
|
|
124
|
+
}
|
|
125
|
+
if (typeof raw !== "string") {
|
|
126
|
+
throw new Error(`Invalid backend in contexts config. Expected bullmq, river, or pg-boss.`);
|
|
127
|
+
}
|
|
128
|
+
const normalized = raw.trim().toLowerCase();
|
|
129
|
+
if (!VALID_BACKENDS.has(normalized)) {
|
|
130
|
+
throw new Error(`Invalid backend "${raw}" in contexts config. Expected bullmq, river, or pg-boss.`);
|
|
131
|
+
}
|
|
132
|
+
return normalized;
|
|
133
|
+
}
|
|
134
|
+
function normalizeEntry(name, obj) {
|
|
135
|
+
const url = typeof obj.url === "string" ? obj.url.trim() : "";
|
|
136
|
+
if (!url) {
|
|
137
|
+
throw new Error(`Context "${name}" is missing a required "url" field.`);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
name,
|
|
141
|
+
url,
|
|
142
|
+
backend: normalizeBackend(obj.backend),
|
|
143
|
+
readOnly: parseBool(obj.readOnly ?? obj.read_only, "readOnly"),
|
|
144
|
+
prefix: typeof obj.prefix === "string" && obj.prefix.trim() ? obj.prefix.trim() : void 0,
|
|
145
|
+
schema: typeof obj.schema === "string" && obj.schema.trim() ? obj.schema.trim() : void 0
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function parseSimpleYamlContexts(content) {
|
|
149
|
+
const lines = content.split(/\r?\n/);
|
|
150
|
+
const entries = /* @__PURE__ */ new Map();
|
|
151
|
+
let insideContextsRoot = false;
|
|
152
|
+
let ignoreCurrentTopSection = false;
|
|
153
|
+
let currentName = null;
|
|
154
|
+
let contextIndent = null;
|
|
155
|
+
for (const rawLine of lines) {
|
|
156
|
+
const withoutComment = stripInlineYamlComment(rawLine);
|
|
157
|
+
if (withoutComment.trim().length === 0) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const indent = rawLine.length - rawLine.trimStart().length;
|
|
161
|
+
const trimmed = withoutComment.trim();
|
|
162
|
+
if (indent === 0 && trimmed === "contexts:") {
|
|
163
|
+
insideContextsRoot = true;
|
|
164
|
+
ignoreCurrentTopSection = false;
|
|
165
|
+
currentName = null;
|
|
166
|
+
contextIndent = null;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (indent === 0 && insideContextsRoot) {
|
|
170
|
+
insideContextsRoot = false;
|
|
171
|
+
ignoreCurrentTopSection = true;
|
|
172
|
+
currentName = null;
|
|
173
|
+
contextIndent = null;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (ignoreCurrentTopSection) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const colonIdx = trimmed.indexOf(":");
|
|
180
|
+
if (colonIdx === -1) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const key = unquoteYamlValue(trimmed.slice(0, colonIdx));
|
|
184
|
+
const value = unquoteYamlValue(trimmed.slice(colonIdx + 1));
|
|
185
|
+
if (value === "") {
|
|
186
|
+
if (!insideContextsRoot && indent === 0) {
|
|
187
|
+
currentName = key;
|
|
188
|
+
contextIndent = 0;
|
|
189
|
+
if (!entries.has(currentName)) {
|
|
190
|
+
entries.set(currentName, {});
|
|
191
|
+
}
|
|
192
|
+
} else if (insideContextsRoot && (contextIndent === null || indent === contextIndent)) {
|
|
193
|
+
currentName = key;
|
|
194
|
+
contextIndent = indent;
|
|
195
|
+
if (!entries.has(currentName)) {
|
|
196
|
+
entries.set(currentName, {});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (currentName !== null && contextIndent !== null && indent > contextIndent) {
|
|
202
|
+
const target = entries.get(currentName);
|
|
203
|
+
if (target) {
|
|
204
|
+
target[key] = value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const result = [];
|
|
209
|
+
for (const [name, fields] of entries) {
|
|
210
|
+
result.push(normalizeEntry(name, fields));
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
function parseJsonContexts(content) {
|
|
215
|
+
const parsed = JSON.parse(content);
|
|
216
|
+
if (!parsed || typeof parsed !== "object") {
|
|
217
|
+
throw new Error("Contexts JSON must be an object.");
|
|
218
|
+
}
|
|
219
|
+
const root = parsed;
|
|
220
|
+
const source = "contexts" in root ? root.contexts : root;
|
|
221
|
+
if (Array.isArray(source)) {
|
|
222
|
+
return source.map((item, idx) => {
|
|
223
|
+
if (!item || typeof item !== "object") {
|
|
224
|
+
throw new Error(`Invalid context entry at index ${idx}.`);
|
|
225
|
+
}
|
|
226
|
+
const rec = item;
|
|
227
|
+
const name = typeof rec.name === "string" ? rec.name.trim() : "";
|
|
228
|
+
if (!name) {
|
|
229
|
+
throw new Error(`Context entry at index ${idx} is missing "name".`);
|
|
230
|
+
}
|
|
231
|
+
return normalizeEntry(name, rec);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (source && typeof source === "object") {
|
|
235
|
+
const entries = [];
|
|
236
|
+
for (const [name, value] of Object.entries(source)) {
|
|
237
|
+
if (!value || typeof value !== "object") {
|
|
238
|
+
throw new Error(`Invalid context definition for "${name}".`);
|
|
239
|
+
}
|
|
240
|
+
entries.push(normalizeEntry(name, value));
|
|
241
|
+
}
|
|
242
|
+
return entries;
|
|
243
|
+
}
|
|
244
|
+
throw new Error("Invalid contexts structure in config file.");
|
|
245
|
+
}
|
|
246
|
+
function getDefaultConfigPaths() {
|
|
247
|
+
const envPath = process.env.Q9S_CONFIG;
|
|
248
|
+
if (envPath && envPath.trim().length > 0) {
|
|
249
|
+
return [path.resolve(envPath.trim())];
|
|
250
|
+
}
|
|
251
|
+
const baseDir = path.join(os.homedir(), ".config", "q9s");
|
|
252
|
+
return [
|
|
253
|
+
path.join(baseDir, "contexts.json"),
|
|
254
|
+
path.join(baseDir, "contexts.yaml"),
|
|
255
|
+
path.join(baseDir, "contexts.yml")
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
function findConfigFile(explicitPath) {
|
|
259
|
+
if (explicitPath) {
|
|
260
|
+
const resolved = path.resolve(explicitPath);
|
|
261
|
+
if (!fs.existsSync(resolved)) {
|
|
262
|
+
throw new Error(`Contexts config file not found: ${resolved}`);
|
|
263
|
+
}
|
|
264
|
+
return resolved;
|
|
265
|
+
}
|
|
266
|
+
for (const candidate of getDefaultConfigPaths()) {
|
|
267
|
+
if (fs.existsSync(candidate)) {
|
|
268
|
+
return candidate;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
function loadContexts(explicitPath) {
|
|
274
|
+
const filePath = findConfigFile(explicitPath);
|
|
275
|
+
if (!filePath) {
|
|
276
|
+
return [];
|
|
277
|
+
}
|
|
278
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
279
|
+
const lowerPath = filePath.toLowerCase();
|
|
280
|
+
if (lowerPath.endsWith(".yaml") || lowerPath.endsWith(".yml")) {
|
|
281
|
+
return parseSimpleYamlContexts(content);
|
|
282
|
+
}
|
|
283
|
+
return parseJsonContexts(content);
|
|
284
|
+
}
|
|
285
|
+
function resolveContext(name, explicitPath) {
|
|
286
|
+
const contexts = loadContexts(explicitPath);
|
|
287
|
+
const match = contexts.find((ctx) => ctx.name === name);
|
|
288
|
+
if (!match) {
|
|
289
|
+
const available = contexts.map((c) => c.name).join(", ");
|
|
290
|
+
const hint = available ? `Available contexts: ${available}` : `Create ${path.join(os.homedir(), ".config", "q9s", "contexts.json")} to define named contexts.`;
|
|
291
|
+
throw new Error(`Context "${name}" not found. ${hint}`);
|
|
292
|
+
}
|
|
293
|
+
return match;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/clients/resp.ts
|
|
297
|
+
var RespError = class extends Error {
|
|
298
|
+
code = "RESP_SERVER_ERROR";
|
|
299
|
+
constructor(message) {
|
|
300
|
+
super(message);
|
|
301
|
+
this.name = "RespError";
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
var RespProtocolError = class extends Error {
|
|
305
|
+
code = "RESP_PROTOCOL_ERROR";
|
|
306
|
+
constructor(message) {
|
|
307
|
+
super(message);
|
|
308
|
+
this.name = "RespProtocolError";
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
var RespTimeoutError = class extends Error {
|
|
312
|
+
code = "RESP_TIMEOUT";
|
|
313
|
+
constructor(message) {
|
|
314
|
+
super(message);
|
|
315
|
+
this.name = "RespTimeoutError";
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
var CRLF = Buffer.from("\r\n", "utf8");
|
|
319
|
+
var STRICT_INT_RE = /^-?\d+$/;
|
|
320
|
+
var STRICT_UINT_RE = /^\d+$/;
|
|
321
|
+
var MAX_RESP_DEPTH = 32;
|
|
322
|
+
var MAX_RESP_BULK_BYTES = 16 * 1024 * 1024;
|
|
323
|
+
var MAX_RESP_ARRAY_LEN = 1e5;
|
|
324
|
+
var MAX_RESP_BUFFER_BYTES = 32 * 1024 * 1024;
|
|
325
|
+
function parseStrictInt(line, label) {
|
|
326
|
+
if (!STRICT_INT_RE.test(line)) {
|
|
327
|
+
throw new RespProtocolError(`Invalid RESP ${label}: "${line}"`);
|
|
328
|
+
}
|
|
329
|
+
const num = Number(line);
|
|
330
|
+
if (!Number.isSafeInteger(num)) {
|
|
331
|
+
throw new RespProtocolError(`Out-of-range RESP ${label}: "${line}"`);
|
|
332
|
+
}
|
|
333
|
+
return num;
|
|
334
|
+
}
|
|
335
|
+
function parseRedisUrl(rawUrl) {
|
|
336
|
+
let parsed;
|
|
337
|
+
try {
|
|
338
|
+
parsed = new URL(rawUrl);
|
|
339
|
+
} catch {
|
|
340
|
+
throw new Error(`Invalid Redis URL: ${redactUrl(rawUrl)}`);
|
|
341
|
+
}
|
|
342
|
+
if (parsed.protocol !== "redis:" && parsed.protocol !== "rediss:") {
|
|
343
|
+
throw new Error(`Unsupported Redis protocol "${parsed.protocol}". Expected redis:// or rediss://`);
|
|
344
|
+
}
|
|
345
|
+
const rawHost = parsed.hostname || "127.0.0.1";
|
|
346
|
+
const host = rawHost.startsWith("[") && rawHost.endsWith("]") ? rawHost.slice(1, -1) : rawHost;
|
|
347
|
+
let port = 6379;
|
|
348
|
+
if (parsed.port) {
|
|
349
|
+
if (!STRICT_UINT_RE.test(parsed.port)) {
|
|
350
|
+
throw new Error(`Invalid Redis port: ${parsed.port}`);
|
|
351
|
+
}
|
|
352
|
+
port = Number(parsed.port);
|
|
353
|
+
}
|
|
354
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
355
|
+
throw new Error(`Invalid Redis port: ${parsed.port}`);
|
|
356
|
+
}
|
|
357
|
+
const username = parsed.username ? decodeURIComponent(parsed.username) : void 0;
|
|
358
|
+
const password = parsed.password ? decodeURIComponent(parsed.password) : void 0;
|
|
359
|
+
let db = 0;
|
|
360
|
+
const trimmedPath = parsed.pathname.replace(/^\/+/, "");
|
|
361
|
+
if (trimmedPath.length > 0) {
|
|
362
|
+
if (!STRICT_UINT_RE.test(trimmedPath)) {
|
|
363
|
+
throw new Error(`Invalid Redis database index: ${trimmedPath}`);
|
|
364
|
+
}
|
|
365
|
+
const parsedDb = Number(trimmedPath);
|
|
366
|
+
if (!Number.isSafeInteger(parsedDb) || parsedDb < 0) {
|
|
367
|
+
throw new Error(`Invalid Redis database index: ${trimmedPath}`);
|
|
368
|
+
}
|
|
369
|
+
db = parsedDb;
|
|
370
|
+
}
|
|
371
|
+
const useTls = parsed.protocol === "rediss:";
|
|
372
|
+
return {
|
|
373
|
+
host,
|
|
374
|
+
port,
|
|
375
|
+
username,
|
|
376
|
+
password,
|
|
377
|
+
db,
|
|
378
|
+
tls: useTls,
|
|
379
|
+
servername: useTls && net.isIP(host) === 0 ? host : void 0
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
function encodeRespCommand(args) {
|
|
383
|
+
const parts = [Buffer.from(`*${args.length}\r
|
|
384
|
+
`, "utf8")];
|
|
385
|
+
for (const arg of args) {
|
|
386
|
+
const buf = Buffer.isBuffer(arg) ? arg : Buffer.from(String(arg), "utf8");
|
|
387
|
+
parts.push(Buffer.from(`$${buf.length}\r
|
|
388
|
+
`, "utf8"), buf, CRLF);
|
|
389
|
+
}
|
|
390
|
+
return Buffer.concat(parts);
|
|
391
|
+
}
|
|
392
|
+
function findCrlf(buf, offset) {
|
|
393
|
+
for (let i = offset; i < buf.length - 1; i++) {
|
|
394
|
+
if (buf[i] === 13 && buf[i + 1] === 10) {
|
|
395
|
+
return i;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return -1;
|
|
399
|
+
}
|
|
400
|
+
function parseRespFrame(buf, offset = 0, depth = 0) {
|
|
401
|
+
if (depth > MAX_RESP_DEPTH) {
|
|
402
|
+
throw new RespProtocolError(`RESP nesting depth exceeded maximum of ${MAX_RESP_DEPTH}`);
|
|
403
|
+
}
|
|
404
|
+
if (offset >= buf.length) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const prefix = buf[offset];
|
|
408
|
+
const lineEnd = findCrlf(buf, offset + 1);
|
|
409
|
+
if (lineEnd === -1) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
const line = buf.toString("utf8", offset + 1, lineEnd);
|
|
413
|
+
const afterLine = lineEnd + 2;
|
|
414
|
+
switch (prefix) {
|
|
415
|
+
case 43:
|
|
416
|
+
return { value: line, nextOffset: afterLine };
|
|
417
|
+
case 45:
|
|
418
|
+
return { value: new RespError(line), nextOffset: afterLine };
|
|
419
|
+
case 58: {
|
|
420
|
+
const num = parseStrictInt(line, "integer");
|
|
421
|
+
return { value: num, nextOffset: afterLine };
|
|
422
|
+
}
|
|
423
|
+
case 36: {
|
|
424
|
+
const byteLength = parseStrictInt(line, "bulk length");
|
|
425
|
+
if (byteLength === -1) {
|
|
426
|
+
return { value: null, nextOffset: afterLine };
|
|
427
|
+
}
|
|
428
|
+
if (byteLength < 0) {
|
|
429
|
+
throw new RespProtocolError(`Invalid negative RESP bulk length: ${byteLength}`);
|
|
430
|
+
}
|
|
431
|
+
if (byteLength > MAX_RESP_BULK_BYTES) {
|
|
432
|
+
throw new RespProtocolError(`RESP bulk length ${byteLength} exceeds limit of ${MAX_RESP_BULK_BYTES} bytes`);
|
|
433
|
+
}
|
|
434
|
+
const payloadEnd = afterLine + byteLength;
|
|
435
|
+
if (buf.length < payloadEnd + 2) {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
if (buf[payloadEnd] !== 13 || buf[payloadEnd + 1] !== 10) {
|
|
439
|
+
throw new RespProtocolError("Missing CRLF after RESP bulk string payload");
|
|
440
|
+
}
|
|
441
|
+
const text = buf.toString("utf8", afterLine, payloadEnd);
|
|
442
|
+
return { value: text, nextOffset: payloadEnd + 2 };
|
|
443
|
+
}
|
|
444
|
+
case 42: {
|
|
445
|
+
const count = parseStrictInt(line, "array length");
|
|
446
|
+
if (count === -1) {
|
|
447
|
+
return { value: null, nextOffset: afterLine };
|
|
448
|
+
}
|
|
449
|
+
if (count < 0) {
|
|
450
|
+
throw new RespProtocolError(`Invalid negative RESP array length: ${count}`);
|
|
451
|
+
}
|
|
452
|
+
if (count > MAX_RESP_ARRAY_LEN) {
|
|
453
|
+
throw new RespProtocolError(`RESP array length ${count} exceeds limit of ${MAX_RESP_ARRAY_LEN}`);
|
|
454
|
+
}
|
|
455
|
+
const items = [];
|
|
456
|
+
let cursor = afterLine;
|
|
457
|
+
for (let i = 0; i < count; i++) {
|
|
458
|
+
const elem = parseRespFrame(buf, cursor, depth + 1);
|
|
459
|
+
if (!elem) {
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
items.push(elem.value);
|
|
463
|
+
cursor = elem.nextOffset;
|
|
464
|
+
}
|
|
465
|
+
return { value: items, nextOffset: cursor };
|
|
466
|
+
}
|
|
467
|
+
default:
|
|
468
|
+
throw new RespProtocolError(
|
|
469
|
+
`Unexpected RESP2 type byte: 0x${(prefix ?? 0).toString(16)}`
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
var RespParser = class {
|
|
474
|
+
buffer = Buffer.alloc(0);
|
|
475
|
+
push(chunk) {
|
|
476
|
+
if (this.buffer.length + chunk.length > MAX_RESP_BUFFER_BYTES) {
|
|
477
|
+
this.buffer = Buffer.alloc(0);
|
|
478
|
+
throw new RespProtocolError(`RESP parser buffer exceeded ${MAX_RESP_BUFFER_BYTES} bytes`);
|
|
479
|
+
}
|
|
480
|
+
this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
|
|
481
|
+
const frames = [];
|
|
482
|
+
let offset = 0;
|
|
483
|
+
while (offset < this.buffer.length) {
|
|
484
|
+
const parsed = parseRespFrame(this.buffer, offset, 0);
|
|
485
|
+
if (!parsed) {
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
frames.push(parsed.value);
|
|
489
|
+
offset = parsed.nextOffset;
|
|
490
|
+
}
|
|
491
|
+
if (offset > 0) {
|
|
492
|
+
this.buffer = offset === this.buffer.length ? Buffer.alloc(0) : this.buffer.subarray(offset);
|
|
493
|
+
}
|
|
494
|
+
return frames;
|
|
495
|
+
}
|
|
496
|
+
get bufferedBytes() {
|
|
497
|
+
return this.buffer.length;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
var RespClient = class _RespClient {
|
|
501
|
+
socket;
|
|
502
|
+
parser = new RespParser();
|
|
503
|
+
pending = [];
|
|
504
|
+
timeoutMs;
|
|
505
|
+
closed = false;
|
|
506
|
+
constructor(socket, timeoutMs) {
|
|
507
|
+
this.socket = socket;
|
|
508
|
+
this.timeoutMs = timeoutMs;
|
|
509
|
+
this.socket.on("data", (chunk) => {
|
|
510
|
+
try {
|
|
511
|
+
const frames = this.parser.push(chunk);
|
|
512
|
+
for (const frame of frames) {
|
|
513
|
+
const item = this.pending.shift();
|
|
514
|
+
if (!item) {
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
clearTimeout(item.timer);
|
|
518
|
+
if (frame instanceof RespError) {
|
|
519
|
+
item.reject(frame);
|
|
520
|
+
} else {
|
|
521
|
+
item.resolve(frame);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
} catch (err) {
|
|
525
|
+
const protocolErr = err instanceof Error ? err : new Error(String(err));
|
|
526
|
+
this.closed = true;
|
|
527
|
+
this.failAllPending(protocolErr);
|
|
528
|
+
this.socket.destroy(protocolErr);
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
this.socket.on("error", (err) => {
|
|
532
|
+
this.closed = true;
|
|
533
|
+
this.failAllPending(err);
|
|
534
|
+
});
|
|
535
|
+
this.socket.on("close", () => {
|
|
536
|
+
this.closed = true;
|
|
537
|
+
this.failAllPending(new Error("Redis connection closed"));
|
|
538
|
+
});
|
|
539
|
+
this.socket.resume();
|
|
540
|
+
}
|
|
541
|
+
static async connect(target, options = {}) {
|
|
542
|
+
const config = typeof target === "string" ? parseRedisUrl(target) : target;
|
|
543
|
+
const timeoutMs = options.timeoutMs ?? 5e3;
|
|
544
|
+
const client = await new Promise((resolve, reject) => {
|
|
545
|
+
let settled = false;
|
|
546
|
+
const connectEvent = config.tls ? "secureConnect" : "connect";
|
|
547
|
+
const sock = config.tls ? tls.connect({
|
|
548
|
+
host: config.host,
|
|
549
|
+
port: config.port,
|
|
550
|
+
servername: config.servername,
|
|
551
|
+
rejectUnauthorized: options.rejectUnauthorized ?? true
|
|
552
|
+
}) : net.createConnection({
|
|
553
|
+
host: config.host,
|
|
554
|
+
port: config.port
|
|
555
|
+
});
|
|
556
|
+
const cleanup = () => {
|
|
557
|
+
clearTimeout(timer);
|
|
558
|
+
sock.removeListener(connectEvent, onConnect);
|
|
559
|
+
sock.removeListener("error", onError);
|
|
560
|
+
sock.removeListener("close", onClose);
|
|
561
|
+
};
|
|
562
|
+
const timer = setTimeout(() => {
|
|
563
|
+
if (!settled) {
|
|
564
|
+
settled = true;
|
|
565
|
+
cleanup();
|
|
566
|
+
sock.destroy();
|
|
567
|
+
reject(
|
|
568
|
+
new RespTimeoutError(
|
|
569
|
+
`Timed out connecting to Redis at ${config.host}:${config.port} after ${timeoutMs}ms`
|
|
570
|
+
)
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
}, timeoutMs);
|
|
574
|
+
const onConnect = () => {
|
|
575
|
+
if (!settled) {
|
|
576
|
+
settled = true;
|
|
577
|
+
cleanup();
|
|
578
|
+
sock.pause();
|
|
579
|
+
sock.setNoDelay(true);
|
|
580
|
+
resolve(new _RespClient(sock, timeoutMs));
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
const onError = (err) => {
|
|
584
|
+
if (!settled) {
|
|
585
|
+
settled = true;
|
|
586
|
+
cleanup();
|
|
587
|
+
sock.destroy();
|
|
588
|
+
reject(err);
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
const onClose = () => {
|
|
592
|
+
if (!settled) {
|
|
593
|
+
settled = true;
|
|
594
|
+
cleanup();
|
|
595
|
+
reject(new Error(`Redis connection closed during handshake (${config.host}:${config.port})`));
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
sock.once(connectEvent, onConnect);
|
|
599
|
+
sock.once("error", onError);
|
|
600
|
+
sock.once("close", onClose);
|
|
601
|
+
});
|
|
602
|
+
try {
|
|
603
|
+
if (config.password !== void 0) {
|
|
604
|
+
if (config.username !== void 0 && config.username.length > 0) {
|
|
605
|
+
await client.command("AUTH", config.username, config.password);
|
|
606
|
+
} else {
|
|
607
|
+
await client.command("AUTH", config.password);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (config.db > 0) {
|
|
611
|
+
await client.command("SELECT", config.db);
|
|
612
|
+
}
|
|
613
|
+
} catch (err) {
|
|
614
|
+
await client.close();
|
|
615
|
+
throw err;
|
|
616
|
+
}
|
|
617
|
+
return client;
|
|
618
|
+
}
|
|
619
|
+
command(...args) {
|
|
620
|
+
if (this.closed || this.socket.destroyed) {
|
|
621
|
+
return Promise.reject(new Error("Cannot send command on closed Redis connection"));
|
|
622
|
+
}
|
|
623
|
+
const encoded = encodeRespCommand(args);
|
|
624
|
+
const cmdName = String(args[0] ?? "UNKNOWN");
|
|
625
|
+
return new Promise((resolve, reject) => {
|
|
626
|
+
const timer = setTimeout(() => {
|
|
627
|
+
const timeoutErr = new RespTimeoutError(
|
|
628
|
+
`Redis command ${cmdName} timed out after ${this.timeoutMs}ms`
|
|
629
|
+
);
|
|
630
|
+
this.closed = true;
|
|
631
|
+
this.failAllPending(timeoutErr);
|
|
632
|
+
this.socket.destroy(timeoutErr);
|
|
633
|
+
}, this.timeoutMs);
|
|
634
|
+
const entry = { resolve, reject, timer };
|
|
635
|
+
this.pending.push(entry);
|
|
636
|
+
this.socket.write(encoded, (err) => {
|
|
637
|
+
if (err) {
|
|
638
|
+
const idx = this.pending.indexOf(entry);
|
|
639
|
+
if (idx !== -1) {
|
|
640
|
+
this.pending.splice(idx, 1);
|
|
641
|
+
}
|
|
642
|
+
clearTimeout(timer);
|
|
643
|
+
reject(err);
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
async ping() {
|
|
649
|
+
const res = await this.command("PING");
|
|
650
|
+
return String(res ?? "");
|
|
651
|
+
}
|
|
652
|
+
async scan(cursor, pattern, count) {
|
|
653
|
+
const args = ["SCAN", cursor];
|
|
654
|
+
if (pattern !== void 0) {
|
|
655
|
+
args.push("MATCH", pattern);
|
|
656
|
+
}
|
|
657
|
+
if (count !== void 0) {
|
|
658
|
+
args.push("COUNT", count);
|
|
659
|
+
}
|
|
660
|
+
const res = await this.command(...args);
|
|
661
|
+
if (!Array.isArray(res) || res.length < 2) {
|
|
662
|
+
throw new RespProtocolError("Unexpected SCAN response shape");
|
|
663
|
+
}
|
|
664
|
+
const nextCursor = String(res[0] ?? "0");
|
|
665
|
+
const rawKeys = Array.isArray(res[1]) ? res[1] : [];
|
|
666
|
+
const keys = rawKeys.map((k) => String(k ?? ""));
|
|
667
|
+
return { cursor: nextCursor, keys };
|
|
668
|
+
}
|
|
669
|
+
async scanAll(pattern, count = 200) {
|
|
670
|
+
const seen = /* @__PURE__ */ new Set();
|
|
671
|
+
let cursor = "0";
|
|
672
|
+
let iterations = 0;
|
|
673
|
+
const maxIterations = 5e3;
|
|
674
|
+
do {
|
|
675
|
+
const page = await this.scan(cursor, pattern, count);
|
|
676
|
+
for (const key of page.keys) {
|
|
677
|
+
seen.add(key);
|
|
678
|
+
}
|
|
679
|
+
cursor = page.cursor;
|
|
680
|
+
iterations++;
|
|
681
|
+
} while (cursor !== "0" && iterations < maxIterations);
|
|
682
|
+
if (cursor !== "0") {
|
|
683
|
+
throw new RespProtocolError(
|
|
684
|
+
`SCAN exceeded ${maxIterations} iterations without completing (pattern: ${pattern})`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return Array.from(seen).sort();
|
|
688
|
+
}
|
|
689
|
+
async llen(key) {
|
|
690
|
+
const res = await this.command("LLEN", key);
|
|
691
|
+
return typeof res === "number" ? res : Number.parseInt(String(res ?? "0"), 10) || 0;
|
|
692
|
+
}
|
|
693
|
+
async zcard(key) {
|
|
694
|
+
const res = await this.command("ZCARD", key);
|
|
695
|
+
return typeof res === "number" ? res : Number.parseInt(String(res ?? "0"), 10) || 0;
|
|
696
|
+
}
|
|
697
|
+
async lrange(key, start, stop) {
|
|
698
|
+
const res = await this.command("LRANGE", key, start, stop);
|
|
699
|
+
return Array.isArray(res) ? res.map((item) => String(item ?? "")) : [];
|
|
700
|
+
}
|
|
701
|
+
async zrange(key, start, stop) {
|
|
702
|
+
const res = await this.command("ZRANGE", key, start, stop);
|
|
703
|
+
return Array.isArray(res) ? res.map((item) => String(item ?? "")) : [];
|
|
704
|
+
}
|
|
705
|
+
async zrevrange(key, start, stop) {
|
|
706
|
+
const res = await this.command("ZREVRANGE", key, start, stop);
|
|
707
|
+
return Array.isArray(res) ? res.map((item) => String(item ?? "")) : [];
|
|
708
|
+
}
|
|
709
|
+
async hget(key, field) {
|
|
710
|
+
const res = await this.command("HGET", key, field);
|
|
711
|
+
return res === null ? null : String(res);
|
|
712
|
+
}
|
|
713
|
+
async hgetall(key) {
|
|
714
|
+
const res = await this.command("HGETALL", key);
|
|
715
|
+
const record = {};
|
|
716
|
+
if (!Array.isArray(res)) {
|
|
717
|
+
return record;
|
|
718
|
+
}
|
|
719
|
+
for (let i = 0; i + 1 < res.length; i += 2) {
|
|
720
|
+
const field = String(res[i] ?? "");
|
|
721
|
+
const value = String(res[i + 1] ?? "");
|
|
722
|
+
record[field] = value;
|
|
723
|
+
}
|
|
724
|
+
return record;
|
|
725
|
+
}
|
|
726
|
+
async close() {
|
|
727
|
+
if (this.closed) {
|
|
728
|
+
if (!this.socket.destroyed) {
|
|
729
|
+
this.socket.destroy();
|
|
730
|
+
}
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
this.closed = true;
|
|
734
|
+
if (this.socket.destroyed) {
|
|
735
|
+
this.failAllPending(new Error("Redis connection closed"));
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
await new Promise((resolve) => {
|
|
739
|
+
const done = () => {
|
|
740
|
+
clearTimeout(forceTimer);
|
|
741
|
+
resolve();
|
|
742
|
+
};
|
|
743
|
+
const forceTimer = setTimeout(() => {
|
|
744
|
+
this.socket.destroy();
|
|
745
|
+
resolve();
|
|
746
|
+
}, 250);
|
|
747
|
+
this.socket.once("close", done);
|
|
748
|
+
this.socket.end();
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
failAllPending(err) {
|
|
752
|
+
while (this.pending.length > 0) {
|
|
753
|
+
const item = this.pending.shift();
|
|
754
|
+
if (item) {
|
|
755
|
+
clearTimeout(item.timer);
|
|
756
|
+
item.reject(err);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
// src/backends/bullmq.ts
|
|
763
|
+
function safeJsonParse(raw) {
|
|
764
|
+
if (raw === void 0 || raw === "") {
|
|
765
|
+
return {};
|
|
766
|
+
}
|
|
767
|
+
try {
|
|
768
|
+
return JSON.parse(raw);
|
|
769
|
+
} catch {
|
|
770
|
+
return raw;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function parseOptionalInt(raw) {
|
|
774
|
+
if (raw === void 0 || raw === "") {
|
|
775
|
+
return void 0;
|
|
776
|
+
}
|
|
777
|
+
const parsed = Number.parseInt(raw, 10);
|
|
778
|
+
return Number.isFinite(parsed) ? parsed : void 0;
|
|
779
|
+
}
|
|
780
|
+
function clampLimit(limit, fallback, max) {
|
|
781
|
+
const n = Number.isFinite(limit) ? Math.trunc(limit) : fallback;
|
|
782
|
+
return Math.max(1, Math.min(n, max));
|
|
783
|
+
}
|
|
784
|
+
function escapeRedisGlob(value) {
|
|
785
|
+
return value.replace(/([*?[\]\\])/g, "\\$1");
|
|
786
|
+
}
|
|
787
|
+
function parseStacktrace(raw) {
|
|
788
|
+
if (!raw) {
|
|
789
|
+
return void 0;
|
|
790
|
+
}
|
|
791
|
+
const parsed = safeJsonParse(raw);
|
|
792
|
+
if (Array.isArray(parsed)) {
|
|
793
|
+
const lines = parsed.map((entry) => String(entry ?? "").trim()).filter((entry) => entry.length > 0);
|
|
794
|
+
return lines.length > 0 ? lines : void 0;
|
|
795
|
+
}
|
|
796
|
+
if (typeof parsed === "string" && parsed.trim().length > 0) {
|
|
797
|
+
return [parsed.trim()];
|
|
798
|
+
}
|
|
799
|
+
return void 0;
|
|
800
|
+
}
|
|
801
|
+
function extractQueueNameFromKey(key, prefix, suffix) {
|
|
802
|
+
const head = `${prefix}:`;
|
|
803
|
+
const tail = `:${suffix}`;
|
|
804
|
+
if (!key.startsWith(head) || !key.endsWith(tail)) {
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
const inner = key.slice(head.length, key.length - tail.length);
|
|
808
|
+
if (inner.length === 0) {
|
|
809
|
+
return null;
|
|
810
|
+
}
|
|
811
|
+
if (inner.endsWith(":metrics") || inner.endsWith(":repeat")) {
|
|
812
|
+
return null;
|
|
813
|
+
}
|
|
814
|
+
return inner;
|
|
815
|
+
}
|
|
816
|
+
function parseBullMqJobHash(queue, jobId, state, hash) {
|
|
817
|
+
const opts = safeJsonParse(hash.opts);
|
|
818
|
+
const maxAttempts = typeof opts === "object" && opts !== null && "attempts" in opts && typeof opts.attempts === "number" ? opts.attempts : void 0;
|
|
819
|
+
const attemptsMade = parseOptionalInt(hash.attemptsMade) ?? parseOptionalInt(hash.atm) ?? 0;
|
|
820
|
+
return {
|
|
821
|
+
id: jobId,
|
|
822
|
+
queue,
|
|
823
|
+
name: hash.name || "default",
|
|
824
|
+
state,
|
|
825
|
+
data: safeJsonParse(hash.data),
|
|
826
|
+
failedReason: hash.failedReason || void 0,
|
|
827
|
+
stacktrace: parseStacktrace(hash.stacktrace),
|
|
828
|
+
attemptsMade,
|
|
829
|
+
maxAttempts,
|
|
830
|
+
timestamp: parseOptionalInt(hash.timestamp),
|
|
831
|
+
processedOn: parseOptionalInt(hash.processedOn),
|
|
832
|
+
finishedOn: parseOptionalInt(hash.finishedOn),
|
|
833
|
+
delay: parseOptionalInt(hash.delay)
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
var BullMqBackend = class _BullMqBackend {
|
|
837
|
+
kind = "bullmq";
|
|
838
|
+
readOnly;
|
|
839
|
+
client;
|
|
840
|
+
ownsClient;
|
|
841
|
+
prefix;
|
|
842
|
+
constructor(client, ownsClient, options) {
|
|
843
|
+
this.client = client;
|
|
844
|
+
this.ownsClient = ownsClient;
|
|
845
|
+
this.readOnly = options.readOnly !== false;
|
|
846
|
+
const cleanedPrefix = (options.prefix ?? "bull").replace(/:+$/, "").trim();
|
|
847
|
+
if (!cleanedPrefix) {
|
|
848
|
+
throw new Error("BullMQ prefix cannot be empty.");
|
|
849
|
+
}
|
|
850
|
+
this.prefix = cleanedPrefix;
|
|
851
|
+
}
|
|
852
|
+
static async connect(url, options) {
|
|
853
|
+
if (options.client) {
|
|
854
|
+
return new _BullMqBackend(options.client, false, options);
|
|
855
|
+
}
|
|
856
|
+
const client = await RespClient.connect(url, {
|
|
857
|
+
timeoutMs: options.timeoutMs ?? 5e3,
|
|
858
|
+
rejectUnauthorized: options.rejectUnauthorized
|
|
859
|
+
});
|
|
860
|
+
return new _BullMqBackend(client, true, options);
|
|
861
|
+
}
|
|
862
|
+
static fromClient(client, options) {
|
|
863
|
+
return new _BullMqBackend(client, false, options);
|
|
864
|
+
}
|
|
865
|
+
key(queue, suffix) {
|
|
866
|
+
return `${this.prefix}:${queue}:${suffix}`;
|
|
867
|
+
}
|
|
868
|
+
async discoverQueues() {
|
|
869
|
+
const queueNames = /* @__PURE__ */ new Set();
|
|
870
|
+
const suffixes = ["meta", "id", "failed"];
|
|
871
|
+
const escapedPrefix = escapeRedisGlob(this.prefix);
|
|
872
|
+
for (const suffix of suffixes) {
|
|
873
|
+
const keys = await this.client.scanAll(`${escapedPrefix}:*:${suffix}`);
|
|
874
|
+
for (const fullKey of keys) {
|
|
875
|
+
const name = extractQueueNameFromKey(fullKey, this.prefix, suffix);
|
|
876
|
+
if (name) {
|
|
877
|
+
queueNames.add(name);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return Array.from(queueNames).sort((a, b) => a.localeCompare(b));
|
|
882
|
+
}
|
|
883
|
+
async listQueues() {
|
|
884
|
+
const names = await this.discoverQueues();
|
|
885
|
+
const results = [];
|
|
886
|
+
for (const name of names) {
|
|
887
|
+
const [
|
|
888
|
+
waitCount,
|
|
889
|
+
pausedCount,
|
|
890
|
+
prioritizedCount,
|
|
891
|
+
active,
|
|
892
|
+
delayed,
|
|
893
|
+
completed,
|
|
894
|
+
failed,
|
|
895
|
+
pausedMeta,
|
|
896
|
+
metricsRaw
|
|
897
|
+
] = await Promise.all([
|
|
898
|
+
this.client.llen(this.key(name, "wait")),
|
|
899
|
+
this.client.llen(this.key(name, "paused")),
|
|
900
|
+
this.client.zcard(this.key(name, "prioritized")),
|
|
901
|
+
this.client.llen(this.key(name, "active")),
|
|
902
|
+
this.client.zcard(this.key(name, "delayed")),
|
|
903
|
+
this.client.zcard(this.key(name, "completed")),
|
|
904
|
+
this.client.zcard(this.key(name, "failed")),
|
|
905
|
+
this.client.hget(this.key(name, "meta"), "paused"),
|
|
906
|
+
this.client.lrange(this.key(name, "metrics:completed:data"), 0, 15)
|
|
907
|
+
]);
|
|
908
|
+
const parsedMetrics = metricsRaw.map((v) => Number.parseFloat(v)).filter((n) => Number.isFinite(n)).reverse();
|
|
909
|
+
results.push({
|
|
910
|
+
name,
|
|
911
|
+
backend: "bullmq",
|
|
912
|
+
waiting: waitCount + pausedCount + prioritizedCount,
|
|
913
|
+
active,
|
|
914
|
+
delayed,
|
|
915
|
+
completed,
|
|
916
|
+
failed,
|
|
917
|
+
paused: pausedMeta === "1",
|
|
918
|
+
throughputHistory: parsedMetrics.length > 0 ? parsedMetrics : void 0
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
return results;
|
|
922
|
+
}
|
|
923
|
+
async listJobs(queue, state, limit = 25) {
|
|
924
|
+
const safeLimit = clampLimit(limit, 25, 500);
|
|
925
|
+
const stop = safeLimit - 1;
|
|
926
|
+
let ids;
|
|
927
|
+
switch (state) {
|
|
928
|
+
case "failed":
|
|
929
|
+
ids = await this.client.zrevrange(this.key(queue, "failed"), 0, stop);
|
|
930
|
+
break;
|
|
931
|
+
case "completed":
|
|
932
|
+
ids = await this.client.zrevrange(this.key(queue, "completed"), 0, stop);
|
|
933
|
+
break;
|
|
934
|
+
case "delayed":
|
|
935
|
+
ids = await this.client.zrange(this.key(queue, "delayed"), 0, stop);
|
|
936
|
+
break;
|
|
937
|
+
case "waiting": {
|
|
938
|
+
const [waitIds, pausedIds, prioIds] = await Promise.all([
|
|
939
|
+
this.client.lrange(this.key(queue, "wait"), 0, stop),
|
|
940
|
+
this.client.lrange(this.key(queue, "paused"), 0, stop),
|
|
941
|
+
this.client.zrange(this.key(queue, "prioritized"), 0, stop)
|
|
942
|
+
]);
|
|
943
|
+
const merged = /* @__PURE__ */ new Set();
|
|
944
|
+
for (const id of [...waitIds, ...pausedIds, ...prioIds]) {
|
|
945
|
+
merged.add(id);
|
|
946
|
+
if (merged.size >= safeLimit) break;
|
|
947
|
+
}
|
|
948
|
+
ids = Array.from(merged);
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
case "active":
|
|
952
|
+
ids = await this.client.lrange(this.key(queue, "active"), 0, stop);
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
const jobs = [];
|
|
956
|
+
for (const id of ids) {
|
|
957
|
+
const hash = await this.client.hgetall(this.key(queue, id));
|
|
958
|
+
if (Object.keys(hash).length === 0) {
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
jobs.push(parseBullMqJobHash(queue, id, state, hash));
|
|
962
|
+
}
|
|
963
|
+
return jobs;
|
|
964
|
+
}
|
|
965
|
+
async getJob(queue, jobId) {
|
|
966
|
+
const hash = await this.client.hgetall(this.key(queue, jobId));
|
|
967
|
+
if (Object.keys(hash).length === 0) {
|
|
968
|
+
return null;
|
|
969
|
+
}
|
|
970
|
+
const [failedScore, completedScore, delayedScore] = await Promise.all([
|
|
971
|
+
this.client.command("ZSCORE", this.key(queue, "failed"), jobId),
|
|
972
|
+
this.client.command("ZSCORE", this.key(queue, "completed"), jobId),
|
|
973
|
+
this.client.command("ZSCORE", this.key(queue, "delayed"), jobId)
|
|
974
|
+
]);
|
|
975
|
+
let state = "waiting";
|
|
976
|
+
if (failedScore !== null) {
|
|
977
|
+
state = "failed";
|
|
978
|
+
} else if (completedScore !== null) {
|
|
979
|
+
state = "completed";
|
|
980
|
+
} else if (delayedScore !== null) {
|
|
981
|
+
state = "delayed";
|
|
982
|
+
} else if (hash.processedOn && !hash.finishedOn) {
|
|
983
|
+
state = "active";
|
|
984
|
+
}
|
|
985
|
+
return parseBullMqJobHash(queue, jobId, state, hash);
|
|
986
|
+
}
|
|
987
|
+
async retryJob(queue, jobId) {
|
|
988
|
+
if (this.readOnly) {
|
|
989
|
+
throw new ReadOnlyError(`retry job ${jobId} in queue ${queue}`);
|
|
990
|
+
}
|
|
991
|
+
const failedKey = this.key(queue, "failed");
|
|
992
|
+
const metaKey = this.key(queue, "meta");
|
|
993
|
+
await this.client.command("WATCH", failedKey, metaKey);
|
|
994
|
+
let inTx = false;
|
|
995
|
+
try {
|
|
996
|
+
const [score, pausedMeta] = await Promise.all([
|
|
997
|
+
this.client.command("ZSCORE", failedKey, jobId),
|
|
998
|
+
this.client.hget(metaKey, "paused")
|
|
999
|
+
]);
|
|
1000
|
+
if (score === null) {
|
|
1001
|
+
await this.client.command("UNWATCH");
|
|
1002
|
+
return { queue, retried: 0, jobIds: [] };
|
|
1003
|
+
}
|
|
1004
|
+
const jobKey = this.key(queue, jobId);
|
|
1005
|
+
const targetListKey = this.key(queue, pausedMeta === "1" ? "paused" : "wait");
|
|
1006
|
+
await this.client.command("MULTI");
|
|
1007
|
+
inTx = true;
|
|
1008
|
+
await this.client.command("ZREM", failedKey, jobId);
|
|
1009
|
+
await this.client.command(
|
|
1010
|
+
"HDEL",
|
|
1011
|
+
jobKey,
|
|
1012
|
+
"failedReason",
|
|
1013
|
+
"stacktrace",
|
|
1014
|
+
"processedOn",
|
|
1015
|
+
"finishedOn",
|
|
1016
|
+
"returnvalue"
|
|
1017
|
+
);
|
|
1018
|
+
await this.client.command("RPUSH", targetListKey, jobId);
|
|
1019
|
+
const execRes = await this.client.command("EXEC");
|
|
1020
|
+
inTx = false;
|
|
1021
|
+
if (execRes === null || !Array.isArray(execRes)) {
|
|
1022
|
+
return { queue, retried: 0, jobIds: [] };
|
|
1023
|
+
}
|
|
1024
|
+
for (const entry of execRes) {
|
|
1025
|
+
if (entry instanceof RespError) {
|
|
1026
|
+
throw entry;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
const removed = typeof execRes[0] === "number" ? execRes[0] : 0;
|
|
1030
|
+
if (removed === 0) {
|
|
1031
|
+
return { queue, retried: 0, jobIds: [] };
|
|
1032
|
+
}
|
|
1033
|
+
return { queue, retried: 1, jobIds: [jobId] };
|
|
1034
|
+
} catch (err) {
|
|
1035
|
+
if (inTx) {
|
|
1036
|
+
await this.client.command("DISCARD").catch(() => void 0);
|
|
1037
|
+
} else {
|
|
1038
|
+
await this.client.command("UNWATCH").catch(() => void 0);
|
|
1039
|
+
}
|
|
1040
|
+
throw err;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
async retryFailedBulk(queue, limit = 100) {
|
|
1044
|
+
if (this.readOnly) {
|
|
1045
|
+
throw new ReadOnlyError(`bulk retry failed jobs in queue ${queue}`);
|
|
1046
|
+
}
|
|
1047
|
+
const safeLimit = clampLimit(limit, 100, 1e3);
|
|
1048
|
+
const failedKey = this.key(queue, "failed");
|
|
1049
|
+
const metaKey = this.key(queue, "meta");
|
|
1050
|
+
await this.client.command("WATCH", failedKey, metaKey);
|
|
1051
|
+
let inTx = false;
|
|
1052
|
+
try {
|
|
1053
|
+
const [ids, pausedMeta] = await Promise.all([
|
|
1054
|
+
this.client.zrange(failedKey, 0, safeLimit - 1),
|
|
1055
|
+
this.client.hget(metaKey, "paused")
|
|
1056
|
+
]);
|
|
1057
|
+
if (ids.length === 0) {
|
|
1058
|
+
await this.client.command("UNWATCH");
|
|
1059
|
+
return { queue, retried: 0, jobIds: [] };
|
|
1060
|
+
}
|
|
1061
|
+
const targetListKey = this.key(queue, pausedMeta === "1" ? "paused" : "wait");
|
|
1062
|
+
await this.client.command("MULTI");
|
|
1063
|
+
inTx = true;
|
|
1064
|
+
for (const jobId of ids) {
|
|
1065
|
+
const jobKey = this.key(queue, jobId);
|
|
1066
|
+
await this.client.command("ZREM", failedKey, jobId);
|
|
1067
|
+
await this.client.command(
|
|
1068
|
+
"HDEL",
|
|
1069
|
+
jobKey,
|
|
1070
|
+
"failedReason",
|
|
1071
|
+
"stacktrace",
|
|
1072
|
+
"processedOn",
|
|
1073
|
+
"finishedOn",
|
|
1074
|
+
"returnvalue"
|
|
1075
|
+
);
|
|
1076
|
+
await this.client.command("RPUSH", targetListKey, jobId);
|
|
1077
|
+
}
|
|
1078
|
+
const execRes = await this.client.command("EXEC");
|
|
1079
|
+
inTx = false;
|
|
1080
|
+
if (execRes === null || !Array.isArray(execRes)) {
|
|
1081
|
+
return { queue, retried: 0, jobIds: [] };
|
|
1082
|
+
}
|
|
1083
|
+
for (const entry of execRes) {
|
|
1084
|
+
if (entry instanceof RespError) {
|
|
1085
|
+
throw entry;
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
const retriedIds = [];
|
|
1089
|
+
for (let i = 0; i < ids.length; i++) {
|
|
1090
|
+
const zremRes = execRes[i * 3];
|
|
1091
|
+
if (typeof zremRes === "number" && zremRes > 0) {
|
|
1092
|
+
retriedIds.push(ids[i]);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return { queue, retried: retriedIds.length, jobIds: retriedIds };
|
|
1096
|
+
} catch (err) {
|
|
1097
|
+
if (inTx) {
|
|
1098
|
+
await this.client.command("DISCARD").catch(() => void 0);
|
|
1099
|
+
} else {
|
|
1100
|
+
await this.client.command("UNWATCH").catch(() => void 0);
|
|
1101
|
+
}
|
|
1102
|
+
throw err;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
async close() {
|
|
1106
|
+
if (this.ownsClient) {
|
|
1107
|
+
await this.client.close();
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
// src/backends/postgres.ts
|
|
1113
|
+
import { execFileSync } from "node:child_process";
|
|
1114
|
+
var PSQL_FIELD_SEPARATOR = "";
|
|
1115
|
+
var PSQL_MAX_BUFFER_BYTES = 16 * 1024 * 1024;
|
|
1116
|
+
var IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_]{0,62}$/;
|
|
1117
|
+
var QUEUE_NAME_RE = /^[a-zA-Z0-9_.:/@-]{1,128}$/;
|
|
1118
|
+
var RIVER_ID_RE = /^[0-9]{1,20}$/;
|
|
1119
|
+
var UUID_RE = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
1120
|
+
var STRIPPED_PG_QUERY_KEYS = /* @__PURE__ */ new Set(["password", "pass", "passwd", "pwd", "options"]);
|
|
1121
|
+
function clampLimit2(limit, fallback, max) {
|
|
1122
|
+
const n = Number.isFinite(limit) ? Math.trunc(limit) : fallback;
|
|
1123
|
+
return Math.max(1, Math.min(n, max));
|
|
1124
|
+
}
|
|
1125
|
+
function parsePostgresUrl(rawUrl) {
|
|
1126
|
+
let parsed;
|
|
1127
|
+
try {
|
|
1128
|
+
parsed = new URL(rawUrl);
|
|
1129
|
+
} catch {
|
|
1130
|
+
throw new Error(`Invalid Postgres URL: ${redactUrl(rawUrl)}`);
|
|
1131
|
+
}
|
|
1132
|
+
if (parsed.protocol !== "postgres:" && parsed.protocol !== "postgresql:") {
|
|
1133
|
+
throw new Error(
|
|
1134
|
+
`Unsupported Postgres protocol "${parsed.protocol}". Expected postgres:// or postgresql://`
|
|
1135
|
+
);
|
|
1136
|
+
}
|
|
1137
|
+
const rawHost = parsed.hostname || "127.0.0.1";
|
|
1138
|
+
const host = rawHost.startsWith("[") && rawHost.endsWith("]") ? rawHost.slice(1, -1) : rawHost;
|
|
1139
|
+
const port = parsed.port ? Number.parseInt(parsed.port, 10) : 5432;
|
|
1140
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
1141
|
+
throw new Error(`Invalid Postgres port: ${parsed.port}`);
|
|
1142
|
+
}
|
|
1143
|
+
const username = parsed.username ? decodeURIComponent(parsed.username) : void 0;
|
|
1144
|
+
let password = parsed.password ? decodeURIComponent(parsed.password) : void 0;
|
|
1145
|
+
const database = parsed.pathname.replace(/^\/+/, "") || "postgres";
|
|
1146
|
+
const sanitized = new URL(parsed.toString());
|
|
1147
|
+
sanitized.password = "";
|
|
1148
|
+
for (const key of Array.from(sanitized.searchParams.keys())) {
|
|
1149
|
+
const lower = key.toLowerCase();
|
|
1150
|
+
if (lower === "password" && password === void 0) {
|
|
1151
|
+
const val = sanitized.searchParams.get(key);
|
|
1152
|
+
if (val !== null) {
|
|
1153
|
+
password = val;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
if (STRIPPED_PG_QUERY_KEYS.has(lower)) {
|
|
1157
|
+
sanitized.searchParams.delete(key);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
return {
|
|
1161
|
+
sanitizedUrl: sanitized.toString(),
|
|
1162
|
+
host,
|
|
1163
|
+
port,
|
|
1164
|
+
database,
|
|
1165
|
+
username,
|
|
1166
|
+
password
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
function validateIdentifier(identifier, label = "identifier") {
|
|
1170
|
+
if (!IDENTIFIER_RE.test(identifier)) {
|
|
1171
|
+
throw new Error(`Invalid SQL ${label}: "${identifier}"`);
|
|
1172
|
+
}
|
|
1173
|
+
return identifier;
|
|
1174
|
+
}
|
|
1175
|
+
function validateQueueName(queue) {
|
|
1176
|
+
if (!QUEUE_NAME_RE.test(queue)) {
|
|
1177
|
+
throw new Error(`Invalid queue name: "${queue}"`);
|
|
1178
|
+
}
|
|
1179
|
+
return queue;
|
|
1180
|
+
}
|
|
1181
|
+
function validateJobId(jobId, kind) {
|
|
1182
|
+
if (kind === "river") {
|
|
1183
|
+
if (!RIVER_ID_RE.test(jobId)) {
|
|
1184
|
+
throw new Error(`Invalid River job ID (expected numeric bigint): "${jobId}"`);
|
|
1185
|
+
}
|
|
1186
|
+
return jobId;
|
|
1187
|
+
}
|
|
1188
|
+
if (!UUID_RE.test(jobId)) {
|
|
1189
|
+
throw new Error(`Invalid pg-boss job ID (expected UUID): "${jobId}"`);
|
|
1190
|
+
}
|
|
1191
|
+
return jobId;
|
|
1192
|
+
}
|
|
1193
|
+
function sqlLiteral(value) {
|
|
1194
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
1195
|
+
}
|
|
1196
|
+
function safeJsonParse2(raw) {
|
|
1197
|
+
if (!raw || raw.trim() === "") {
|
|
1198
|
+
return {};
|
|
1199
|
+
}
|
|
1200
|
+
try {
|
|
1201
|
+
return JSON.parse(raw);
|
|
1202
|
+
} catch {
|
|
1203
|
+
return raw;
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
function parseIntField(raw) {
|
|
1207
|
+
if (!raw) {
|
|
1208
|
+
return 0;
|
|
1209
|
+
}
|
|
1210
|
+
const n = Number.parseInt(raw, 10);
|
|
1211
|
+
return Number.isFinite(n) ? n : 0;
|
|
1212
|
+
}
|
|
1213
|
+
function parseOptionalIntField(raw) {
|
|
1214
|
+
if (raw === void 0 || raw.trim() === "") {
|
|
1215
|
+
return void 0;
|
|
1216
|
+
}
|
|
1217
|
+
const n = Number.parseInt(raw, 10);
|
|
1218
|
+
return Number.isFinite(n) ? n : void 0;
|
|
1219
|
+
}
|
|
1220
|
+
function parseTimestampMs(raw) {
|
|
1221
|
+
if (!raw || raw.trim() === "") {
|
|
1222
|
+
return void 0;
|
|
1223
|
+
}
|
|
1224
|
+
const asNum = Number(raw);
|
|
1225
|
+
if (Number.isFinite(asNum) && asNum > 0) {
|
|
1226
|
+
return Math.round(asNum);
|
|
1227
|
+
}
|
|
1228
|
+
const ms = Date.parse(raw);
|
|
1229
|
+
return Number.isFinite(ms) ? ms : void 0;
|
|
1230
|
+
}
|
|
1231
|
+
function buildDetectPostgresBackendSql(schema) {
|
|
1232
|
+
if (schema) {
|
|
1233
|
+
const safeSchema = validateIdentifier(schema, "schema");
|
|
1234
|
+
return [
|
|
1235
|
+
"SELECT table_schema, table_name",
|
|
1236
|
+
"FROM information_schema.tables",
|
|
1237
|
+
`WHERE table_schema = ${sqlLiteral(safeSchema)}`,
|
|
1238
|
+
" AND table_name IN ('river_job', 'job')",
|
|
1239
|
+
"ORDER BY table_name ASC;"
|
|
1240
|
+
].join(" ");
|
|
1241
|
+
}
|
|
1242
|
+
return [
|
|
1243
|
+
"SELECT table_schema, table_name",
|
|
1244
|
+
"FROM information_schema.tables",
|
|
1245
|
+
"WHERE (table_name = 'river_job' AND table_schema NOT IN ('pg_catalog', 'information_schema'))",
|
|
1246
|
+
" OR (table_schema = 'pgboss' AND table_name = 'job')",
|
|
1247
|
+
"ORDER BY CASE WHEN table_name = 'river_job' THEN 0 ELSE 1 END, table_schema ASC;"
|
|
1248
|
+
].join(" ");
|
|
1249
|
+
}
|
|
1250
|
+
function parseBackendDetection(output) {
|
|
1251
|
+
const lines = output.split(/\r?\n/).map((l) => l.trim()).filter((l) => l.length > 0);
|
|
1252
|
+
for (const line of lines) {
|
|
1253
|
+
const [schema, table] = line.split(PSQL_FIELD_SEPARATOR);
|
|
1254
|
+
if (schema && table === "river_job") {
|
|
1255
|
+
return { kind: "river", schema: validateIdentifier(schema, "schema") };
|
|
1256
|
+
}
|
|
1257
|
+
if (schema && table === "job") {
|
|
1258
|
+
return { kind: "pg-boss", schema: validateIdentifier(schema, "schema") };
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
throw new Error(
|
|
1262
|
+
"Could not auto-detect River (river_job) or pg-boss (pgboss.job) tables in the target Postgres database."
|
|
1263
|
+
);
|
|
1264
|
+
}
|
|
1265
|
+
function buildRiverQueueStatsSql(schema = "public") {
|
|
1266
|
+
const s = validateIdentifier(schema, "schema");
|
|
1267
|
+
return [
|
|
1268
|
+
"SELECT",
|
|
1269
|
+
" queue,",
|
|
1270
|
+
" COUNT(*) FILTER (WHERE state IN ('available', 'pending')) AS waiting,",
|
|
1271
|
+
" COUNT(*) FILTER (WHERE state = 'running') AS active,",
|
|
1272
|
+
" COUNT(*) FILTER (WHERE state IN ('scheduled', 'retryable')) AS delayed,",
|
|
1273
|
+
" COUNT(*) FILTER (WHERE state = 'completed') AS completed,",
|
|
1274
|
+
" COUNT(*) FILTER (WHERE state IN ('discarded', 'cancelled')) AS failed",
|
|
1275
|
+
`FROM "${s}"."river_job"`,
|
|
1276
|
+
"GROUP BY queue",
|
|
1277
|
+
"ORDER BY queue ASC;"
|
|
1278
|
+
].join(" ");
|
|
1279
|
+
}
|
|
1280
|
+
function parseRiverQueueStats(output) {
|
|
1281
|
+
const lines = output.split(/\r?\n/).map((l) => l.trim()).filter((l) => l.length > 0);
|
|
1282
|
+
return lines.map((line) => {
|
|
1283
|
+
const cols = line.split(PSQL_FIELD_SEPARATOR);
|
|
1284
|
+
return {
|
|
1285
|
+
name: cols[0] ?? "default",
|
|
1286
|
+
backend: "river",
|
|
1287
|
+
waiting: parseIntField(cols[1]),
|
|
1288
|
+
active: parseIntField(cols[2]),
|
|
1289
|
+
delayed: parseIntField(cols[3]),
|
|
1290
|
+
completed: parseIntField(cols[4]),
|
|
1291
|
+
failed: parseIntField(cols[5])
|
|
1292
|
+
};
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
function riverStatesFor(state) {
|
|
1296
|
+
switch (state) {
|
|
1297
|
+
case "waiting":
|
|
1298
|
+
return ["available", "pending"];
|
|
1299
|
+
case "active":
|
|
1300
|
+
return ["running"];
|
|
1301
|
+
case "delayed":
|
|
1302
|
+
return ["scheduled", "retryable"];
|
|
1303
|
+
case "completed":
|
|
1304
|
+
return ["completed"];
|
|
1305
|
+
case "failed":
|
|
1306
|
+
return ["discarded", "cancelled"];
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
function mapRiverStateToJobState(rawState) {
|
|
1310
|
+
switch (rawState) {
|
|
1311
|
+
case "running":
|
|
1312
|
+
return "active";
|
|
1313
|
+
case "scheduled":
|
|
1314
|
+
case "retryable":
|
|
1315
|
+
return "delayed";
|
|
1316
|
+
case "completed":
|
|
1317
|
+
return "completed";
|
|
1318
|
+
case "discarded":
|
|
1319
|
+
case "cancelled":
|
|
1320
|
+
return "failed";
|
|
1321
|
+
default:
|
|
1322
|
+
return "waiting";
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
function buildRiverJobsSql(schema, queue, state, limit = 25) {
|
|
1326
|
+
const s = validateIdentifier(schema, "schema");
|
|
1327
|
+
const q = validateQueueName(queue);
|
|
1328
|
+
const safeLimit = clampLimit2(limit, 25, 500);
|
|
1329
|
+
const statesList = riverStatesFor(state).map(sqlLiteral).join(", ");
|
|
1330
|
+
return [
|
|
1331
|
+
"SELECT",
|
|
1332
|
+
" id::text,",
|
|
1333
|
+
" kind,",
|
|
1334
|
+
" state::text,",
|
|
1335
|
+
" COALESCE(args::jsonb::text, '{}'),",
|
|
1336
|
+
" COALESCE(array_to_json(errors)::jsonb::text, '[]'),",
|
|
1337
|
+
" attempt::text,",
|
|
1338
|
+
" max_attempts::text,",
|
|
1339
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM created_at) * 1000)::bigint::text, ''),",
|
|
1340
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM attempted_at) * 1000)::bigint::text, ''),",
|
|
1341
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM finalized_at) * 1000)::bigint::text, '')",
|
|
1342
|
+
`FROM "${s}"."river_job"`,
|
|
1343
|
+
`WHERE queue = ${sqlLiteral(q)} AND state IN (${statesList})`,
|
|
1344
|
+
"ORDER BY id DESC",
|
|
1345
|
+
`LIMIT ${safeLimit};`
|
|
1346
|
+
].join(" ");
|
|
1347
|
+
}
|
|
1348
|
+
function buildRiverGetJobSql(schema, queue, jobId) {
|
|
1349
|
+
const s = validateIdentifier(schema, "schema");
|
|
1350
|
+
const q = validateQueueName(queue);
|
|
1351
|
+
const id = validateJobId(jobId, "river");
|
|
1352
|
+
return [
|
|
1353
|
+
"SELECT",
|
|
1354
|
+
" id::text,",
|
|
1355
|
+
" kind,",
|
|
1356
|
+
" state::text,",
|
|
1357
|
+
" COALESCE(args::jsonb::text, '{}'),",
|
|
1358
|
+
" COALESCE(array_to_json(errors)::jsonb::text, '[]'),",
|
|
1359
|
+
" attempt::text,",
|
|
1360
|
+
" max_attempts::text,",
|
|
1361
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM created_at) * 1000)::bigint::text, ''),",
|
|
1362
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM attempted_at) * 1000)::bigint::text, ''),",
|
|
1363
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM finalized_at) * 1000)::bigint::text, '')",
|
|
1364
|
+
`FROM "${s}"."river_job"`,
|
|
1365
|
+
`WHERE queue = ${sqlLiteral(q)} AND id = ${id}`,
|
|
1366
|
+
"LIMIT 1;"
|
|
1367
|
+
].join(" ");
|
|
1368
|
+
}
|
|
1369
|
+
function parseRiverJobs(output, queue) {
|
|
1370
|
+
const lines = output.split(/\r?\n/).map((l) => l.trim()).filter((l) => l.length > 0);
|
|
1371
|
+
return lines.map((line) => {
|
|
1372
|
+
const cols = line.split(PSQL_FIELD_SEPARATOR);
|
|
1373
|
+
const id = cols[0] ?? "";
|
|
1374
|
+
const kind = cols[1] ?? "unknown";
|
|
1375
|
+
const rawState = cols[2] ?? "available";
|
|
1376
|
+
const data = safeJsonParse2(cols[3]);
|
|
1377
|
+
const parsedErrors = safeJsonParse2(cols[4]);
|
|
1378
|
+
let failedReason;
|
|
1379
|
+
let stacktrace;
|
|
1380
|
+
if (Array.isArray(parsedErrors) && parsedErrors.length > 0) {
|
|
1381
|
+
const latest = parsedErrors[parsedErrors.length - 1];
|
|
1382
|
+
if (latest && typeof latest === "object") {
|
|
1383
|
+
if (typeof latest.error === "string" && latest.error.length > 0) {
|
|
1384
|
+
failedReason = latest.error;
|
|
1385
|
+
}
|
|
1386
|
+
if (typeof latest.trace === "string" && latest.trace.trim().length > 0) {
|
|
1387
|
+
stacktrace = latest.trace.split(/\r?\n/).filter((l) => l.trim().length > 0);
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
return {
|
|
1392
|
+
id,
|
|
1393
|
+
queue,
|
|
1394
|
+
name: kind,
|
|
1395
|
+
state: mapRiverStateToJobState(rawState),
|
|
1396
|
+
data,
|
|
1397
|
+
failedReason,
|
|
1398
|
+
stacktrace,
|
|
1399
|
+
attemptsMade: parseIntField(cols[5]),
|
|
1400
|
+
maxAttempts: parseOptionalIntField(cols[6]),
|
|
1401
|
+
timestamp: parseTimestampMs(cols[7]),
|
|
1402
|
+
processedOn: parseTimestampMs(cols[8]),
|
|
1403
|
+
finishedOn: parseTimestampMs(cols[9])
|
|
1404
|
+
};
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
function buildRiverRetryJobSql(schema, queue, jobId) {
|
|
1408
|
+
const s = validateIdentifier(schema, "schema");
|
|
1409
|
+
const q = validateQueueName(queue);
|
|
1410
|
+
const id = validateJobId(jobId, "river");
|
|
1411
|
+
return [
|
|
1412
|
+
`UPDATE "${s}"."river_job"`,
|
|
1413
|
+
"SET state = 'available', scheduled_at = NOW(), finalized_at = NULL, max_attempts = CASE WHEN max_attempts <= attempt THEN LEAST(attempt + 1, 32767) ELSE max_attempts END",
|
|
1414
|
+
`WHERE queue = ${sqlLiteral(q)} AND id = ${id} AND state IN ('discarded', 'cancelled')`,
|
|
1415
|
+
"RETURNING id::text;"
|
|
1416
|
+
].join(" ");
|
|
1417
|
+
}
|
|
1418
|
+
function buildRiverRetryFailedBulkSql(schema, queue, limit = 100) {
|
|
1419
|
+
const s = validateIdentifier(schema, "schema");
|
|
1420
|
+
const q = validateQueueName(queue);
|
|
1421
|
+
const safeLimit = clampLimit2(limit, 100, 1e3);
|
|
1422
|
+
return [
|
|
1423
|
+
"WITH target AS (",
|
|
1424
|
+
` SELECT id FROM "${s}"."river_job"`,
|
|
1425
|
+
` WHERE queue = ${sqlLiteral(q)} AND state IN ('discarded', 'cancelled')`,
|
|
1426
|
+
" ORDER BY id ASC",
|
|
1427
|
+
` LIMIT ${safeLimit}`,
|
|
1428
|
+
" FOR UPDATE SKIP LOCKED",
|
|
1429
|
+
")",
|
|
1430
|
+
`UPDATE "${s}"."river_job" r`,
|
|
1431
|
+
"SET state = 'available', scheduled_at = NOW(), finalized_at = NULL, max_attempts = CASE WHEN max_attempts <= attempt THEN LEAST(attempt + 1, 32767) ELSE max_attempts END",
|
|
1432
|
+
"FROM target",
|
|
1433
|
+
"WHERE r.id = target.id",
|
|
1434
|
+
"RETURNING r.id::text;"
|
|
1435
|
+
].join(" ");
|
|
1436
|
+
}
|
|
1437
|
+
function buildPgBossQueueStatsSql(schema = "pgboss") {
|
|
1438
|
+
const s = validateIdentifier(schema, "schema");
|
|
1439
|
+
return [
|
|
1440
|
+
"SELECT",
|
|
1441
|
+
" name,",
|
|
1442
|
+
" COUNT(*) FILTER (WHERE state = 'created' AND start_after <= NOW()) AS waiting,",
|
|
1443
|
+
" COUNT(*) FILTER (WHERE state = 'active') AS active,",
|
|
1444
|
+
" COUNT(*) FILTER (WHERE (state = 'created' AND start_after > NOW()) OR state = 'retry') AS delayed,",
|
|
1445
|
+
" COUNT(*) FILTER (WHERE state = 'completed') AS completed,",
|
|
1446
|
+
" COUNT(*) FILTER (WHERE state IN ('failed', 'cancelled')) AS failed",
|
|
1447
|
+
`FROM "${s}"."job"`,
|
|
1448
|
+
"GROUP BY name",
|
|
1449
|
+
"ORDER BY name ASC;"
|
|
1450
|
+
].join(" ");
|
|
1451
|
+
}
|
|
1452
|
+
function parsePgBossQueueStats(output) {
|
|
1453
|
+
const lines = output.split(/\r?\n/).map((l) => l.trim()).filter((l) => l.length > 0);
|
|
1454
|
+
return lines.map((line) => {
|
|
1455
|
+
const cols = line.split(PSQL_FIELD_SEPARATOR);
|
|
1456
|
+
return {
|
|
1457
|
+
name: cols[0] ?? "default",
|
|
1458
|
+
backend: "pg-boss",
|
|
1459
|
+
waiting: parseIntField(cols[1]),
|
|
1460
|
+
active: parseIntField(cols[2]),
|
|
1461
|
+
delayed: parseIntField(cols[3]),
|
|
1462
|
+
completed: parseIntField(cols[4]),
|
|
1463
|
+
failed: parseIntField(cols[5])
|
|
1464
|
+
};
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
function pgBossWhereForState(state) {
|
|
1468
|
+
switch (state) {
|
|
1469
|
+
case "waiting":
|
|
1470
|
+
return "state = 'created' AND start_after <= NOW()";
|
|
1471
|
+
case "active":
|
|
1472
|
+
return "state = 'active'";
|
|
1473
|
+
case "delayed":
|
|
1474
|
+
return "((state = 'created' AND start_after > NOW()) OR state = 'retry')";
|
|
1475
|
+
case "completed":
|
|
1476
|
+
return "state = 'completed'";
|
|
1477
|
+
case "failed":
|
|
1478
|
+
return "state IN ('failed', 'cancelled')";
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
function mapPgBossStateToJobState(rawState) {
|
|
1482
|
+
switch (rawState) {
|
|
1483
|
+
case "active":
|
|
1484
|
+
return "active";
|
|
1485
|
+
case "retry":
|
|
1486
|
+
return "delayed";
|
|
1487
|
+
case "completed":
|
|
1488
|
+
return "completed";
|
|
1489
|
+
case "failed":
|
|
1490
|
+
case "cancelled":
|
|
1491
|
+
return "failed";
|
|
1492
|
+
default:
|
|
1493
|
+
return "waiting";
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
function buildPgBossJobsSql(schema, queue, state, limit = 25) {
|
|
1497
|
+
const s = validateIdentifier(schema, "schema");
|
|
1498
|
+
const q = validateQueueName(queue);
|
|
1499
|
+
const safeLimit = clampLimit2(limit, 25, 500);
|
|
1500
|
+
return [
|
|
1501
|
+
"SELECT",
|
|
1502
|
+
" id::text,",
|
|
1503
|
+
" name,",
|
|
1504
|
+
" state::text,",
|
|
1505
|
+
" COALESCE(data::jsonb::text, '{}'),",
|
|
1506
|
+
" COALESCE(output::jsonb::text, 'null'),",
|
|
1507
|
+
" retry_count::text,",
|
|
1508
|
+
" retry_limit::text,",
|
|
1509
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM created_on) * 1000)::bigint::text, ''),",
|
|
1510
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM started_on) * 1000)::bigint::text, ''),",
|
|
1511
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM completed_on) * 1000)::bigint::text, '')",
|
|
1512
|
+
`FROM "${s}"."job"`,
|
|
1513
|
+
`WHERE name = ${sqlLiteral(q)} AND ${pgBossWhereForState(state)}`,
|
|
1514
|
+
"ORDER BY created_on DESC",
|
|
1515
|
+
`LIMIT ${safeLimit};`
|
|
1516
|
+
].join(" ");
|
|
1517
|
+
}
|
|
1518
|
+
function buildPgBossGetJobSql(schema, queue, jobId) {
|
|
1519
|
+
const s = validateIdentifier(schema, "schema");
|
|
1520
|
+
const q = validateQueueName(queue);
|
|
1521
|
+
const id = validateJobId(jobId, "pg-boss");
|
|
1522
|
+
return [
|
|
1523
|
+
"SELECT",
|
|
1524
|
+
" id::text,",
|
|
1525
|
+
" name,",
|
|
1526
|
+
" state::text,",
|
|
1527
|
+
" COALESCE(data::jsonb::text, '{}'),",
|
|
1528
|
+
" COALESCE(output::jsonb::text, 'null'),",
|
|
1529
|
+
" retry_count::text,",
|
|
1530
|
+
" retry_limit::text,",
|
|
1531
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM created_on) * 1000)::bigint::text, ''),",
|
|
1532
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM started_on) * 1000)::bigint::text, ''),",
|
|
1533
|
+
" COALESCE(FLOOR(EXTRACT(EPOCH FROM completed_on) * 1000)::bigint::text, '')",
|
|
1534
|
+
`FROM "${s}"."job"`,
|
|
1535
|
+
`WHERE name = ${sqlLiteral(q)} AND id = ${sqlLiteral(id)}`,
|
|
1536
|
+
"LIMIT 1;"
|
|
1537
|
+
].join(" ");
|
|
1538
|
+
}
|
|
1539
|
+
function parsePgBossJobs(output, queue) {
|
|
1540
|
+
const lines = output.split(/\r?\n/).map((l) => l.trim()).filter((l) => l.length > 0);
|
|
1541
|
+
return lines.map((line) => {
|
|
1542
|
+
const cols = line.split(PSQL_FIELD_SEPARATOR);
|
|
1543
|
+
const id = cols[0] ?? "";
|
|
1544
|
+
const name = cols[1] ?? queue;
|
|
1545
|
+
const rawState = cols[2] ?? "created";
|
|
1546
|
+
const data = safeJsonParse2(cols[3]);
|
|
1547
|
+
const parsedOutput = safeJsonParse2(cols[4]);
|
|
1548
|
+
let failedReason;
|
|
1549
|
+
let stacktrace;
|
|
1550
|
+
if (typeof parsedOutput === "string" && parsedOutput.length > 0) {
|
|
1551
|
+
failedReason = parsedOutput;
|
|
1552
|
+
} else if (parsedOutput && typeof parsedOutput === "object") {
|
|
1553
|
+
const obj = parsedOutput;
|
|
1554
|
+
if (typeof obj.message === "string" && obj.message.length > 0) {
|
|
1555
|
+
failedReason = obj.message;
|
|
1556
|
+
} else if (typeof obj.error === "string" && obj.error.length > 0) {
|
|
1557
|
+
failedReason = obj.error;
|
|
1558
|
+
}
|
|
1559
|
+
if (typeof obj.stack === "string" && obj.stack.trim().length > 0) {
|
|
1560
|
+
stacktrace = obj.stack.split(/\r?\n/).filter((l) => l.trim().length > 0);
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return {
|
|
1564
|
+
id,
|
|
1565
|
+
queue,
|
|
1566
|
+
name,
|
|
1567
|
+
state: mapPgBossStateToJobState(rawState),
|
|
1568
|
+
data,
|
|
1569
|
+
failedReason,
|
|
1570
|
+
stacktrace,
|
|
1571
|
+
attemptsMade: parseIntField(cols[5]),
|
|
1572
|
+
maxAttempts: parseOptionalIntField(cols[6]),
|
|
1573
|
+
timestamp: parseTimestampMs(cols[7]),
|
|
1574
|
+
processedOn: parseTimestampMs(cols[8]),
|
|
1575
|
+
finishedOn: parseTimestampMs(cols[9])
|
|
1576
|
+
};
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
function buildPgBossRetryJobSql(schema, queue, jobId) {
|
|
1580
|
+
const s = validateIdentifier(schema, "schema");
|
|
1581
|
+
const q = validateQueueName(queue);
|
|
1582
|
+
const id = validateJobId(jobId, "pg-boss");
|
|
1583
|
+
return [
|
|
1584
|
+
`UPDATE "${s}"."job"`,
|
|
1585
|
+
"SET state = 'created', retry_count = 0, started_on = NULL, completed_on = NULL, output = NULL, start_after = NOW()",
|
|
1586
|
+
`WHERE name = ${sqlLiteral(q)} AND id = ${sqlLiteral(id)} AND state IN ('failed', 'cancelled')`,
|
|
1587
|
+
"RETURNING id::text;"
|
|
1588
|
+
].join(" ");
|
|
1589
|
+
}
|
|
1590
|
+
function buildPgBossRetryFailedBulkSql(schema, queue, limit = 100) {
|
|
1591
|
+
const s = validateIdentifier(schema, "schema");
|
|
1592
|
+
const q = validateQueueName(queue);
|
|
1593
|
+
const safeLimit = clampLimit2(limit, 100, 1e3);
|
|
1594
|
+
return [
|
|
1595
|
+
"WITH target AS (",
|
|
1596
|
+
` SELECT id FROM "${s}"."job"`,
|
|
1597
|
+
` WHERE name = ${sqlLiteral(q)} AND state IN ('failed', 'cancelled')`,
|
|
1598
|
+
" ORDER BY created_on ASC",
|
|
1599
|
+
` LIMIT ${safeLimit}`,
|
|
1600
|
+
" FOR UPDATE SKIP LOCKED",
|
|
1601
|
+
")",
|
|
1602
|
+
`UPDATE "${s}"."job" j`,
|
|
1603
|
+
"SET state = 'created', retry_count = 0, started_on = NULL, completed_on = NULL, output = NULL, start_after = NOW()",
|
|
1604
|
+
"FROM target",
|
|
1605
|
+
`WHERE j.id = target.id AND j.name = ${sqlLiteral(q)}`,
|
|
1606
|
+
"RETURNING j.id::text;"
|
|
1607
|
+
].join(" ");
|
|
1608
|
+
}
|
|
1609
|
+
function parseReturnedJobIds(output) {
|
|
1610
|
+
return output.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
1611
|
+
}
|
|
1612
|
+
var PostgresQueueBackend = class _PostgresQueueBackend {
|
|
1613
|
+
kind;
|
|
1614
|
+
readOnly;
|
|
1615
|
+
schema;
|
|
1616
|
+
runner;
|
|
1617
|
+
constructor(kind, schema, readOnly, runner) {
|
|
1618
|
+
this.kind = kind;
|
|
1619
|
+
this.schema = validateIdentifier(schema, "schema");
|
|
1620
|
+
this.readOnly = readOnly !== false;
|
|
1621
|
+
this.runner = runner;
|
|
1622
|
+
}
|
|
1623
|
+
static connect(url, options) {
|
|
1624
|
+
const parsed = parsePostgresUrl(url);
|
|
1625
|
+
const psqlPath = options.psqlPath ?? "psql";
|
|
1626
|
+
const timeoutMs = options.timeoutMs ?? 1e4;
|
|
1627
|
+
const runner = options.sqlRunner ?? ((sql, readOnly) => {
|
|
1628
|
+
const env = { ...process.env };
|
|
1629
|
+
if (parsed.password !== void 0) {
|
|
1630
|
+
env.PGPASSWORD = parsed.password;
|
|
1631
|
+
}
|
|
1632
|
+
if (readOnly) {
|
|
1633
|
+
const existing = env.PGOPTIONS ? `${env.PGOPTIONS} ` : "";
|
|
1634
|
+
env.PGOPTIONS = `${existing}-c default_transaction_read_only=on`;
|
|
1635
|
+
}
|
|
1636
|
+
const effectiveSql = readOnly ? `BEGIN TRANSACTION READ ONLY; ${sql} COMMIT;` : sql;
|
|
1637
|
+
const args = [
|
|
1638
|
+
"-X",
|
|
1639
|
+
"-A",
|
|
1640
|
+
"-t",
|
|
1641
|
+
"-F",
|
|
1642
|
+
PSQL_FIELD_SEPARATOR,
|
|
1643
|
+
"-v",
|
|
1644
|
+
"ON_ERROR_STOP=1",
|
|
1645
|
+
"-d",
|
|
1646
|
+
parsed.sanitizedUrl,
|
|
1647
|
+
"-c",
|
|
1648
|
+
effectiveSql
|
|
1649
|
+
];
|
|
1650
|
+
try {
|
|
1651
|
+
return execFileSync(psqlPath, args, {
|
|
1652
|
+
env,
|
|
1653
|
+
encoding: "utf8",
|
|
1654
|
+
timeout: timeoutMs,
|
|
1655
|
+
maxBuffer: PSQL_MAX_BUFFER_BYTES,
|
|
1656
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1657
|
+
});
|
|
1658
|
+
} catch (err) {
|
|
1659
|
+
const stderr = err && typeof err === "object" && "stderr" in err && typeof err.stderr === "string" ? err.stderr.trim() : err instanceof Error ? err.message : String(err);
|
|
1660
|
+
throw new Error(`psql query failed on ${redactUrl(url)}: ${stderr || "unknown error"}`);
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
let kind = options.kind;
|
|
1664
|
+
let schema = options.schema;
|
|
1665
|
+
if (!kind) {
|
|
1666
|
+
const detectSql = buildDetectPostgresBackendSql(schema);
|
|
1667
|
+
const detectOut = runner(detectSql, true);
|
|
1668
|
+
const detected = parseBackendDetection(detectOut);
|
|
1669
|
+
kind = detected.kind;
|
|
1670
|
+
schema = schema ?? detected.schema;
|
|
1671
|
+
}
|
|
1672
|
+
const resolvedSchema = schema ?? (kind === "river" ? "public" : "pgboss");
|
|
1673
|
+
return new _PostgresQueueBackend(kind, resolvedSchema, options.readOnly, runner);
|
|
1674
|
+
}
|
|
1675
|
+
async listQueues() {
|
|
1676
|
+
if (this.kind === "river") {
|
|
1677
|
+
const out2 = this.runner(buildRiverQueueStatsSql(this.schema), true);
|
|
1678
|
+
return parseRiverQueueStats(out2);
|
|
1679
|
+
}
|
|
1680
|
+
const out = this.runner(buildPgBossQueueStatsSql(this.schema), true);
|
|
1681
|
+
return parsePgBossQueueStats(out);
|
|
1682
|
+
}
|
|
1683
|
+
async listJobs(queue, state, limit = 25) {
|
|
1684
|
+
if (this.kind === "river") {
|
|
1685
|
+
const out2 = this.runner(buildRiverJobsSql(this.schema, queue, state, limit), true);
|
|
1686
|
+
return parseRiverJobs(out2, queue);
|
|
1687
|
+
}
|
|
1688
|
+
const out = this.runner(buildPgBossJobsSql(this.schema, queue, state, limit), true);
|
|
1689
|
+
return parsePgBossJobs(out, queue);
|
|
1690
|
+
}
|
|
1691
|
+
async getJob(queue, jobId) {
|
|
1692
|
+
if (this.kind === "river") {
|
|
1693
|
+
const out2 = this.runner(buildRiverGetJobSql(this.schema, queue, jobId), true);
|
|
1694
|
+
return parseRiverJobs(out2, queue)[0] ?? null;
|
|
1695
|
+
}
|
|
1696
|
+
const out = this.runner(buildPgBossGetJobSql(this.schema, queue, jobId), true);
|
|
1697
|
+
return parsePgBossJobs(out, queue)[0] ?? null;
|
|
1698
|
+
}
|
|
1699
|
+
async retryJob(queue, jobId) {
|
|
1700
|
+
if (this.readOnly) {
|
|
1701
|
+
throw new ReadOnlyError(`retry job ${jobId} in queue ${queue}`);
|
|
1702
|
+
}
|
|
1703
|
+
const sql = this.kind === "river" ? buildRiverRetryJobSql(this.schema, queue, jobId) : buildPgBossRetryJobSql(this.schema, queue, jobId);
|
|
1704
|
+
const out = this.runner(sql, false);
|
|
1705
|
+
const jobIds = parseReturnedJobIds(out);
|
|
1706
|
+
return { queue, retried: jobIds.length, jobIds };
|
|
1707
|
+
}
|
|
1708
|
+
async retryFailedBulk(queue, limit = 100) {
|
|
1709
|
+
if (this.readOnly) {
|
|
1710
|
+
throw new ReadOnlyError(`bulk retry failed jobs in queue ${queue}`);
|
|
1711
|
+
}
|
|
1712
|
+
const sql = this.kind === "river" ? buildRiverRetryFailedBulkSql(this.schema, queue, limit) : buildPgBossRetryFailedBulkSql(this.schema, queue, limit);
|
|
1713
|
+
const out = this.runner(sql, false);
|
|
1714
|
+
const jobIds = parseReturnedJobIds(out);
|
|
1715
|
+
return { queue, retried: jobIds.length, jobIds };
|
|
1716
|
+
}
|
|
1717
|
+
async close() {
|
|
1718
|
+
}
|
|
1719
|
+
};
|
|
1720
|
+
|
|
1721
|
+
// src/render/dashboard.ts
|
|
1722
|
+
var SPARKLINE_BLOCKS = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"];
|
|
1723
|
+
var ANSI = {
|
|
1724
|
+
reset: "\x1B[0m",
|
|
1725
|
+
bold: "\x1B[1m",
|
|
1726
|
+
dim: "\x1B[2m",
|
|
1727
|
+
red: "\x1B[31m",
|
|
1728
|
+
green: "\x1B[32m",
|
|
1729
|
+
yellow: "\x1B[33m",
|
|
1730
|
+
cyan: "\x1B[36m",
|
|
1731
|
+
white: "\x1B[37m"
|
|
1732
|
+
};
|
|
1733
|
+
function sanitizeTerminalText(raw) {
|
|
1734
|
+
return raw.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, "").replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "").replace(/\x1b[@-Z\\-_]/g, "").replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g, "");
|
|
1735
|
+
}
|
|
1736
|
+
function sanitizeSingleLine(raw) {
|
|
1737
|
+
return sanitizeTerminalText(raw).replace(/[\r\n]+/g, " ");
|
|
1738
|
+
}
|
|
1739
|
+
function style(text, codes, enabled) {
|
|
1740
|
+
return enabled ? `${codes}${text}${ANSI.reset}` : text;
|
|
1741
|
+
}
|
|
1742
|
+
function renderSparkline(values) {
|
|
1743
|
+
if (!values || values.length === 0) {
|
|
1744
|
+
return "--------";
|
|
1745
|
+
}
|
|
1746
|
+
const clean = values.map((v) => Number.isFinite(v) && v >= 0 ? v : 0);
|
|
1747
|
+
const min = Math.min(...clean);
|
|
1748
|
+
const max = Math.max(...clean);
|
|
1749
|
+
if (max === min) {
|
|
1750
|
+
const block = max === 0 ? SPARKLINE_BLOCKS[0] : SPARKLINE_BLOCKS[3];
|
|
1751
|
+
return block.repeat(clean.length);
|
|
1752
|
+
}
|
|
1753
|
+
const range = max - min;
|
|
1754
|
+
return clean.map((v) => {
|
|
1755
|
+
const normalized = (v - min) / range;
|
|
1756
|
+
const idx = Math.min(
|
|
1757
|
+
SPARKLINE_BLOCKS.length - 1,
|
|
1758
|
+
Math.max(0, Math.round(normalized * (SPARKLINE_BLOCKS.length - 1)))
|
|
1759
|
+
);
|
|
1760
|
+
return SPARKLINE_BLOCKS[idx] ?? SPARKLINE_BLOCKS[0];
|
|
1761
|
+
}).join("");
|
|
1762
|
+
}
|
|
1763
|
+
function renderSafetyBadge(readOnly, color = false) {
|
|
1764
|
+
if (readOnly) {
|
|
1765
|
+
return style("[READ-ONLY]", `${ANSI.bold}${ANSI.green}`, color);
|
|
1766
|
+
}
|
|
1767
|
+
return style("[WRITE ENABLED]", `${ANSI.bold}${ANSI.yellow}`, color);
|
|
1768
|
+
}
|
|
1769
|
+
function padRight(text, width) {
|
|
1770
|
+
return text.length >= width ? text : text + " ".repeat(width - text.length);
|
|
1771
|
+
}
|
|
1772
|
+
function padLeft(text, width) {
|
|
1773
|
+
return text.length >= width ? text : " ".repeat(width - text.length) + text;
|
|
1774
|
+
}
|
|
1775
|
+
function renderQueueTable(queues, options = {}) {
|
|
1776
|
+
const color = options.color ?? false;
|
|
1777
|
+
if (queues.length === 0) {
|
|
1778
|
+
return " (no queues discovered)";
|
|
1779
|
+
}
|
|
1780
|
+
const headers = ["QUEUE", "BACKEND", "WAIT", "ACTIVE", "DELAYED", "DONE", "FAILED", "TREND"];
|
|
1781
|
+
const rows = queues.map((q) => {
|
|
1782
|
+
const safeName = sanitizeSingleLine(q.name);
|
|
1783
|
+
const nameLabel = q.paused ? `${safeName} (paused)` : safeName;
|
|
1784
|
+
const trend = renderSparkline(q.throughputHistory);
|
|
1785
|
+
return {
|
|
1786
|
+
raw: [
|
|
1787
|
+
nameLabel,
|
|
1788
|
+
q.backend,
|
|
1789
|
+
String(q.waiting),
|
|
1790
|
+
String(q.active),
|
|
1791
|
+
String(q.delayed),
|
|
1792
|
+
String(q.completed),
|
|
1793
|
+
String(q.failed),
|
|
1794
|
+
trend
|
|
1795
|
+
],
|
|
1796
|
+
failed: q.failed
|
|
1797
|
+
};
|
|
1798
|
+
});
|
|
1799
|
+
const totals = queues.reduce(
|
|
1800
|
+
(acc, q) => ({
|
|
1801
|
+
waiting: acc.waiting + q.waiting,
|
|
1802
|
+
active: acc.active + q.active,
|
|
1803
|
+
delayed: acc.delayed + q.delayed,
|
|
1804
|
+
completed: acc.completed + q.completed,
|
|
1805
|
+
failed: acc.failed + q.failed
|
|
1806
|
+
}),
|
|
1807
|
+
{ waiting: 0, active: 0, delayed: 0, completed: 0, failed: 0 }
|
|
1808
|
+
);
|
|
1809
|
+
const totalRow = [
|
|
1810
|
+
`TOTAL (${queues.length})`,
|
|
1811
|
+
"",
|
|
1812
|
+
String(totals.waiting),
|
|
1813
|
+
String(totals.active),
|
|
1814
|
+
String(totals.delayed),
|
|
1815
|
+
String(totals.completed),
|
|
1816
|
+
String(totals.failed),
|
|
1817
|
+
""
|
|
1818
|
+
];
|
|
1819
|
+
const widths = headers.map((header, idx) => {
|
|
1820
|
+
const dataMax = Math.max(
|
|
1821
|
+
...rows.map((r) => (r.raw[idx] ?? "").length),
|
|
1822
|
+
(totalRow[idx] ?? "").length
|
|
1823
|
+
);
|
|
1824
|
+
return Math.max(header.length, dataMax);
|
|
1825
|
+
});
|
|
1826
|
+
const rightAlignCols = /* @__PURE__ */ new Set([2, 3, 4, 5, 6]);
|
|
1827
|
+
const formatCells = (cells, failedCount = 0, isHeader = false) => {
|
|
1828
|
+
const formatted = cells.map((cell, idx) => {
|
|
1829
|
+
const width = widths[idx] ?? cell.length;
|
|
1830
|
+
const padded = rightAlignCols.has(idx) ? padLeft(cell, width) : padRight(cell, width);
|
|
1831
|
+
if (isHeader) {
|
|
1832
|
+
return style(padded, `${ANSI.bold}${ANSI.cyan}`, color);
|
|
1833
|
+
}
|
|
1834
|
+
if (idx === 6 && failedCount > 0) {
|
|
1835
|
+
return style(padded, `${ANSI.bold}${ANSI.red}`, color);
|
|
1836
|
+
}
|
|
1837
|
+
if (idx === 7 && cell.trim().length > 0) {
|
|
1838
|
+
return style(padded, ANSI.cyan, color);
|
|
1839
|
+
}
|
|
1840
|
+
return padded;
|
|
1841
|
+
});
|
|
1842
|
+
return ` ${formatted.join(" ")}`;
|
|
1843
|
+
};
|
|
1844
|
+
const ruleWidth = widths.reduce((sum, w) => sum + w, 0) + (widths.length - 1) * 2;
|
|
1845
|
+
const divider = ` ${"-".repeat(ruleWidth)}`;
|
|
1846
|
+
const lines = [
|
|
1847
|
+
formatCells(headers, 0, true),
|
|
1848
|
+
style(divider, ANSI.dim, color),
|
|
1849
|
+
...rows.map((r) => formatCells(r.raw, r.failed, false)),
|
|
1850
|
+
style(divider, ANSI.dim, color),
|
|
1851
|
+
formatCells(totalRow, totals.failed, false)
|
|
1852
|
+
];
|
|
1853
|
+
return lines.join("\n");
|
|
1854
|
+
}
|
|
1855
|
+
function formatTimestamp(ms) {
|
|
1856
|
+
if (ms === void 0 || !Number.isFinite(ms)) {
|
|
1857
|
+
return "n/a";
|
|
1858
|
+
}
|
|
1859
|
+
return new Date(ms).toISOString();
|
|
1860
|
+
}
|
|
1861
|
+
function renderJobInspector(jobs, queueName, state, totalInState, options = {}) {
|
|
1862
|
+
const color = options.color ?? false;
|
|
1863
|
+
const maxStackLines = options.maxStackLines ?? 6;
|
|
1864
|
+
const safeQueue = sanitizeSingleLine(queueName);
|
|
1865
|
+
const countSuffix = totalInState !== void 0 && totalInState > jobs.length ? ` (showing ${jobs.length} of ${totalInState} ${state} jobs)` : ` (${jobs.length} ${state} ${jobs.length === 1 ? "job" : "jobs"})`;
|
|
1866
|
+
const heading = style(
|
|
1867
|
+
`DLQ / Job Inspector: ${safeQueue}${countSuffix}`,
|
|
1868
|
+
`${ANSI.bold}${ANSI.white}`,
|
|
1869
|
+
color
|
|
1870
|
+
);
|
|
1871
|
+
if (jobs.length === 0) {
|
|
1872
|
+
return `${heading}
|
|
1873
|
+
(no ${state} jobs found in "${safeQueue}")`;
|
|
1874
|
+
}
|
|
1875
|
+
const blocks = jobs.map((job, idx) => {
|
|
1876
|
+
const lines = [];
|
|
1877
|
+
const attempts = job.maxAttempts !== void 0 ? `${job.attemptsMade}/${job.maxAttempts}` : String(job.attemptsMade);
|
|
1878
|
+
const jobHeader = ` #${idx + 1} id=${sanitizeSingleLine(job.id)} name=${sanitizeSingleLine(job.name)} state=${job.state} attempts=${attempts}`;
|
|
1879
|
+
lines.push(style(jobHeader, ANSI.bold, color));
|
|
1880
|
+
if (job.failedReason) {
|
|
1881
|
+
lines.push(` reason: ${style(sanitizeSingleLine(job.failedReason), ANSI.red, color)}`);
|
|
1882
|
+
}
|
|
1883
|
+
lines.push(
|
|
1884
|
+
style(
|
|
1885
|
+
` created=${formatTimestamp(job.timestamp)} finished=${formatTimestamp(job.finishedOn)}`,
|
|
1886
|
+
ANSI.dim,
|
|
1887
|
+
color
|
|
1888
|
+
)
|
|
1889
|
+
);
|
|
1890
|
+
const prettyData = sanitizeTerminalText(JSON.stringify(job.data ?? {}, null, 2)).split("\n").map((line) => ` ${line}`).join("\n");
|
|
1891
|
+
lines.push(" payload:");
|
|
1892
|
+
lines.push(prettyData);
|
|
1893
|
+
if (job.stacktrace && job.stacktrace.length > 0) {
|
|
1894
|
+
lines.push(" stacktrace:");
|
|
1895
|
+
const flatLines = job.stacktrace.flatMap((entry) => entry.split(/\r?\n/));
|
|
1896
|
+
const trimmed = flatLines.slice(0, maxStackLines);
|
|
1897
|
+
for (const traceLine of trimmed) {
|
|
1898
|
+
lines.push(` ${style(sanitizeSingleLine(traceLine), ANSI.dim, color)}`);
|
|
1899
|
+
}
|
|
1900
|
+
if (flatLines.length > maxStackLines) {
|
|
1901
|
+
lines.push(
|
|
1902
|
+
` ${style(`... (${flatLines.length - maxStackLines} more lines)`, ANSI.dim, color)}`
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
return lines.join("\n");
|
|
1907
|
+
});
|
|
1908
|
+
return `${heading}
|
|
1909
|
+
${blocks.join("\n\n")}`;
|
|
1910
|
+
}
|
|
1911
|
+
function renderDashboard(snapshot, options = {}) {
|
|
1912
|
+
const color = options.color ?? false;
|
|
1913
|
+
const badge = renderSafetyBadge(snapshot.readOnly, color);
|
|
1914
|
+
const title = style(`q9s v${snapshot.version}`, `${ANSI.bold}${ANSI.cyan}`, color);
|
|
1915
|
+
const safeTarget = sanitizeSingleLine(snapshot.target);
|
|
1916
|
+
const safeCtx = snapshot.contextName ? sanitizeSingleLine(snapshot.contextName) : void 0;
|
|
1917
|
+
const headerLines = [
|
|
1918
|
+
`${title} ${badge}`,
|
|
1919
|
+
` target: ${safeTarget}${safeCtx ? ` (ctx: ${safeCtx})` : ""}`,
|
|
1920
|
+
` backend: ${snapshot.backend} captured: ${snapshot.capturedAt}`
|
|
1921
|
+
];
|
|
1922
|
+
const sections = [
|
|
1923
|
+
headerLines.join("\n"),
|
|
1924
|
+
"",
|
|
1925
|
+
renderQueueTable(snapshot.queues, options)
|
|
1926
|
+
];
|
|
1927
|
+
if (snapshot.inspectedQueue) {
|
|
1928
|
+
const queueStat = snapshot.queues.find((q) => q.name === snapshot.inspectedQueue);
|
|
1929
|
+
const state = snapshot.inspectedState ?? "failed";
|
|
1930
|
+
const totalInState = queueStat ? state === "failed" ? queueStat.failed : state === "waiting" ? queueStat.waiting : state === "active" ? queueStat.active : state === "delayed" ? queueStat.delayed : queueStat.completed : void 0;
|
|
1931
|
+
sections.push(
|
|
1932
|
+
"",
|
|
1933
|
+
renderJobInspector(
|
|
1934
|
+
snapshot.inspectedJobs,
|
|
1935
|
+
snapshot.inspectedQueue,
|
|
1936
|
+
state,
|
|
1937
|
+
totalInState,
|
|
1938
|
+
options
|
|
1939
|
+
)
|
|
1940
|
+
);
|
|
1941
|
+
}
|
|
1942
|
+
const footer = snapshot.readOnly ? "Safety: read-only mode is active by default. Pass --allow-write to unlock --retry or --retry-failed." : "Warning: write mode is enabled (--allow-write). Mutations will modify live queue state.";
|
|
1943
|
+
sections.push("", style(footer, ANSI.dim, color));
|
|
1944
|
+
return sections.join("\n");
|
|
1945
|
+
}
|
|
1946
|
+
function renderJson(payload) {
|
|
1947
|
+
return JSON.stringify(payload, null, 2);
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
// src/demo/fixture.ts
|
|
1951
|
+
import net2 from "node:net";
|
|
1952
|
+
function createEmptyStore() {
|
|
1953
|
+
return {
|
|
1954
|
+
hashes: /* @__PURE__ */ new Map(),
|
|
1955
|
+
lists: /* @__PURE__ */ new Map(),
|
|
1956
|
+
zsets: /* @__PURE__ */ new Map(),
|
|
1957
|
+
strings: /* @__PURE__ */ new Map()
|
|
1958
|
+
};
|
|
1959
|
+
}
|
|
1960
|
+
function sanitizeRespLine(text) {
|
|
1961
|
+
return text.replace(/[\r\n]+/g, " ");
|
|
1962
|
+
}
|
|
1963
|
+
function encodeRespReply(value) {
|
|
1964
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
1965
|
+
if ("error" in value) {
|
|
1966
|
+
return Buffer.from(`-${sanitizeRespLine(value.error)}\r
|
|
1967
|
+
`, "utf8");
|
|
1968
|
+
}
|
|
1969
|
+
if ("simple" in value) {
|
|
1970
|
+
return Buffer.from(`+${sanitizeRespLine(value.simple)}\r
|
|
1971
|
+
`, "utf8");
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
if (value === null) {
|
|
1975
|
+
return Buffer.from("$-1\r\n", "utf8");
|
|
1976
|
+
}
|
|
1977
|
+
if (typeof value === "number") {
|
|
1978
|
+
return Buffer.from(`:${Math.trunc(value)}\r
|
|
1979
|
+
`, "utf8");
|
|
1980
|
+
}
|
|
1981
|
+
if (typeof value === "string") {
|
|
1982
|
+
const bytes = Buffer.from(value, "utf8");
|
|
1983
|
+
return Buffer.concat([Buffer.from(`$${bytes.length}\r
|
|
1984
|
+
`, "utf8"), bytes, Buffer.from("\r\n", "utf8")]);
|
|
1985
|
+
}
|
|
1986
|
+
if (Array.isArray(value)) {
|
|
1987
|
+
const parts = [Buffer.from(`*${value.length}\r
|
|
1988
|
+
`, "utf8")];
|
|
1989
|
+
for (const item of value) {
|
|
1990
|
+
parts.push(encodeRespReply(item));
|
|
1991
|
+
}
|
|
1992
|
+
return Buffer.concat(parts);
|
|
1993
|
+
}
|
|
1994
|
+
return Buffer.from("$-1\r\n", "utf8");
|
|
1995
|
+
}
|
|
1996
|
+
function globToRegExp(pattern) {
|
|
1997
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
1998
|
+
const regexStr = `^${escaped.replace(/\*/g, ".*").replace(/\?/g, ".")}$`;
|
|
1999
|
+
return new RegExp(regexStr);
|
|
2000
|
+
}
|
|
2001
|
+
function getAllKeys(store) {
|
|
2002
|
+
const keys = /* @__PURE__ */ new Set();
|
|
2003
|
+
for (const k of store.hashes.keys()) keys.add(k);
|
|
2004
|
+
for (const k of store.lists.keys()) keys.add(k);
|
|
2005
|
+
for (const k of store.zsets.keys()) keys.add(k);
|
|
2006
|
+
for (const k of store.strings.keys()) keys.add(k);
|
|
2007
|
+
return Array.from(keys).sort();
|
|
2008
|
+
}
|
|
2009
|
+
function normalizeSlice(length, start, stop) {
|
|
2010
|
+
const s = start < 0 ? Math.max(0, length + start) : start;
|
|
2011
|
+
const e = stop < 0 ? length + stop : Math.min(length - 1, stop);
|
|
2012
|
+
return [s, e];
|
|
2013
|
+
}
|
|
2014
|
+
function sortedZsetMembers(zset, reverse = false) {
|
|
2015
|
+
const entries = Array.from(zset.entries());
|
|
2016
|
+
entries.sort((a, b) => {
|
|
2017
|
+
if (a[1] !== b[1]) {
|
|
2018
|
+
return reverse ? b[1] - a[1] : a[1] - b[1];
|
|
2019
|
+
}
|
|
2020
|
+
return reverse ? b[0].localeCompare(a[0]) : a[0].localeCompare(b[0]);
|
|
2021
|
+
});
|
|
2022
|
+
return entries.map(([member]) => member);
|
|
2023
|
+
}
|
|
2024
|
+
function seedQueueCounters(store, prefix, queue, counts) {
|
|
2025
|
+
const meta = /* @__PURE__ */ new Map([
|
|
2026
|
+
["opts.maxLen.count", "10000"],
|
|
2027
|
+
["paused", counts.paused ? "1" : "0"]
|
|
2028
|
+
]);
|
|
2029
|
+
store.hashes.set(`${prefix}:${queue}:meta`, meta);
|
|
2030
|
+
store.strings.set(`${prefix}:${queue}:id`, String(counts.completed + counts.waiting + 100));
|
|
2031
|
+
const waitList = [];
|
|
2032
|
+
for (let i = 1; i <= counts.waiting; i++) {
|
|
2033
|
+
waitList.push(`${queue}-wait-${i}`);
|
|
2034
|
+
}
|
|
2035
|
+
store.lists.set(`${prefix}:${queue}:wait`, waitList);
|
|
2036
|
+
const activeList = [];
|
|
2037
|
+
for (let i = 1; i <= counts.active; i++) {
|
|
2038
|
+
activeList.push(`${queue}-active-${i}`);
|
|
2039
|
+
}
|
|
2040
|
+
store.lists.set(`${prefix}:${queue}:active`, activeList);
|
|
2041
|
+
const delayedSet = /* @__PURE__ */ new Map();
|
|
2042
|
+
const baseNow = Date.UTC(2026, 10, 21, 19, 0, 0);
|
|
2043
|
+
for (let i = 1; i <= counts.delayed; i++) {
|
|
2044
|
+
delayedSet.set(`${queue}-delayed-${i}`, baseNow + i * 6e4);
|
|
2045
|
+
}
|
|
2046
|
+
store.zsets.set(`${prefix}:${queue}:delayed`, delayedSet);
|
|
2047
|
+
const completedSet = /* @__PURE__ */ new Map();
|
|
2048
|
+
for (let i = 1; i <= counts.completed; i++) {
|
|
2049
|
+
completedSet.set(`${queue}-done-${i}`, baseNow - i * 1e3);
|
|
2050
|
+
}
|
|
2051
|
+
store.zsets.set(`${prefix}:${queue}:completed`, completedSet);
|
|
2052
|
+
store.lists.set(
|
|
2053
|
+
`${prefix}:${queue}:metrics:completed:data`,
|
|
2054
|
+
[...counts.metrics].reverse().map(String)
|
|
2055
|
+
);
|
|
2056
|
+
}
|
|
2057
|
+
function addFailedJob(store, prefix, queue, job) {
|
|
2058
|
+
const failedKey = `${prefix}:${queue}:failed`;
|
|
2059
|
+
let zset = store.zsets.get(failedKey);
|
|
2060
|
+
if (!zset) {
|
|
2061
|
+
zset = /* @__PURE__ */ new Map();
|
|
2062
|
+
store.zsets.set(failedKey, zset);
|
|
2063
|
+
}
|
|
2064
|
+
zset.set(job.id, job.finishedOn);
|
|
2065
|
+
const hash = /* @__PURE__ */ new Map([
|
|
2066
|
+
["name", job.name],
|
|
2067
|
+
["data", JSON.stringify(job.data)],
|
|
2068
|
+
["opts", JSON.stringify({ attempts: job.maxAttempts, backoff: { type: "exponential", delay: 2e3 } })],
|
|
2069
|
+
["failedReason", job.failedReason],
|
|
2070
|
+
["stacktrace", JSON.stringify(job.stacktrace)],
|
|
2071
|
+
["attemptsMade", String(job.attemptsMade)],
|
|
2072
|
+
["timestamp", String(job.timestamp)],
|
|
2073
|
+
["processedOn", String(job.processedOn)],
|
|
2074
|
+
["finishedOn", String(job.finishedOn)]
|
|
2075
|
+
]);
|
|
2076
|
+
store.hashes.set(`${prefix}:${queue}:${job.id}`, hash);
|
|
2077
|
+
}
|
|
2078
|
+
function seedBullMqDemoData(store, prefix = "bull") {
|
|
2079
|
+
const baseTs = Date.UTC(2026, 10, 21, 18, 45, 0);
|
|
2080
|
+
seedQueueCounters(store, prefix, "emails", {
|
|
2081
|
+
waiting: 12,
|
|
2082
|
+
active: 4,
|
|
2083
|
+
delayed: 8,
|
|
2084
|
+
completed: 1482,
|
|
2085
|
+
metrics: [42, 46, 51, 49, 54, 58, 55, 61, 59, 64, 62, 66]
|
|
2086
|
+
});
|
|
2087
|
+
addFailedJob(store, prefix, "emails", {
|
|
2088
|
+
id: "email-9041",
|
|
2089
|
+
name: "send-receipt",
|
|
2090
|
+
data: {
|
|
2091
|
+
to: "finance@acme-example.test",
|
|
2092
|
+
template: "invoice-paid-v3",
|
|
2093
|
+
invoiceId: "inv_01J9E3K8"
|
|
2094
|
+
},
|
|
2095
|
+
failedReason: "SMTP 451 4.3.0 Temporary local problem, please try later",
|
|
2096
|
+
stacktrace: [
|
|
2097
|
+
"Error: SMTP 451 4.3.0 Temporary local problem, please try later",
|
|
2098
|
+
" at SmtpTransport.send (/app/src/mail/smtp.ts:118:17)",
|
|
2099
|
+
" at processEmailJob (/app/src/workers/emails.ts:44:9)"
|
|
2100
|
+
],
|
|
2101
|
+
attemptsMade: 3,
|
|
2102
|
+
maxAttempts: 3,
|
|
2103
|
+
timestamp: baseTs - 32e4,
|
|
2104
|
+
processedOn: baseTs - 31e4,
|
|
2105
|
+
finishedOn: baseTs - 305e3
|
|
2106
|
+
});
|
|
2107
|
+
seedQueueCounters(store, prefix, "webhooks", {
|
|
2108
|
+
waiting: 37,
|
|
2109
|
+
active: 6,
|
|
2110
|
+
delayed: 19,
|
|
2111
|
+
completed: 942,
|
|
2112
|
+
metrics: [74, 71, 66, 59, 48, 37, 26, 18, 14, 11, 9, 7]
|
|
2113
|
+
});
|
|
2114
|
+
const webhookEndpoints = [
|
|
2115
|
+
"https://hooks.partner-a.example/v2/events",
|
|
2116
|
+
"https://api.merchant-sync.example/webhooks/orders",
|
|
2117
|
+
"https://ingest.northwind.example/stripe-relay"
|
|
2118
|
+
];
|
|
2119
|
+
const webhookEvents = [
|
|
2120
|
+
"order.fulfilled",
|
|
2121
|
+
"subscription.renewed",
|
|
2122
|
+
"payment.captured",
|
|
2123
|
+
"invoice.finalized"
|
|
2124
|
+
];
|
|
2125
|
+
for (let i = 1; i <= 14; i++) {
|
|
2126
|
+
const jobNum = 4800 + i;
|
|
2127
|
+
const endpoint = webhookEndpoints[(i - 1) % webhookEndpoints.length] ?? webhookEndpoints[0];
|
|
2128
|
+
const webhookEvent = webhookEvents[(i - 1) % webhookEvents.length] ?? webhookEvents[0];
|
|
2129
|
+
const isConnReset = i % 3 !== 0;
|
|
2130
|
+
const reason = isConnReset ? `read ECONNRESET while posting ${webhookEvent} to ${endpoint}` : `Upstream returned HTTP 502 Bad Gateway from ${endpoint}`;
|
|
2131
|
+
const finishedOn = baseTs + i * 12e3;
|
|
2132
|
+
addFailedJob(store, prefix, "webhooks", {
|
|
2133
|
+
id: `wh-${jobNum}`,
|
|
2134
|
+
name: "deliver-webhook",
|
|
2135
|
+
data: {
|
|
2136
|
+
eventId: `evt_2026_11_${jobNum}`,
|
|
2137
|
+
webhookEvent,
|
|
2138
|
+
endpoint,
|
|
2139
|
+
customerId: `cus_${100 + i}`,
|
|
2140
|
+
attemptDurationMs: isConnReset ? 5012 : 1840,
|
|
2141
|
+
httpStatus: isConnReset ? null : 502
|
|
2142
|
+
},
|
|
2143
|
+
failedReason: reason,
|
|
2144
|
+
stacktrace: isConnReset ? [
|
|
2145
|
+
`Error: ${reason}`,
|
|
2146
|
+
" at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20)",
|
|
2147
|
+
" at WebhookDispatcher.post (/app/src/webhooks/dispatcher.ts:89:15)",
|
|
2148
|
+
" at Worker.processJob (/app/src/workers/webhooks.ts:52:11)"
|
|
2149
|
+
] : [
|
|
2150
|
+
`Error: ${reason}`,
|
|
2151
|
+
" at assertOkResponse (/app/src/webhooks/http.ts:61:11)",
|
|
2152
|
+
" at WebhookDispatcher.post (/app/src/webhooks/dispatcher.ts:94:7)",
|
|
2153
|
+
" at Worker.processJob (/app/src/workers/webhooks.ts:52:11)"
|
|
2154
|
+
],
|
|
2155
|
+
attemptsMade: 5,
|
|
2156
|
+
maxAttempts: 5,
|
|
2157
|
+
timestamp: finishedOn - 25e3,
|
|
2158
|
+
processedOn: finishedOn - 5e3,
|
|
2159
|
+
finishedOn
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
seedQueueCounters(store, prefix, "billing-sync", {
|
|
2163
|
+
waiting: 5,
|
|
2164
|
+
active: 2,
|
|
2165
|
+
delayed: 3,
|
|
2166
|
+
completed: 618,
|
|
2167
|
+
metrics: [19, 22, 21, 24, 23, 25, 22, 26, 24, 25, 23, 27]
|
|
2168
|
+
});
|
|
2169
|
+
addFailedJob(store, prefix, "billing-sync", {
|
|
2170
|
+
id: "bill-7701",
|
|
2171
|
+
name: "reconcile-invoice",
|
|
2172
|
+
data: {
|
|
2173
|
+
accountId: "acct_8841",
|
|
2174
|
+
invoiceId: "in_1Qx92A",
|
|
2175
|
+
currency: "USD",
|
|
2176
|
+
amountCents: 24900
|
|
2177
|
+
},
|
|
2178
|
+
failedReason: "StripeRateLimitError: 429 Too Many Requests on /v1/invoices/in_1Qx92A",
|
|
2179
|
+
stacktrace: [
|
|
2180
|
+
"StripeRateLimitError: 429 Too Many Requests on /v1/invoices/in_1Qx92A",
|
|
2181
|
+
" at BillingClient.fetchInvoice (/app/src/billing/client.ts:74:13)",
|
|
2182
|
+
" at reconcileInvoice (/app/src/workers/billing-sync.ts:39:18)"
|
|
2183
|
+
],
|
|
2184
|
+
attemptsMade: 4,
|
|
2185
|
+
maxAttempts: 4,
|
|
2186
|
+
timestamp: baseTs - 9e4,
|
|
2187
|
+
processedOn: baseTs - 84e3,
|
|
2188
|
+
finishedOn: baseTs - 82e3
|
|
2189
|
+
});
|
|
2190
|
+
addFailedJob(store, prefix, "billing-sync", {
|
|
2191
|
+
id: "bill-7702",
|
|
2192
|
+
name: "sync-usage-meter",
|
|
2193
|
+
data: {
|
|
2194
|
+
accountId: "acct_9912",
|
|
2195
|
+
meterId: "mtr_api_calls",
|
|
2196
|
+
quantity: 184200,
|
|
2197
|
+
period: "2026-11"
|
|
2198
|
+
},
|
|
2199
|
+
failedReason: "ConflictError: usage ledger lock timeout after 3000ms",
|
|
2200
|
+
stacktrace: [
|
|
2201
|
+
"ConflictError: usage ledger lock timeout after 3000ms",
|
|
2202
|
+
" at LedgerStore.commitUsage (/app/src/billing/ledger.ts:103:11)",
|
|
2203
|
+
" at syncUsageMeter (/app/src/workers/billing-sync.ts:77:9)"
|
|
2204
|
+
],
|
|
2205
|
+
attemptsMade: 3,
|
|
2206
|
+
maxAttempts: 3,
|
|
2207
|
+
timestamp: baseTs - 4e4,
|
|
2208
|
+
processedOn: baseTs - 35e3,
|
|
2209
|
+
finishedOn: baseTs - 32e3
|
|
2210
|
+
});
|
|
2211
|
+
}
|
|
2212
|
+
function executeCommand(store, args, authPassword, session) {
|
|
2213
|
+
const cmd = (args[0] ?? "").toUpperCase();
|
|
2214
|
+
if (cmd === "AUTH") {
|
|
2215
|
+
const provided = args.length >= 3 ? args[2] : args[1];
|
|
2216
|
+
if (authPassword === void 0) {
|
|
2217
|
+
return { simple: "OK" };
|
|
2218
|
+
}
|
|
2219
|
+
if (provided === authPassword) {
|
|
2220
|
+
session.authenticated = true;
|
|
2221
|
+
return { simple: "OK" };
|
|
2222
|
+
}
|
|
2223
|
+
return { error: "WRONGPASS invalid username-password pair or user is disabled." };
|
|
2224
|
+
}
|
|
2225
|
+
if (authPassword !== void 0 && !session.authenticated && cmd !== "QUIT") {
|
|
2226
|
+
return { error: "NOAUTH Authentication required." };
|
|
2227
|
+
}
|
|
2228
|
+
switch (cmd) {
|
|
2229
|
+
case "PING":
|
|
2230
|
+
return args[1] !== void 0 ? args[1] : { simple: "PONG" };
|
|
2231
|
+
case "QUIT":
|
|
2232
|
+
return { simple: "OK" };
|
|
2233
|
+
case "SELECT": {
|
|
2234
|
+
const db = Number.parseInt(args[1] ?? "0", 10);
|
|
2235
|
+
if (!Number.isInteger(db) || db < 0) {
|
|
2236
|
+
return { error: "ERR invalid DB index" };
|
|
2237
|
+
}
|
|
2238
|
+
session.db = db;
|
|
2239
|
+
return { simple: "OK" };
|
|
2240
|
+
}
|
|
2241
|
+
case "SCAN": {
|
|
2242
|
+
const cursor = Number.parseInt(args[1] ?? "0", 10) || 0;
|
|
2243
|
+
let matchPattern = "*";
|
|
2244
|
+
let count = 100;
|
|
2245
|
+
for (let i = 2; i < args.length; i++) {
|
|
2246
|
+
const token = (args[i] ?? "").toUpperCase();
|
|
2247
|
+
if (token === "MATCH" && args[i + 1] !== void 0) {
|
|
2248
|
+
matchPattern = args[i + 1];
|
|
2249
|
+
i++;
|
|
2250
|
+
} else if (token === "COUNT" && args[i + 1] !== void 0) {
|
|
2251
|
+
count = Math.max(1, Number.parseInt(args[i + 1], 10) || 100);
|
|
2252
|
+
i++;
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
const re = globToRegExp(matchPattern);
|
|
2256
|
+
const allKeys = getAllKeys(store);
|
|
2257
|
+
const end = Math.min(allKeys.length, cursor + count);
|
|
2258
|
+
const slice = allKeys.slice(cursor, end).filter((k) => re.test(k));
|
|
2259
|
+
const nextCursor = end >= allKeys.length ? "0" : String(end);
|
|
2260
|
+
return [nextCursor, slice];
|
|
2261
|
+
}
|
|
2262
|
+
case "KEYS": {
|
|
2263
|
+
const re = globToRegExp(args[1] ?? "*");
|
|
2264
|
+
return getAllKeys(store).filter((k) => re.test(k));
|
|
2265
|
+
}
|
|
2266
|
+
case "GET": {
|
|
2267
|
+
const key = args[1] ?? "";
|
|
2268
|
+
return store.strings.get(key) ?? null;
|
|
2269
|
+
}
|
|
2270
|
+
case "SET": {
|
|
2271
|
+
const key = args[1] ?? "";
|
|
2272
|
+
const val = args[2] ?? "";
|
|
2273
|
+
store.strings.set(key, val);
|
|
2274
|
+
return { simple: "OK" };
|
|
2275
|
+
}
|
|
2276
|
+
case "EXISTS": {
|
|
2277
|
+
let found = 0;
|
|
2278
|
+
for (let i = 1; i < args.length; i++) {
|
|
2279
|
+
const k = args[i] ?? "";
|
|
2280
|
+
if (store.hashes.has(k) || store.lists.has(k) || store.zsets.has(k) || store.strings.has(k)) {
|
|
2281
|
+
found++;
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
return found;
|
|
2285
|
+
}
|
|
2286
|
+
case "LLEN": {
|
|
2287
|
+
const list = store.lists.get(args[1] ?? "");
|
|
2288
|
+
return list ? list.length : 0;
|
|
2289
|
+
}
|
|
2290
|
+
case "LRANGE": {
|
|
2291
|
+
const list = store.lists.get(args[1] ?? "");
|
|
2292
|
+
if (!list || list.length === 0) {
|
|
2293
|
+
return [];
|
|
2294
|
+
}
|
|
2295
|
+
const start = Number.parseInt(args[2] ?? "0", 10);
|
|
2296
|
+
const stop = Number.parseInt(args[3] ?? "-1", 10);
|
|
2297
|
+
const [s, e] = normalizeSlice(list.length, start, stop);
|
|
2298
|
+
if (s > e || s >= list.length) {
|
|
2299
|
+
return [];
|
|
2300
|
+
}
|
|
2301
|
+
return list.slice(s, e + 1);
|
|
2302
|
+
}
|
|
2303
|
+
case "LPUSH": {
|
|
2304
|
+
const key = args[1] ?? "";
|
|
2305
|
+
let list = store.lists.get(key);
|
|
2306
|
+
if (!list) {
|
|
2307
|
+
list = [];
|
|
2308
|
+
store.lists.set(key, list);
|
|
2309
|
+
}
|
|
2310
|
+
for (let i = 2; i < args.length; i++) {
|
|
2311
|
+
list.unshift(args[i] ?? "");
|
|
2312
|
+
}
|
|
2313
|
+
return list.length;
|
|
2314
|
+
}
|
|
2315
|
+
case "RPUSH": {
|
|
2316
|
+
const key = args[1] ?? "";
|
|
2317
|
+
let list = store.lists.get(key);
|
|
2318
|
+
if (!list) {
|
|
2319
|
+
list = [];
|
|
2320
|
+
store.lists.set(key, list);
|
|
2321
|
+
}
|
|
2322
|
+
for (let i = 2; i < args.length; i++) {
|
|
2323
|
+
list.push(args[i] ?? "");
|
|
2324
|
+
}
|
|
2325
|
+
return list.length;
|
|
2326
|
+
}
|
|
2327
|
+
case "ZCARD": {
|
|
2328
|
+
const zset = store.zsets.get(args[1] ?? "");
|
|
2329
|
+
return zset ? zset.size : 0;
|
|
2330
|
+
}
|
|
2331
|
+
case "ZADD": {
|
|
2332
|
+
const key = args[1] ?? "";
|
|
2333
|
+
let zset = store.zsets.get(key);
|
|
2334
|
+
if (!zset) {
|
|
2335
|
+
zset = /* @__PURE__ */ new Map();
|
|
2336
|
+
store.zsets.set(key, zset);
|
|
2337
|
+
}
|
|
2338
|
+
let added = 0;
|
|
2339
|
+
for (let i = 2; i + 1 < args.length; i += 2) {
|
|
2340
|
+
const score = Number.parseFloat(args[i] ?? "0");
|
|
2341
|
+
const member = args[i + 1] ?? "";
|
|
2342
|
+
if (!zset.has(member)) {
|
|
2343
|
+
added++;
|
|
2344
|
+
}
|
|
2345
|
+
zset.set(member, score);
|
|
2346
|
+
}
|
|
2347
|
+
return added;
|
|
2348
|
+
}
|
|
2349
|
+
case "ZSCORE": {
|
|
2350
|
+
const zset = store.zsets.get(args[1] ?? "");
|
|
2351
|
+
const score = zset?.get(args[2] ?? "");
|
|
2352
|
+
return score === void 0 ? null : String(score);
|
|
2353
|
+
}
|
|
2354
|
+
case "ZREM": {
|
|
2355
|
+
const zset = store.zsets.get(args[1] ?? "");
|
|
2356
|
+
if (!zset) {
|
|
2357
|
+
return 0;
|
|
2358
|
+
}
|
|
2359
|
+
let removed = 0;
|
|
2360
|
+
for (let i = 2; i < args.length; i++) {
|
|
2361
|
+
if (zset.delete(args[i] ?? "")) {
|
|
2362
|
+
removed++;
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
return removed;
|
|
2366
|
+
}
|
|
2367
|
+
case "ZRANGE":
|
|
2368
|
+
case "ZREVRANGE": {
|
|
2369
|
+
const zset = store.zsets.get(args[1] ?? "");
|
|
2370
|
+
if (!zset || zset.size === 0) {
|
|
2371
|
+
return [];
|
|
2372
|
+
}
|
|
2373
|
+
const ordered = sortedZsetMembers(zset, cmd === "ZREVRANGE");
|
|
2374
|
+
const start = Number.parseInt(args[2] ?? "0", 10);
|
|
2375
|
+
const stop = Number.parseInt(args[3] ?? "-1", 10);
|
|
2376
|
+
const [s, e] = normalizeSlice(ordered.length, start, stop);
|
|
2377
|
+
if (s > e || s >= ordered.length) {
|
|
2378
|
+
return [];
|
|
2379
|
+
}
|
|
2380
|
+
return ordered.slice(s, e + 1);
|
|
2381
|
+
}
|
|
2382
|
+
case "HGET": {
|
|
2383
|
+
const hash = store.hashes.get(args[1] ?? "");
|
|
2384
|
+
return hash?.get(args[2] ?? "") ?? null;
|
|
2385
|
+
}
|
|
2386
|
+
case "HGETALL": {
|
|
2387
|
+
const hash = store.hashes.get(args[1] ?? "");
|
|
2388
|
+
if (!hash) {
|
|
2389
|
+
return [];
|
|
2390
|
+
}
|
|
2391
|
+
const out = [];
|
|
2392
|
+
for (const [k, v] of hash.entries()) {
|
|
2393
|
+
out.push(k, v);
|
|
2394
|
+
}
|
|
2395
|
+
return out;
|
|
2396
|
+
}
|
|
2397
|
+
case "HSET": {
|
|
2398
|
+
const key = args[1] ?? "";
|
|
2399
|
+
let hash = store.hashes.get(key);
|
|
2400
|
+
if (!hash) {
|
|
2401
|
+
hash = /* @__PURE__ */ new Map();
|
|
2402
|
+
store.hashes.set(key, hash);
|
|
2403
|
+
}
|
|
2404
|
+
let added = 0;
|
|
2405
|
+
for (let i = 2; i + 1 < args.length; i += 2) {
|
|
2406
|
+
const field = args[i] ?? "";
|
|
2407
|
+
const val = args[i + 1] ?? "";
|
|
2408
|
+
if (!hash.has(field)) {
|
|
2409
|
+
added++;
|
|
2410
|
+
}
|
|
2411
|
+
hash.set(field, val);
|
|
2412
|
+
}
|
|
2413
|
+
return added;
|
|
2414
|
+
}
|
|
2415
|
+
case "HDEL": {
|
|
2416
|
+
const hash = store.hashes.get(args[1] ?? "");
|
|
2417
|
+
if (!hash) {
|
|
2418
|
+
return 0;
|
|
2419
|
+
}
|
|
2420
|
+
let removed = 0;
|
|
2421
|
+
for (let i = 2; i < args.length; i++) {
|
|
2422
|
+
if (hash.delete(args[i] ?? "")) {
|
|
2423
|
+
removed++;
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
return removed;
|
|
2427
|
+
}
|
|
2428
|
+
case "WATCH":
|
|
2429
|
+
case "UNWATCH":
|
|
2430
|
+
return { simple: "OK" };
|
|
2431
|
+
default:
|
|
2432
|
+
return { error: `ERR unknown command '${cmd}'` };
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
async function startMockRedisServer(options = {}) {
|
|
2436
|
+
const store = createEmptyStore();
|
|
2437
|
+
if (options.seedBullMqDemo !== false) {
|
|
2438
|
+
seedBullMqDemoData(store, options.prefix ?? "bull");
|
|
2439
|
+
}
|
|
2440
|
+
const sockets = /* @__PURE__ */ new Set();
|
|
2441
|
+
const server = net2.createServer((socket) => {
|
|
2442
|
+
sockets.add(socket);
|
|
2443
|
+
const parser = new RespParser();
|
|
2444
|
+
const session = {
|
|
2445
|
+
authenticated: options.password === void 0,
|
|
2446
|
+
db: 0,
|
|
2447
|
+
inMulti: false,
|
|
2448
|
+
txQueue: []
|
|
2449
|
+
};
|
|
2450
|
+
socket.on("close", () => {
|
|
2451
|
+
sockets.delete(socket);
|
|
2452
|
+
});
|
|
2453
|
+
socket.on("data", (chunk) => {
|
|
2454
|
+
let frames;
|
|
2455
|
+
try {
|
|
2456
|
+
frames = parser.push(chunk);
|
|
2457
|
+
} catch (err) {
|
|
2458
|
+
socket.write(encodeRespReply({ error: `ERR ${err instanceof Error ? err.message : String(err)}` }));
|
|
2459
|
+
return;
|
|
2460
|
+
}
|
|
2461
|
+
for (const frame of frames) {
|
|
2462
|
+
if (!Array.isArray(frame)) {
|
|
2463
|
+
socket.write(encodeRespReply({ error: "ERR expected array command" }));
|
|
2464
|
+
continue;
|
|
2465
|
+
}
|
|
2466
|
+
const args = frame.map((item) => String(item ?? ""));
|
|
2467
|
+
const cmd = (args[0] ?? "").toUpperCase();
|
|
2468
|
+
if (cmd === "MULTI") {
|
|
2469
|
+
session.inMulti = true;
|
|
2470
|
+
session.txQueue = [];
|
|
2471
|
+
socket.write(encodeRespReply({ simple: "OK" }));
|
|
2472
|
+
continue;
|
|
2473
|
+
}
|
|
2474
|
+
if (cmd === "DISCARD") {
|
|
2475
|
+
session.inMulti = false;
|
|
2476
|
+
session.txQueue = [];
|
|
2477
|
+
socket.write(encodeRespReply({ simple: "OK" }));
|
|
2478
|
+
continue;
|
|
2479
|
+
}
|
|
2480
|
+
if (cmd === "EXEC") {
|
|
2481
|
+
if (!session.inMulti) {
|
|
2482
|
+
socket.write(encodeRespReply({ error: "ERR EXEC without MULTI" }));
|
|
2483
|
+
continue;
|
|
2484
|
+
}
|
|
2485
|
+
session.inMulti = false;
|
|
2486
|
+
const queued = session.txQueue;
|
|
2487
|
+
session.txQueue = [];
|
|
2488
|
+
const replies = [];
|
|
2489
|
+
for (const queuedArgs of queued) {
|
|
2490
|
+
const reply2 = executeCommand(store, queuedArgs, options.password, session);
|
|
2491
|
+
if (reply2 !== null && typeof reply2 === "object" && !Array.isArray(reply2)) {
|
|
2492
|
+
if ("simple" in reply2) {
|
|
2493
|
+
replies.push(reply2.simple);
|
|
2494
|
+
} else if ("error" in reply2) {
|
|
2495
|
+
replies.push(reply2.error);
|
|
2496
|
+
}
|
|
2497
|
+
} else {
|
|
2498
|
+
replies.push(reply2);
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
socket.write(encodeRespReply(replies));
|
|
2502
|
+
continue;
|
|
2503
|
+
}
|
|
2504
|
+
if (session.inMulti) {
|
|
2505
|
+
session.txQueue.push(args);
|
|
2506
|
+
socket.write(encodeRespReply({ simple: "QUEUED" }));
|
|
2507
|
+
continue;
|
|
2508
|
+
}
|
|
2509
|
+
const reply = executeCommand(store, args, options.password, session);
|
|
2510
|
+
socket.write(encodeRespReply(reply));
|
|
2511
|
+
if (cmd === "QUIT") {
|
|
2512
|
+
socket.end();
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
});
|
|
2516
|
+
});
|
|
2517
|
+
await new Promise((resolve, reject) => {
|
|
2518
|
+
server.once("error", reject);
|
|
2519
|
+
server.listen(0, "127.0.0.1", () => {
|
|
2520
|
+
server.removeListener("error", reject);
|
|
2521
|
+
resolve();
|
|
2522
|
+
});
|
|
2523
|
+
});
|
|
2524
|
+
const addr = server.address();
|
|
2525
|
+
if (!addr || typeof addr === "string") {
|
|
2526
|
+
throw new Error("Failed to bind mock Redis TCP server.");
|
|
2527
|
+
}
|
|
2528
|
+
const host = "127.0.0.1";
|
|
2529
|
+
const port = addr.port;
|
|
2530
|
+
const authPrefix = options.password ? `:${encodeURIComponent(options.password)}@` : "";
|
|
2531
|
+
const url = `redis://${authPrefix}${host}:${port}`;
|
|
2532
|
+
return {
|
|
2533
|
+
host,
|
|
2534
|
+
port,
|
|
2535
|
+
url,
|
|
2536
|
+
close: async () => {
|
|
2537
|
+
for (const sock of sockets) {
|
|
2538
|
+
sock.destroy();
|
|
2539
|
+
}
|
|
2540
|
+
await new Promise((resolve) => {
|
|
2541
|
+
server.close(() => resolve());
|
|
2542
|
+
});
|
|
2543
|
+
}
|
|
2544
|
+
};
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
// src/cli.ts
|
|
2548
|
+
import fs2 from "node:fs";
|
|
2549
|
+
import path2 from "node:path";
|
|
2550
|
+
import { fileURLToPath } from "node:url";
|
|
2551
|
+
var Q9S_VERSION = "0.1.0";
|
|
2552
|
+
var VALID_STATES = /* @__PURE__ */ new Set([
|
|
2553
|
+
"waiting",
|
|
2554
|
+
"active",
|
|
2555
|
+
"delayed",
|
|
2556
|
+
"completed",
|
|
2557
|
+
"failed"
|
|
2558
|
+
]);
|
|
2559
|
+
var VALID_BACKENDS2 = /* @__PURE__ */ new Set(["bullmq", "river", "pg-boss"]);
|
|
2560
|
+
var STRICT_UINT_RE2 = /^\d+$/;
|
|
2561
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
2562
|
+
"--ctx",
|
|
2563
|
+
"--config",
|
|
2564
|
+
"--backend",
|
|
2565
|
+
"--prefix",
|
|
2566
|
+
"--schema",
|
|
2567
|
+
"--inspect",
|
|
2568
|
+
"--queue",
|
|
2569
|
+
"--state",
|
|
2570
|
+
"--limit",
|
|
2571
|
+
"--timeout",
|
|
2572
|
+
"--retry",
|
|
2573
|
+
"--retry-failed"
|
|
2574
|
+
]);
|
|
2575
|
+
function formatHelpText() {
|
|
2576
|
+
return [
|
|
2577
|
+
`q9s v${Q9S_VERSION} - k9s for background job queues (BullMQ, River, pg-boss)`,
|
|
2578
|
+
"",
|
|
2579
|
+
"Usage:",
|
|
2580
|
+
" q9s <redis://...|rediss://...|postgres://...> [options]",
|
|
2581
|
+
" q9s --ctx <name> [options]",
|
|
2582
|
+
" q9s --demo [--json] [--inspect <queue>]",
|
|
2583
|
+
" q9s --contexts [--config <path>] [--json]",
|
|
2584
|
+
"",
|
|
2585
|
+
"Target Options:",
|
|
2586
|
+
" --demo Start live in-process mock BullMQ Redis TCP server",
|
|
2587
|
+
" --ctx <name> Load a named context from ~/.config/q9s/contexts.json|.yaml",
|
|
2588
|
+
" --contexts List configured contexts with redacted credentials",
|
|
2589
|
+
" --config <path> Override contexts config file path",
|
|
2590
|
+
" --backend <kind> Force backend: bullmq | river | pg-boss",
|
|
2591
|
+
" --prefix <prefix> BullMQ Redis key prefix (default: bull)",
|
|
2592
|
+
" --schema <schema> Postgres schema override (default: auto-detected)",
|
|
2593
|
+
" --timeout <ms> Connection/query timeout in ms (default: 5000)",
|
|
2594
|
+
"",
|
|
2595
|
+
"Inspection Options:",
|
|
2596
|
+
" --inspect <queue> Inspect jobs in a specific queue (default: highest failed)",
|
|
2597
|
+
" --state <state> Job state: waiting | active | delayed | completed | failed",
|
|
2598
|
+
" --limit <n> Max jobs to inspect (default: 3) or bulk-retry (default: 100)",
|
|
2599
|
+
" --json Emit machine-readable JSON output",
|
|
2600
|
+
" --no-color Disable ANSI terminal colors",
|
|
2601
|
+
"",
|
|
2602
|
+
"Safety & Write Actions (read-only by default):",
|
|
2603
|
+
" --read-only Enforce read-only mode (default: true)",
|
|
2604
|
+
" --allow-write Explicitly enable write actions (--retry, --retry-failed)",
|
|
2605
|
+
" --retry <jobId> Retry a single failed job (use with --inspect/--queue + --allow-write)",
|
|
2606
|
+
" --queue <queue> Target queue for --retry",
|
|
2607
|
+
" --retry-failed <queue> Bulk retry failed jobs in <queue> (requires --allow-write --yes)",
|
|
2608
|
+
" -y, --yes Confirm bulk write operation",
|
|
2609
|
+
"",
|
|
2610
|
+
"Other:",
|
|
2611
|
+
" -h, --help Show this help message",
|
|
2612
|
+
" -v, --version Print q9s version",
|
|
2613
|
+
"",
|
|
2614
|
+
"Examples:",
|
|
2615
|
+
" q9s --demo",
|
|
2616
|
+
" q9s --demo --inspect webhooks --json",
|
|
2617
|
+
" q9s redis://localhost:6379",
|
|
2618
|
+
" q9s postgres://app:secret@localhost:5432/prod --inspect emails",
|
|
2619
|
+
" q9s --ctx prod-bull --retry-failed webhooks --allow-write --yes"
|
|
2620
|
+
].join("\n");
|
|
2621
|
+
}
|
|
2622
|
+
function expandEqualsFlags(argv) {
|
|
2623
|
+
const expanded = [];
|
|
2624
|
+
for (const arg of argv) {
|
|
2625
|
+
if (arg.startsWith("--") && arg.includes("=")) {
|
|
2626
|
+
const eqIdx = arg.indexOf("=");
|
|
2627
|
+
const flag = arg.slice(0, eqIdx);
|
|
2628
|
+
const val = arg.slice(eqIdx + 1);
|
|
2629
|
+
if (VALUE_FLAGS.has(flag)) {
|
|
2630
|
+
expanded.push(flag, val);
|
|
2631
|
+
continue;
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
expanded.push(arg);
|
|
2635
|
+
}
|
|
2636
|
+
return expanded;
|
|
2637
|
+
}
|
|
2638
|
+
function readNextValue(argv, index, flag, allowLeadingDash = false) {
|
|
2639
|
+
const val = argv[index + 1];
|
|
2640
|
+
if (val === void 0 || val === "" || !allowLeadingDash && val.startsWith("-")) {
|
|
2641
|
+
throw new Error(`Option ${flag} requires a value.`);
|
|
2642
|
+
}
|
|
2643
|
+
return val;
|
|
2644
|
+
}
|
|
2645
|
+
function parseCliArgs(rawArgv) {
|
|
2646
|
+
const argv = expandEqualsFlags(rawArgv);
|
|
2647
|
+
const parsed = {
|
|
2648
|
+
listContexts: false,
|
|
2649
|
+
demo: false,
|
|
2650
|
+
state: "failed",
|
|
2651
|
+
readOnlyFlag: false,
|
|
2652
|
+
allowWrite: false,
|
|
2653
|
+
yes: false,
|
|
2654
|
+
json: false,
|
|
2655
|
+
noColor: false,
|
|
2656
|
+
timeoutMs: 5e3,
|
|
2657
|
+
help: false,
|
|
2658
|
+
version: false
|
|
2659
|
+
};
|
|
2660
|
+
for (let i = 0; i < argv.length; i++) {
|
|
2661
|
+
const arg = argv[i];
|
|
2662
|
+
switch (arg) {
|
|
2663
|
+
case "-h":
|
|
2664
|
+
case "--help":
|
|
2665
|
+
parsed.help = true;
|
|
2666
|
+
break;
|
|
2667
|
+
case "-v":
|
|
2668
|
+
case "--version":
|
|
2669
|
+
parsed.version = true;
|
|
2670
|
+
break;
|
|
2671
|
+
case "--demo":
|
|
2672
|
+
parsed.demo = true;
|
|
2673
|
+
break;
|
|
2674
|
+
case "--contexts":
|
|
2675
|
+
parsed.listContexts = true;
|
|
2676
|
+
break;
|
|
2677
|
+
case "--read-only":
|
|
2678
|
+
parsed.readOnlyFlag = true;
|
|
2679
|
+
break;
|
|
2680
|
+
case "--allow-write":
|
|
2681
|
+
parsed.allowWrite = true;
|
|
2682
|
+
break;
|
|
2683
|
+
case "--yes":
|
|
2684
|
+
case "-y":
|
|
2685
|
+
parsed.yes = true;
|
|
2686
|
+
break;
|
|
2687
|
+
case "--json":
|
|
2688
|
+
parsed.json = true;
|
|
2689
|
+
break;
|
|
2690
|
+
case "--no-color":
|
|
2691
|
+
parsed.noColor = true;
|
|
2692
|
+
break;
|
|
2693
|
+
case "--ctx":
|
|
2694
|
+
parsed.ctx = readNextValue(argv, i, arg);
|
|
2695
|
+
i++;
|
|
2696
|
+
break;
|
|
2697
|
+
case "--config":
|
|
2698
|
+
parsed.config = readNextValue(argv, i, arg);
|
|
2699
|
+
i++;
|
|
2700
|
+
break;
|
|
2701
|
+
case "--backend": {
|
|
2702
|
+
const val = readNextValue(argv, i, arg).toLowerCase();
|
|
2703
|
+
if (!VALID_BACKENDS2.has(val)) {
|
|
2704
|
+
throw new Error(`Invalid --backend "${val}". Expected bullmq, river, or pg-boss.`);
|
|
2705
|
+
}
|
|
2706
|
+
parsed.backend = val;
|
|
2707
|
+
i++;
|
|
2708
|
+
break;
|
|
2709
|
+
}
|
|
2710
|
+
case "--prefix":
|
|
2711
|
+
parsed.prefix = readNextValue(argv, i, arg);
|
|
2712
|
+
i++;
|
|
2713
|
+
break;
|
|
2714
|
+
case "--schema":
|
|
2715
|
+
parsed.schema = readNextValue(argv, i, arg);
|
|
2716
|
+
i++;
|
|
2717
|
+
break;
|
|
2718
|
+
case "--inspect":
|
|
2719
|
+
parsed.inspect = readNextValue(argv, i, arg);
|
|
2720
|
+
i++;
|
|
2721
|
+
break;
|
|
2722
|
+
case "--queue":
|
|
2723
|
+
parsed.queue = readNextValue(argv, i, arg);
|
|
2724
|
+
i++;
|
|
2725
|
+
break;
|
|
2726
|
+
case "--state": {
|
|
2727
|
+
const val = readNextValue(argv, i, arg).toLowerCase();
|
|
2728
|
+
if (!VALID_STATES.has(val)) {
|
|
2729
|
+
throw new Error(
|
|
2730
|
+
`Invalid --state "${val}". Expected waiting, active, delayed, completed, or failed.`
|
|
2731
|
+
);
|
|
2732
|
+
}
|
|
2733
|
+
parsed.state = val;
|
|
2734
|
+
i++;
|
|
2735
|
+
break;
|
|
2736
|
+
}
|
|
2737
|
+
case "--limit": {
|
|
2738
|
+
const raw = readNextValue(argv, i, arg, true);
|
|
2739
|
+
if (!STRICT_UINT_RE2.test(raw)) {
|
|
2740
|
+
throw new Error(`Invalid --limit "${raw}". Expected a positive integer.`);
|
|
2741
|
+
}
|
|
2742
|
+
const n = Number(raw);
|
|
2743
|
+
if (!Number.isSafeInteger(n) || n < 1) {
|
|
2744
|
+
throw new Error(`Invalid --limit "${raw}". Expected a positive integer.`);
|
|
2745
|
+
}
|
|
2746
|
+
parsed.limit = n;
|
|
2747
|
+
i++;
|
|
2748
|
+
break;
|
|
2749
|
+
}
|
|
2750
|
+
case "--timeout": {
|
|
2751
|
+
const raw = readNextValue(argv, i, arg, true);
|
|
2752
|
+
if (!STRICT_UINT_RE2.test(raw)) {
|
|
2753
|
+
throw new Error(`Invalid --timeout "${raw}". Expected milliseconds >= 100.`);
|
|
2754
|
+
}
|
|
2755
|
+
const n = Number(raw);
|
|
2756
|
+
if (!Number.isSafeInteger(n) || n < 100) {
|
|
2757
|
+
throw new Error(`Invalid --timeout "${raw}". Expected milliseconds >= 100.`);
|
|
2758
|
+
}
|
|
2759
|
+
parsed.timeoutMs = n;
|
|
2760
|
+
i++;
|
|
2761
|
+
break;
|
|
2762
|
+
}
|
|
2763
|
+
case "--retry":
|
|
2764
|
+
parsed.retryJobId = readNextValue(argv, i, arg, true);
|
|
2765
|
+
i++;
|
|
2766
|
+
break;
|
|
2767
|
+
case "--retry-failed":
|
|
2768
|
+
parsed.retryFailedQueue = readNextValue(argv, i, arg);
|
|
2769
|
+
i++;
|
|
2770
|
+
break;
|
|
2771
|
+
default:
|
|
2772
|
+
if (arg.startsWith("-")) {
|
|
2773
|
+
throw new Error(`Unknown option: ${arg}`);
|
|
2774
|
+
}
|
|
2775
|
+
if (parsed.url !== void 0) {
|
|
2776
|
+
throw new Error(`Unexpected extra positional argument: ${arg}`);
|
|
2777
|
+
}
|
|
2778
|
+
parsed.url = arg;
|
|
2779
|
+
break;
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
return parsed;
|
|
2783
|
+
}
|
|
2784
|
+
function detectProtocolKind(rawUrl) {
|
|
2785
|
+
let parsed;
|
|
2786
|
+
try {
|
|
2787
|
+
parsed = new URL(rawUrl);
|
|
2788
|
+
} catch {
|
|
2789
|
+
throw new Error(`Invalid target URL: ${redactUrl(rawUrl)}`);
|
|
2790
|
+
}
|
|
2791
|
+
if (parsed.protocol === "redis:" || parsed.protocol === "rediss:") {
|
|
2792
|
+
return "redis";
|
|
2793
|
+
}
|
|
2794
|
+
if (parsed.protocol === "postgres:" || parsed.protocol === "postgresql:") {
|
|
2795
|
+
return "postgres";
|
|
2796
|
+
}
|
|
2797
|
+
throw new Error(
|
|
2798
|
+
`Unsupported URL protocol "${parsed.protocol}". Expected redis://, rediss://, postgres://, or postgresql://`
|
|
2799
|
+
);
|
|
2800
|
+
}
|
|
2801
|
+
function writeError(io, json, message, code = "Q9S_ERROR") {
|
|
2802
|
+
if (json) {
|
|
2803
|
+
io.stdout(renderJson({ error: message, code }));
|
|
2804
|
+
} else {
|
|
2805
|
+
io.stderr(`Error: ${message}`);
|
|
2806
|
+
}
|
|
2807
|
+
}
|
|
2808
|
+
async function runCli(argv, io = {
|
|
2809
|
+
stdout: (line) => process.stdout.write(`${line}
|
|
2810
|
+
`),
|
|
2811
|
+
stderr: (line) => process.stderr.write(`${line}
|
|
2812
|
+
`),
|
|
2813
|
+
isTty: Boolean(process.stdout.isTTY)
|
|
2814
|
+
}) {
|
|
2815
|
+
let args;
|
|
2816
|
+
try {
|
|
2817
|
+
args = parseCliArgs(argv);
|
|
2818
|
+
} catch (err) {
|
|
2819
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
2820
|
+
const wantsJson = argv.includes("--json");
|
|
2821
|
+
writeError(io, wantsJson, msg, "Q9S_USAGE_ERROR");
|
|
2822
|
+
return 2;
|
|
2823
|
+
}
|
|
2824
|
+
if (args.help) {
|
|
2825
|
+
io.stdout(formatHelpText());
|
|
2826
|
+
return 0;
|
|
2827
|
+
}
|
|
2828
|
+
if (args.version) {
|
|
2829
|
+
io.stdout(Q9S_VERSION);
|
|
2830
|
+
return 0;
|
|
2831
|
+
}
|
|
2832
|
+
if (args.listContexts) {
|
|
2833
|
+
try {
|
|
2834
|
+
const contexts = loadContexts(args.config).map(redactContext);
|
|
2835
|
+
if (args.json) {
|
|
2836
|
+
io.stdout(renderJson({ contexts }));
|
|
2837
|
+
} else if (contexts.length === 0) {
|
|
2838
|
+
io.stdout("No contexts configured. Create ~/.config/q9s/contexts.json or contexts.yaml.");
|
|
2839
|
+
} else {
|
|
2840
|
+
for (const ctx of contexts) {
|
|
2841
|
+
const mode = ctx.readOnly === false ? "allow-write" : "read-only";
|
|
2842
|
+
io.stdout(`${ctx.name} ${ctx.backend ?? "auto"} [${mode}] ${ctx.url}`);
|
|
2843
|
+
}
|
|
2844
|
+
}
|
|
2845
|
+
return 0;
|
|
2846
|
+
} catch (err) {
|
|
2847
|
+
writeError(io, args.json, err instanceof Error ? err.message : String(err));
|
|
2848
|
+
return 1;
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
if (args.readOnlyFlag && args.allowWrite) {
|
|
2852
|
+
writeError(
|
|
2853
|
+
io,
|
|
2854
|
+
args.json,
|
|
2855
|
+
"Cannot pass both --read-only and --allow-write together.",
|
|
2856
|
+
"Q9S_USAGE_ERROR"
|
|
2857
|
+
);
|
|
2858
|
+
return 2;
|
|
2859
|
+
}
|
|
2860
|
+
const targetSelectorCount = (args.demo ? 1 : 0) + (args.ctx ? 1 : 0) + (args.url ? 1 : 0);
|
|
2861
|
+
if (targetSelectorCount > 1) {
|
|
2862
|
+
writeError(
|
|
2863
|
+
io,
|
|
2864
|
+
args.json,
|
|
2865
|
+
"Conflicting target selectors: specify only one of <url>, --ctx <name>, or --demo.",
|
|
2866
|
+
"Q9S_USAGE_ERROR"
|
|
2867
|
+
);
|
|
2868
|
+
return 2;
|
|
2869
|
+
}
|
|
2870
|
+
let targetUrl = args.url;
|
|
2871
|
+
let contextName;
|
|
2872
|
+
let backendHint = args.backend;
|
|
2873
|
+
let prefix = args.prefix;
|
|
2874
|
+
let schema = args.schema;
|
|
2875
|
+
let readOnly = true;
|
|
2876
|
+
if (args.ctx) {
|
|
2877
|
+
try {
|
|
2878
|
+
const ctx = resolveContext(args.ctx, args.config);
|
|
2879
|
+
contextName = ctx.name;
|
|
2880
|
+
targetUrl = ctx.url;
|
|
2881
|
+
backendHint = backendHint ?? ctx.backend;
|
|
2882
|
+
prefix = prefix ?? ctx.prefix;
|
|
2883
|
+
schema = schema ?? ctx.schema;
|
|
2884
|
+
if (ctx.readOnly === true && args.allowWrite) {
|
|
2885
|
+
writeError(
|
|
2886
|
+
io,
|
|
2887
|
+
args.json,
|
|
2888
|
+
`Context "${ctx.name}" is pinned to readOnly: true in config. Remove readOnly or target another context.`,
|
|
2889
|
+
"Q9S_READ_ONLY"
|
|
2890
|
+
);
|
|
2891
|
+
return 2;
|
|
2892
|
+
}
|
|
2893
|
+
} catch (err) {
|
|
2894
|
+
writeError(io, args.json, err instanceof Error ? err.message : String(err));
|
|
2895
|
+
return 2;
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2898
|
+
if (args.allowWrite && !args.readOnlyFlag) {
|
|
2899
|
+
readOnly = false;
|
|
2900
|
+
}
|
|
2901
|
+
if (!args.demo && !targetUrl) {
|
|
2902
|
+
writeError(
|
|
2903
|
+
io,
|
|
2904
|
+
args.json,
|
|
2905
|
+
"Missing target URL. Provide a redis:// or postgres:// URL, --ctx <name>, or --demo.",
|
|
2906
|
+
"Q9S_USAGE_ERROR"
|
|
2907
|
+
);
|
|
2908
|
+
return 2;
|
|
2909
|
+
}
|
|
2910
|
+
const wantsRetrySingle = args.retryJobId !== void 0;
|
|
2911
|
+
const wantsRetryBulk = args.retryFailedQueue !== void 0;
|
|
2912
|
+
if (wantsRetrySingle && wantsRetryBulk) {
|
|
2913
|
+
writeError(
|
|
2914
|
+
io,
|
|
2915
|
+
args.json,
|
|
2916
|
+
"Cannot combine --retry and --retry-failed in a single invocation.",
|
|
2917
|
+
"Q9S_USAGE_ERROR"
|
|
2918
|
+
);
|
|
2919
|
+
return 2;
|
|
2920
|
+
}
|
|
2921
|
+
if ((wantsRetrySingle || wantsRetryBulk) && readOnly) {
|
|
2922
|
+
const action = wantsRetryBulk ? `bulk retry failed jobs in queue "${args.retryFailedQueue}"` : `retry job "${args.retryJobId}"`;
|
|
2923
|
+
const err = new ReadOnlyError(action);
|
|
2924
|
+
writeError(io, args.json, err.message, err.code);
|
|
2925
|
+
return 2;
|
|
2926
|
+
}
|
|
2927
|
+
if (wantsRetrySingle && !(args.queue ?? args.inspect)) {
|
|
2928
|
+
writeError(
|
|
2929
|
+
io,
|
|
2930
|
+
args.json,
|
|
2931
|
+
"--retry requires a target queue via --queue <queue> or --inspect <queue>.",
|
|
2932
|
+
"Q9S_USAGE_ERROR"
|
|
2933
|
+
);
|
|
2934
|
+
return 2;
|
|
2935
|
+
}
|
|
2936
|
+
if (wantsRetryBulk && !args.yes) {
|
|
2937
|
+
writeError(
|
|
2938
|
+
io,
|
|
2939
|
+
args.json,
|
|
2940
|
+
`Bulk retry of failed jobs in "${args.retryFailedQueue}" requires explicit confirmation with --yes.`,
|
|
2941
|
+
"Q9S_CONFIRMATION_REQUIRED"
|
|
2942
|
+
);
|
|
2943
|
+
return 2;
|
|
2944
|
+
}
|
|
2945
|
+
let demoServer = null;
|
|
2946
|
+
let backend = null;
|
|
2947
|
+
try {
|
|
2948
|
+
demoServer = args.demo ? await startMockRedisServer({ prefix: prefix ?? "bull" }) : null;
|
|
2949
|
+
const effectiveUrl = demoServer ? demoServer.url : targetUrl;
|
|
2950
|
+
const protocolKind = detectProtocolKind(effectiveUrl);
|
|
2951
|
+
if (protocolKind === "redis") {
|
|
2952
|
+
if (backendHint && backendHint !== "bullmq") {
|
|
2953
|
+
throw new Error(`Redis URLs only support the "bullmq" backend (got "${backendHint}").`);
|
|
2954
|
+
}
|
|
2955
|
+
backend = await BullMqBackend.connect(effectiveUrl, {
|
|
2956
|
+
readOnly,
|
|
2957
|
+
prefix: prefix ?? "bull",
|
|
2958
|
+
timeoutMs: args.timeoutMs
|
|
2959
|
+
});
|
|
2960
|
+
} else {
|
|
2961
|
+
if (backendHint === "bullmq") {
|
|
2962
|
+
throw new Error('Postgres URLs support "river" or "pg-boss", not "bullmq".');
|
|
2963
|
+
}
|
|
2964
|
+
backend = PostgresQueueBackend.connect(effectiveUrl, {
|
|
2965
|
+
readOnly,
|
|
2966
|
+
kind: backendHint,
|
|
2967
|
+
schema,
|
|
2968
|
+
timeoutMs: args.timeoutMs
|
|
2969
|
+
});
|
|
2970
|
+
}
|
|
2971
|
+
const displayTarget = demoServer ? `${redactUrl(effectiveUrl)} (demo)` : redactUrl(effectiveUrl);
|
|
2972
|
+
if (wantsRetrySingle) {
|
|
2973
|
+
const targetQueue = args.queue ?? args.inspect;
|
|
2974
|
+
const result = await backend.retryJob(targetQueue, args.retryJobId);
|
|
2975
|
+
if (args.json) {
|
|
2976
|
+
io.stdout(
|
|
2977
|
+
renderJson({
|
|
2978
|
+
action: "retry",
|
|
2979
|
+
target: displayTarget,
|
|
2980
|
+
backend: backend.kind,
|
|
2981
|
+
readOnly: backend.readOnly,
|
|
2982
|
+
...result
|
|
2983
|
+
})
|
|
2984
|
+
);
|
|
2985
|
+
} else if (result.retried === 0) {
|
|
2986
|
+
io.stdout(
|
|
2987
|
+
`[WRITE ENABLED] Job "${args.retryJobId}" was not found in failed state for queue "${targetQueue}".`
|
|
2988
|
+
);
|
|
2989
|
+
} else {
|
|
2990
|
+
io.stdout(
|
|
2991
|
+
`[WRITE ENABLED] Retried ${result.retried} job in "${result.queue}": ${result.jobIds.join(", ")}`
|
|
2992
|
+
);
|
|
2993
|
+
}
|
|
2994
|
+
return 0;
|
|
2995
|
+
}
|
|
2996
|
+
if (wantsRetryBulk) {
|
|
2997
|
+
const result = await backend.retryFailedBulk(args.retryFailedQueue, args.limit ?? 100);
|
|
2998
|
+
if (args.json) {
|
|
2999
|
+
io.stdout(
|
|
3000
|
+
renderJson({
|
|
3001
|
+
action: "retry-failed",
|
|
3002
|
+
target: displayTarget,
|
|
3003
|
+
backend: backend.kind,
|
|
3004
|
+
readOnly: backend.readOnly,
|
|
3005
|
+
...result
|
|
3006
|
+
})
|
|
3007
|
+
);
|
|
3008
|
+
} else {
|
|
3009
|
+
io.stdout(
|
|
3010
|
+
`[WRITE ENABLED] Bulk-retried ${result.retried} failed ${result.retried === 1 ? "job" : "jobs"} in "${result.queue}".`
|
|
3011
|
+
);
|
|
3012
|
+
}
|
|
3013
|
+
return 0;
|
|
3014
|
+
}
|
|
3015
|
+
const queues = await backend.listQueues();
|
|
3016
|
+
let inspectedQueue = args.inspect;
|
|
3017
|
+
if (!inspectedQueue && queues.length > 0) {
|
|
3018
|
+
const sortedByFailed = [...queues].sort((a, b) => b.failed - a.failed);
|
|
3019
|
+
if ((sortedByFailed[0]?.failed ?? 0) > 0) {
|
|
3020
|
+
inspectedQueue = sortedByFailed[0].name;
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
const inspectLimit = args.limit ?? 3;
|
|
3024
|
+
const inspectedJobs = inspectedQueue ? await backend.listJobs(inspectedQueue, args.state, inspectLimit) : [];
|
|
3025
|
+
const snapshot = {
|
|
3026
|
+
version: Q9S_VERSION,
|
|
3027
|
+
target: displayTarget,
|
|
3028
|
+
contextName,
|
|
3029
|
+
backend: backend.kind,
|
|
3030
|
+
readOnly: backend.readOnly,
|
|
3031
|
+
capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3032
|
+
queues,
|
|
3033
|
+
inspectedQueue,
|
|
3034
|
+
inspectedState: inspectedQueue ? args.state : void 0,
|
|
3035
|
+
inspectedJobs
|
|
3036
|
+
};
|
|
3037
|
+
if (args.json) {
|
|
3038
|
+
io.stdout(renderJson(snapshot));
|
|
3039
|
+
} else {
|
|
3040
|
+
const useColor = !args.noColor && !process.env.NO_COLOR && Boolean(io.isTty);
|
|
3041
|
+
io.stdout(renderDashboard(snapshot, { color: useColor }));
|
|
3042
|
+
}
|
|
3043
|
+
return 0;
|
|
3044
|
+
} catch (err) {
|
|
3045
|
+
const code = err instanceof ReadOnlyError ? err.code : "Q9S_ERROR";
|
|
3046
|
+
writeError(io, args.json, err instanceof Error ? err.message : String(err), code);
|
|
3047
|
+
return err instanceof ReadOnlyError ? 2 : 1;
|
|
3048
|
+
} finally {
|
|
3049
|
+
if (backend) {
|
|
3050
|
+
await backend.close().catch(() => void 0);
|
|
3051
|
+
}
|
|
3052
|
+
if (demoServer) {
|
|
3053
|
+
await demoServer.close().catch(() => void 0);
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
function isExecutedDirectly() {
|
|
3058
|
+
const entry = process.argv[1];
|
|
3059
|
+
if (!entry) {
|
|
3060
|
+
return false;
|
|
3061
|
+
}
|
|
3062
|
+
const normalize = (p) => process.platform === "win32" ? p.toLowerCase() : p;
|
|
3063
|
+
try {
|
|
3064
|
+
const currentFile = fs2.realpathSync.native(fileURLToPath(import.meta.url));
|
|
3065
|
+
const entryFile = fs2.realpathSync.native(path2.resolve(entry));
|
|
3066
|
+
return normalize(currentFile) === normalize(entryFile);
|
|
3067
|
+
} catch {
|
|
3068
|
+
return normalize(path2.resolve(entry)) === normalize(fileURLToPath(import.meta.url));
|
|
3069
|
+
}
|
|
3070
|
+
}
|
|
3071
|
+
if (isExecutedDirectly()) {
|
|
3072
|
+
runCli(process.argv.slice(2)).then((code) => {
|
|
3073
|
+
process.exitCode = code;
|
|
3074
|
+
}).catch((err) => {
|
|
3075
|
+
process.stderr.write(`Fatal: ${err instanceof Error ? err.message : String(err)}
|
|
3076
|
+
`);
|
|
3077
|
+
process.exitCode = 1;
|
|
3078
|
+
});
|
|
3079
|
+
}
|
|
3080
|
+
export {
|
|
3081
|
+
BullMqBackend,
|
|
3082
|
+
PSQL_FIELD_SEPARATOR,
|
|
3083
|
+
PostgresQueueBackend,
|
|
3084
|
+
Q9S_VERSION,
|
|
3085
|
+
ReadOnlyError,
|
|
3086
|
+
RespClient,
|
|
3087
|
+
RespError,
|
|
3088
|
+
RespParser,
|
|
3089
|
+
RespProtocolError,
|
|
3090
|
+
RespTimeoutError,
|
|
3091
|
+
buildDetectPostgresBackendSql,
|
|
3092
|
+
buildPgBossGetJobSql,
|
|
3093
|
+
buildPgBossJobsSql,
|
|
3094
|
+
buildPgBossQueueStatsSql,
|
|
3095
|
+
buildPgBossRetryFailedBulkSql,
|
|
3096
|
+
buildPgBossRetryJobSql,
|
|
3097
|
+
buildRiverGetJobSql,
|
|
3098
|
+
buildRiverJobsSql,
|
|
3099
|
+
buildRiverQueueStatsSql,
|
|
3100
|
+
buildRiverRetryFailedBulkSql,
|
|
3101
|
+
buildRiverRetryJobSql,
|
|
3102
|
+
encodeRespCommand,
|
|
3103
|
+
extractQueueNameFromKey,
|
|
3104
|
+
findConfigFile,
|
|
3105
|
+
formatHelpText,
|
|
3106
|
+
getDefaultConfigPaths,
|
|
3107
|
+
loadContexts,
|
|
3108
|
+
parseBackendDetection,
|
|
3109
|
+
parseBullMqJobHash,
|
|
3110
|
+
parseCliArgs,
|
|
3111
|
+
parseJsonContexts,
|
|
3112
|
+
parsePgBossJobs,
|
|
3113
|
+
parsePgBossQueueStats,
|
|
3114
|
+
parsePostgresUrl,
|
|
3115
|
+
parseRedisUrl,
|
|
3116
|
+
parseRespFrame,
|
|
3117
|
+
parseReturnedJobIds,
|
|
3118
|
+
parseRiverJobs,
|
|
3119
|
+
parseRiverQueueStats,
|
|
3120
|
+
parseSimpleYamlContexts,
|
|
3121
|
+
redactContext,
|
|
3122
|
+
redactUrl,
|
|
3123
|
+
renderDashboard,
|
|
3124
|
+
renderJobInspector,
|
|
3125
|
+
renderJson,
|
|
3126
|
+
renderQueueTable,
|
|
3127
|
+
renderSafetyBadge,
|
|
3128
|
+
renderSparkline,
|
|
3129
|
+
resolveContext,
|
|
3130
|
+
runCli,
|
|
3131
|
+
sanitizeTerminalText,
|
|
3132
|
+
seedBullMqDemoData,
|
|
3133
|
+
startMockRedisServer,
|
|
3134
|
+
validateIdentifier,
|
|
3135
|
+
validateJobId,
|
|
3136
|
+
validateQueueName
|
|
3137
|
+
};
|