@blamejs/core 0.4.1
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/CHANGELOG.md +230 -0
- package/LICENSE +201 -0
- package/LTS-CALENDAR.md +29 -0
- package/MIGRATING.md +7 -0
- package/NOTICE +59 -0
- package/README.md +100 -0
- package/bin/blamejs.js +13 -0
- package/index.js +253 -0
- package/lib/api-key.js +705 -0
- package/lib/api-snapshot.js +335 -0
- package/lib/app-shutdown.js +381 -0
- package/lib/app.js +364 -0
- package/lib/atomic-file.js +525 -0
- package/lib/audit-chain.js +168 -0
- package/lib/audit-sign.js +319 -0
- package/lib/audit-tools.js +682 -0
- package/lib/audit.js +753 -0
- package/lib/auth/jwt.js +280 -0
- package/lib/auth/oauth.js +691 -0
- package/lib/auth/passkey.js +185 -0
- package/lib/auth/password.js +139 -0
- package/lib/auth/totp.js +17 -0
- package/lib/auth-header.js +81 -0
- package/lib/backup/bundle.js +219 -0
- package/lib/backup/crypto.js +174 -0
- package/lib/backup/index.js +490 -0
- package/lib/backup/manifest.js +275 -0
- package/lib/bundler.js +295 -0
- package/lib/cache.js +819 -0
- package/lib/chain-writer.js +234 -0
- package/lib/cli-helpers.js +201 -0
- package/lib/cli.js +1377 -0
- package/lib/cluster-provider-db.js +245 -0
- package/lib/cluster-storage.js +166 -0
- package/lib/cluster.js +691 -0
- package/lib/consent.js +222 -0
- package/lib/constants.js +186 -0
- package/lib/cookies.js +293 -0
- package/lib/credential-hash.js +303 -0
- package/lib/crypto-field.js +159 -0
- package/lib/crypto.js +250 -0
- package/lib/db-query.js +297 -0
- package/lib/db-schema.js +250 -0
- package/lib/db.js +1054 -0
- package/lib/deprecate.js +226 -0
- package/lib/dev.js +324 -0
- package/lib/error-page.js +424 -0
- package/lib/events.js +135 -0
- package/lib/external-db.js +422 -0
- package/lib/forms.js +378 -0
- package/lib/framework-error.js +189 -0
- package/lib/framework-schema.js +604 -0
- package/lib/handlers.js +350 -0
- package/lib/html-balance.js +227 -0
- package/lib/http-client.js +615 -0
- package/lib/i18n.js +780 -0
- package/lib/jobs.js +181 -0
- package/lib/lazy-require.js +48 -0
- package/lib/log-stream-local.js +137 -0
- package/lib/log-stream-webhook.js +170 -0
- package/lib/log-stream.js +211 -0
- package/lib/log.js +355 -0
- package/lib/mail-bounce.js +507 -0
- package/lib/mail.js +701 -0
- package/lib/metrics.js +647 -0
- package/lib/middleware/api-encrypt.js +553 -0
- package/lib/middleware/attach-user.js +156 -0
- package/lib/middleware/body-parser.js +883 -0
- package/lib/middleware/bot-guard.js +148 -0
- package/lib/middleware/compression.js +436 -0
- package/lib/middleware/cors.js +236 -0
- package/lib/middleware/csp-nonce.js +332 -0
- package/lib/middleware/csrf-protect.js +275 -0
- package/lib/middleware/error-handler.js +46 -0
- package/lib/middleware/health.js +358 -0
- package/lib/middleware/index.js +52 -0
- package/lib/middleware/rate-limit.js +319 -0
- package/lib/middleware/request-id.js +53 -0
- package/lib/middleware/require-auth.js +95 -0
- package/lib/middleware/security-headers.js +91 -0
- package/lib/migrations.js +353 -0
- package/lib/mtls-ca.js +333 -0
- package/lib/mtls-engine-default.js +285 -0
- package/lib/nonce-store.js +177 -0
- package/lib/notify.js +643 -0
- package/lib/ntp-check.js +178 -0
- package/lib/object-store/azure-blob.js +467 -0
- package/lib/object-store/gcs.js +469 -0
- package/lib/object-store/http-put.js +153 -0
- package/lib/object-store/index.js +140 -0
- package/lib/object-store/local.js +163 -0
- package/lib/object-store/retry.js +15 -0
- package/lib/object-store/sigv4.js +535 -0
- package/lib/observability.js +114 -0
- package/lib/pagination.js +371 -0
- package/lib/parsers/index.js +64 -0
- package/lib/parsers/safe-csv.js +224 -0
- package/lib/parsers/safe-env.js +614 -0
- package/lib/parsers/safe-toml.js +745 -0
- package/lib/parsers/safe-xml.js +379 -0
- package/lib/parsers/safe-yaml.js +977 -0
- package/lib/permissions.js +430 -0
- package/lib/pqc-agent.js +85 -0
- package/lib/pqc-gate.js +266 -0
- package/lib/protocol-dispatcher.js +144 -0
- package/lib/queue-local.js +327 -0
- package/lib/queue.js +430 -0
- package/lib/redact.js +192 -0
- package/lib/render.js +193 -0
- package/lib/request-helpers.js +178 -0
- package/lib/restore-bundle.js +239 -0
- package/lib/restore-rollback.js +254 -0
- package/lib/restore.js +301 -0
- package/lib/retry.js +329 -0
- package/lib/router.js +437 -0
- package/lib/safe-async.js +520 -0
- package/lib/safe-buffer.js +162 -0
- package/lib/safe-json.js +532 -0
- package/lib/safe-schema.js +1176 -0
- package/lib/safe-sql.js +157 -0
- package/lib/safe-url.js +109 -0
- package/lib/scheduler.js +680 -0
- package/lib/seeders.js +622 -0
- package/lib/session.js +304 -0
- package/lib/slug.js +243 -0
- package/lib/static.js +268 -0
- package/lib/storage.js +470 -0
- package/lib/subject.js +281 -0
- package/lib/template.js +781 -0
- package/lib/testing.js +621 -0
- package/lib/totp.js +285 -0
- package/lib/tracing.js +484 -0
- package/lib/validate-opts.js +56 -0
- package/lib/vault/index.js +299 -0
- package/lib/vault/passphrase-ops.js +311 -0
- package/lib/vault/passphrase-source.js +198 -0
- package/lib/vault/rotate.js +761 -0
- package/lib/vault/wrap.js +289 -0
- package/lib/vendor/MANIFEST.json +84 -0
- package/lib/vendor/argon2/argon2.cjs +466 -0
- package/lib/vendor/argon2/argon2.d.cts +62 -0
- package/lib/vendor/argon2/package.json +1 -0
- package/lib/vendor/argon2/prebuilds/darwin-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/darwin-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/freebsd-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/freebsd-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-x64/argon2.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/win32-x64/argon2.glibc.node +0 -0
- package/lib/vendor/noble-ciphers.cjs +9 -0
- package/lib/vendor/pki.cjs +181 -0
- package/lib/vendor/simplewebauthn-server.cjs +328 -0
- package/lib/webhook.js +632 -0
- package/lib/websocket-channels.js +413 -0
- package/lib/websocket.js +833 -0
- package/package.json +39 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Local-protocol queue adapter — DB-backed, dialect-portable.
|
|
4
|
+
*
|
|
5
|
+
* Single-node: backed by the framework's main DB (_blamejs_jobs in
|
|
6
|
+
* local SQLite, baked into FRAMEWORK_SCHEMA).
|
|
7
|
+
* Cluster mode: backed by external-db (_blamejs_jobs created via
|
|
8
|
+
* frameworkSchema.ensureSchema). cluster-storage.execute routes the
|
|
9
|
+
* same SQL to the right place based on cluster.isClusterMode().
|
|
10
|
+
*
|
|
11
|
+
* Lease semantics:
|
|
12
|
+
* enqueue → INSERT status='pending'
|
|
13
|
+
* lease → atomic UPDATE pending→inflight (single statement with
|
|
14
|
+
* RETURNING; no transaction needed — the WHERE clause's
|
|
15
|
+
* subquery + Postgres's row-lock-then-recheck behavior
|
|
16
|
+
* makes concurrent leasers safe automatically. The unlucky
|
|
17
|
+
* leaser sees zero rows and tries again on the next tick.)
|
|
18
|
+
* complete→ UPDATE inflight→done
|
|
19
|
+
* fail → single UPDATE with CASE WHEN attempts < maxAttempts
|
|
20
|
+
* (retry) ELSE (final failure) — collapsed into one
|
|
21
|
+
* statement so the same code path works in both single-
|
|
22
|
+
* node and cluster mode without a cross-dialect
|
|
23
|
+
* transaction primitive.
|
|
24
|
+
* sweep → orphaned 'inflight' rows whose lease expired → 'pending'
|
|
25
|
+
*
|
|
26
|
+
* Field-crypto integration: payload + lastError are sealed columns
|
|
27
|
+
* (declared in db.js's FRAMEWORK_SCHEMA registration). enqueue seals
|
|
28
|
+
* before INSERT; lease unseals the leased rows before returning to
|
|
29
|
+
* the caller. cluster-storage's RETURNING clause hands back sealed
|
|
30
|
+
* blobs which we run through cryptoField.unsealRow explicitly.
|
|
31
|
+
*/
|
|
32
|
+
var cluster = require("./cluster");
|
|
33
|
+
var clusterStorage = require("./cluster-storage");
|
|
34
|
+
var { generateToken } = require("./crypto");
|
|
35
|
+
var cryptoField = require("./crypto-field");
|
|
36
|
+
var safeJson = require("./safe-json");
|
|
37
|
+
var lazyRequire = require("./lazy-require");
|
|
38
|
+
var { QueueError } = require("./framework-error");
|
|
39
|
+
|
|
40
|
+
var _err = QueueError.factory;
|
|
41
|
+
|
|
42
|
+
// vault is lazy-required because some flows (sealed lastError) only
|
|
43
|
+
// touch it on retry-with-error paths, and the import order
|
|
44
|
+
// (queue-local → vault → db → audit → cluster) tolerates the late bind.
|
|
45
|
+
var vault = lazyRequire(function () { return require("./vault"); });
|
|
46
|
+
|
|
47
|
+
// Column order kept as a constant so the placeholders + values lists
|
|
48
|
+
// stay in sync. Mirrors db.js's FRAMEWORK_SCHEMA for _blamejs_jobs.
|
|
49
|
+
var JOB_COLS = [
|
|
50
|
+
"_id", "queueName", "payload", "status",
|
|
51
|
+
"enqueuedAt", "availableAt", "leasedAt", "leaseExpiresAt",
|
|
52
|
+
"attempts", "maxAttempts", "lastError", "finishedAt",
|
|
53
|
+
"traceId", "classification",
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
// Columns returned by lease() / used by RETURNING. Subset of JOB_COLS
|
|
57
|
+
// — only what callers need; fewer bytes over the wire in cluster mode.
|
|
58
|
+
var LEASE_RETURN_COLS = [
|
|
59
|
+
"_id", "queueName", "payload",
|
|
60
|
+
"attempts", "maxAttempts", "traceId", "classification",
|
|
61
|
+
"enqueuedAt", "leaseExpiresAt",
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
function _quotedList(cols) {
|
|
65
|
+
return cols.map(function (c) { return '"' + c + '"'; }).join(", ");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function _placeholders(cols) {
|
|
69
|
+
return cols.map(function () { return "?"; }).join(", ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function _shapeLeasedRow(raw) {
|
|
73
|
+
// raw is a row coming back from RETURNING — payload is sealed if
|
|
74
|
+
// present. Run through cryptoField's unseal pipeline so the caller
|
|
75
|
+
// gets cleartext.
|
|
76
|
+
var unsealed = cryptoField.unsealRow("_blamejs_jobs", raw);
|
|
77
|
+
return {
|
|
78
|
+
jobId: unsealed._id,
|
|
79
|
+
queueName: unsealed.queueName,
|
|
80
|
+
payload: unsealed.payload ? safeJson.parse(unsealed.payload) : null,
|
|
81
|
+
attempts: Number(unsealed.attempts),
|
|
82
|
+
maxAttempts: Number(unsealed.maxAttempts),
|
|
83
|
+
traceId: unsealed.traceId,
|
|
84
|
+
classification: unsealed.classification,
|
|
85
|
+
enqueuedAt: Number(unsealed.enqueuedAt),
|
|
86
|
+
leaseExpiresAt: Number(unsealed.leaseExpiresAt),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function create(_config) {
|
|
91
|
+
// No protocol-level config — uses cluster-storage which dispatches
|
|
92
|
+
// to the framework's main DB (single-node) or external-db (cluster).
|
|
93
|
+
|
|
94
|
+
async function enqueue(queueName, payload, opts) {
|
|
95
|
+
cluster.requireLeader();
|
|
96
|
+
opts = opts || {};
|
|
97
|
+
var nowMs = Date.now();
|
|
98
|
+
var availableAt = nowMs + (opts.delaySeconds ? opts.delaySeconds * 1000 : 0);
|
|
99
|
+
|
|
100
|
+
var row = {
|
|
101
|
+
_id: generateToken(16),
|
|
102
|
+
queueName: queueName,
|
|
103
|
+
payload: payload === undefined ? null : JSON.stringify(payload),
|
|
104
|
+
status: "pending",
|
|
105
|
+
enqueuedAt: nowMs,
|
|
106
|
+
availableAt: availableAt,
|
|
107
|
+
leasedAt: null,
|
|
108
|
+
leaseExpiresAt: null,
|
|
109
|
+
attempts: 0,
|
|
110
|
+
maxAttempts: opts.maxAttempts != null ? opts.maxAttempts : 5,
|
|
111
|
+
lastError: null,
|
|
112
|
+
finishedAt: null,
|
|
113
|
+
traceId: opts.traceId || null,
|
|
114
|
+
classification: opts.classification || null,
|
|
115
|
+
};
|
|
116
|
+
var sealed = cryptoField.sealRow("_blamejs_jobs", row);
|
|
117
|
+
var values = JOB_COLS.map(function (c) { return c in sealed ? sealed[c] : null; });
|
|
118
|
+
|
|
119
|
+
await clusterStorage.execute(
|
|
120
|
+
"INSERT INTO _blamejs_jobs (" + _quotedList(JOB_COLS) + ") " +
|
|
121
|
+
"VALUES (" + _placeholders(JOB_COLS) + ")",
|
|
122
|
+
values
|
|
123
|
+
);
|
|
124
|
+
return {
|
|
125
|
+
jobId: row._id,
|
|
126
|
+
queueName: queueName,
|
|
127
|
+
enqueuedAt: nowMs,
|
|
128
|
+
availableAt: availableAt,
|
|
129
|
+
classification: row.classification,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function lease(queueName, leaseMs, count) {
|
|
134
|
+
cluster.requireLeader();
|
|
135
|
+
var nowMs = Date.now();
|
|
136
|
+
var leaseExpiresAt = nowMs + leaseMs;
|
|
137
|
+
var maxRows = count != null ? count : 1;
|
|
138
|
+
|
|
139
|
+
// Single-statement atomic lease. The IN-subquery picks the head of
|
|
140
|
+
// the queue; the outer UPDATE locks those rows and only updates
|
|
141
|
+
// rows that still match status='pending' after the lock acquires
|
|
142
|
+
// (Postgres EvalPlanQual; SQLite is single-writer so the same row
|
|
143
|
+
// can't be picked twice). RETURNING hands back the leased columns
|
|
144
|
+
// so we don't need a separate SELECT after the UPDATE.
|
|
145
|
+
var sql =
|
|
146
|
+
"UPDATE _blamejs_jobs " +
|
|
147
|
+
"SET status = 'inflight', leasedAt = ?, leaseExpiresAt = ?, attempts = attempts + 1 " +
|
|
148
|
+
"WHERE _id IN (" +
|
|
149
|
+
" SELECT _id FROM _blamejs_jobs " +
|
|
150
|
+
" WHERE queueName = ? AND status = 'pending' AND availableAt <= ? " +
|
|
151
|
+
" ORDER BY availableAt ASC, enqueuedAt ASC " +
|
|
152
|
+
" LIMIT ?" +
|
|
153
|
+
") " +
|
|
154
|
+
"RETURNING " + _quotedList(LEASE_RETURN_COLS);
|
|
155
|
+
var result = await clusterStorage.execute(
|
|
156
|
+
sql,
|
|
157
|
+
[nowMs, leaseExpiresAt, queueName, nowMs, maxRows]
|
|
158
|
+
);
|
|
159
|
+
var leased = [];
|
|
160
|
+
for (var i = 0; i < result.rows.length; i++) {
|
|
161
|
+
leased.push(_shapeLeasedRow(result.rows[i]));
|
|
162
|
+
}
|
|
163
|
+
return leased;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// extendLease — push the lease expiry forward for a long-running job.
|
|
167
|
+
// Handler context exposes this as `ctx.extendLease(ms)`. The job must
|
|
168
|
+
// still be in 'inflight' status (i.e. not yet swept by sweepExpired);
|
|
169
|
+
// otherwise the call no-ops and returns false.
|
|
170
|
+
async function extendLease(jobId, additionalMs) {
|
|
171
|
+
cluster.requireLeader();
|
|
172
|
+
if (typeof additionalMs !== "number" || additionalMs <= 0) {
|
|
173
|
+
throw _err("INVALID_LEASE_EXTENSION",
|
|
174
|
+
"extendLease: additionalMs must be a positive number", true);
|
|
175
|
+
}
|
|
176
|
+
var newExpiry = Date.now() + additionalMs;
|
|
177
|
+
var result = await clusterStorage.execute(
|
|
178
|
+
"UPDATE _blamejs_jobs SET leaseExpiresAt = ? " +
|
|
179
|
+
"WHERE _id = ? AND status = 'inflight'",
|
|
180
|
+
[newExpiry, jobId]
|
|
181
|
+
);
|
|
182
|
+
return (result.rowCount || 0) > 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function complete(jobId) {
|
|
186
|
+
cluster.requireLeader();
|
|
187
|
+
await clusterStorage.execute(
|
|
188
|
+
"UPDATE _blamejs_jobs SET status = 'done', finishedAt = ?, leaseExpiresAt = NULL " +
|
|
189
|
+
"WHERE _id = ? AND status = 'inflight'",
|
|
190
|
+
[Date.now(), jobId]
|
|
191
|
+
);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function fail(jobId, errorMessage, opts) {
|
|
196
|
+
cluster.requireLeader();
|
|
197
|
+
opts = opts || {};
|
|
198
|
+
var retryDelayMs = opts.retryDelayMs != null ? opts.retryDelayMs : 0;
|
|
199
|
+
var nowMs = Date.now();
|
|
200
|
+
var sealedErr = errorMessage ? vault().seal(String(errorMessage)) : null;
|
|
201
|
+
|
|
202
|
+
// Single-statement decision: retry vs final failure based on the
|
|
203
|
+
// row's current attempts/maxAttempts. CASE expressions split the
|
|
204
|
+
// status / availableAt / finishedAt updates per branch — same
|
|
205
|
+
// semantics as the previous SELECT-then-UPDATE-in-transaction
|
|
206
|
+
// path, but no cross-dialect transaction primitive needed.
|
|
207
|
+
await clusterStorage.execute(
|
|
208
|
+
"UPDATE _blamejs_jobs SET " +
|
|
209
|
+
" status = CASE WHEN attempts < maxAttempts THEN 'pending' ELSE 'failed' END, " +
|
|
210
|
+
" lastError = ?, " +
|
|
211
|
+
" leaseExpiresAt = NULL, " +
|
|
212
|
+
" availableAt = CASE WHEN attempts < maxAttempts THEN ? ELSE availableAt END, " +
|
|
213
|
+
" finishedAt = CASE WHEN attempts < maxAttempts THEN NULL ELSE ? END " +
|
|
214
|
+
"WHERE _id = ?",
|
|
215
|
+
[sealedErr, nowMs + retryDelayMs, nowMs, jobId]
|
|
216
|
+
);
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function sweepExpired() {
|
|
221
|
+
cluster.requireLeader();
|
|
222
|
+
var result = await clusterStorage.execute(
|
|
223
|
+
"UPDATE _blamejs_jobs SET status = 'pending', leaseExpiresAt = NULL " +
|
|
224
|
+
"WHERE status = 'inflight' AND leaseExpiresAt < ?",
|
|
225
|
+
[Date.now()]
|
|
226
|
+
);
|
|
227
|
+
return result.rowCount || 0;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function size(queueName) {
|
|
231
|
+
var row = await clusterStorage.executeOne(
|
|
232
|
+
"SELECT COUNT(*) AS n FROM _blamejs_jobs " +
|
|
233
|
+
"WHERE queueName = ? AND (status = 'pending' OR status = 'inflight')",
|
|
234
|
+
[queueName]
|
|
235
|
+
);
|
|
236
|
+
return row ? Number(row.n) : 0;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ---- DLQ (dead-letter queue) ----
|
|
240
|
+
//
|
|
241
|
+
// Jobs that exhausted their retry budget land in status='failed' with
|
|
242
|
+
// finishedAt set + lastError sealed. dlqList surfaces them for
|
|
243
|
+
// operator review; dlqRetry resets a job back to 'pending' so it can
|
|
244
|
+
// be reprocessed (operator-driven — never automatic).
|
|
245
|
+
|
|
246
|
+
async function dlqList(queueName, opts) {
|
|
247
|
+
opts = opts || {};
|
|
248
|
+
var limit = (typeof opts.limit === "number" && opts.limit > 0) ? opts.limit : 100;
|
|
249
|
+
var rows = await clusterStorage.executeAll(
|
|
250
|
+
"SELECT _id, queueName, payload, status, enqueuedAt, finishedAt, " +
|
|
251
|
+
" attempts, maxAttempts, lastError, traceId, classification " +
|
|
252
|
+
"FROM _blamejs_jobs " +
|
|
253
|
+
"WHERE queueName = ? AND status = 'failed' " +
|
|
254
|
+
"ORDER BY finishedAt DESC LIMIT ?",
|
|
255
|
+
[queueName, limit]
|
|
256
|
+
);
|
|
257
|
+
return rows.map(function (row) {
|
|
258
|
+
var unsealed = cryptoField.unsealRow("_blamejs_jobs", row);
|
|
259
|
+
return {
|
|
260
|
+
jobId: row._id,
|
|
261
|
+
queueName: row.queueName,
|
|
262
|
+
payload: unsealed.payload ? JSON.parse(unsealed.payload) : null,
|
|
263
|
+
status: row.status,
|
|
264
|
+
enqueuedAt: Number(row.enqueuedAt),
|
|
265
|
+
finishedAt: row.finishedAt ? Number(row.finishedAt) : null,
|
|
266
|
+
attempts: Number(row.attempts),
|
|
267
|
+
maxAttempts: Number(row.maxAttempts),
|
|
268
|
+
lastError: unsealed.lastError || null,
|
|
269
|
+
traceId: row.traceId || null,
|
|
270
|
+
classification: row.classification || null,
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function dlqRetry(jobId) {
|
|
276
|
+
cluster.requireLeader();
|
|
277
|
+
var nowMs = Date.now();
|
|
278
|
+
var result = await clusterStorage.execute(
|
|
279
|
+
"UPDATE _blamejs_jobs SET " +
|
|
280
|
+
" status = 'pending', " +
|
|
281
|
+
" attempts = 0, " +
|
|
282
|
+
" availableAt = ?, " +
|
|
283
|
+
" finishedAt = NULL, " +
|
|
284
|
+
" leasedAt = NULL, " +
|
|
285
|
+
" leaseExpiresAt = NULL, " +
|
|
286
|
+
" lastError = NULL " +
|
|
287
|
+
"WHERE _id = ? AND status = 'failed'",
|
|
288
|
+
[nowMs, jobId]
|
|
289
|
+
);
|
|
290
|
+
return (result.rowCount || 0) > 0;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function dlqSize(queueName) {
|
|
294
|
+
var row = await clusterStorage.executeOne(
|
|
295
|
+
"SELECT COUNT(*) AS n FROM _blamejs_jobs " +
|
|
296
|
+
"WHERE queueName = ? AND status = 'failed'",
|
|
297
|
+
[queueName]
|
|
298
|
+
);
|
|
299
|
+
return row ? Number(row.n) : 0;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function purge(queueName) {
|
|
303
|
+
cluster.requireLeader();
|
|
304
|
+
var result = await clusterStorage.execute(
|
|
305
|
+
"DELETE FROM _blamejs_jobs WHERE queueName = ?",
|
|
306
|
+
[queueName]
|
|
307
|
+
);
|
|
308
|
+
return result.rowCount || 0;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
protocol: "local",
|
|
313
|
+
enqueue: enqueue,
|
|
314
|
+
lease: lease,
|
|
315
|
+
extendLease: extendLease,
|
|
316
|
+
complete: complete,
|
|
317
|
+
fail: fail,
|
|
318
|
+
sweepExpired: sweepExpired,
|
|
319
|
+
size: size,
|
|
320
|
+
purge: purge,
|
|
321
|
+
dlqList: dlqList,
|
|
322
|
+
dlqRetry: dlqRetry,
|
|
323
|
+
dlqSize: dlqSize,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
module.exports = { create: create };
|