@tylix/core 1.4.0 → 2.0.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/README.md +59 -3
- package/package.json +12 -6
- package/src/http/debug/renderDebugPage.js +2 -0
- package/src/http/requireAuth.js +15 -4
- package/src/http/statusPage.js +20 -19
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -0
- package/src/observability/index.d.ts +66 -1
- package/src/observability/logger.js +118 -20
- package/src/observability/logging.js +351 -0
- package/src/plugins/plugins.d.ts +81 -0
- package/src/plugins/plugins.js +200 -0
- package/src/queue/JobRecord.js +1 -1
- package/src/queue/dispatch.d.ts +47 -1
- package/src/queue/dispatch.js +17 -3
- package/src/queue/retry.js +89 -0
package/src/queue/dispatch.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import { JobRecord } from './JobRecord.js'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
// dispatch('SendInvoice', { invoiceId: 7 })
|
|
4
|
+
// dispatch('SendInvoice', { invoiceId: 7 }, { delay: 60 }) // run in a minute
|
|
5
|
+
export async function dispatch(jobName, payload = {}, options = {}) {
|
|
6
|
+
const delay = Number(options.delay ?? 0)
|
|
7
|
+
const record = {
|
|
5
8
|
job: jobName,
|
|
6
9
|
payload: JSON.stringify(payload),
|
|
7
10
|
status: 'pending',
|
|
8
11
|
attempts: 0,
|
|
9
|
-
}
|
|
12
|
+
}
|
|
13
|
+
// Only set when delayed, so a jobs table made before retries (no
|
|
14
|
+
// available_at column) keeps working for jobs that run at once.
|
|
15
|
+
if (delay > 0) record.available_at = new Date(Date.now() + delay * 1000).toISOString()
|
|
16
|
+
try {
|
|
17
|
+
return await JobRecord.create(record)
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (delay > 0 && /available_at/.test(err.message)) {
|
|
20
|
+
throw new Error('dispatch(..., { delay }) needs the available_at column on the jobs table. Run "tylix make:queue-retries" then "tylix migrate".', { cause: err })
|
|
21
|
+
}
|
|
22
|
+
throw err
|
|
23
|
+
}
|
|
10
24
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Retry rules for queued jobs, as in Laravel. A job handler may set:
|
|
2
|
+
//
|
|
3
|
+
// export const SendInvoice = {
|
|
4
|
+
// tries: 5, // attempts in total (default: queue.tries, 1)
|
|
5
|
+
// backoff: [10, 60, 300], // seconds before attempt 2, 3, 4 (the last repeats)
|
|
6
|
+
// // backoff: 30 // the same wait every time
|
|
7
|
+
// // backoff: 'exponential' // 1s, 2s, 4s, 8s ... with jitter, capped at maxBackoff
|
|
8
|
+
// // backoff: (attempt) => attempt * 10
|
|
9
|
+
// maxBackoff: 3600, // cap for 'exponential' (seconds)
|
|
10
|
+
// timeout: 120, // seconds; a slower attempt counts as failed
|
|
11
|
+
// async handle(payload, job) {},
|
|
12
|
+
// async failed(payload, error, job) {}, // once, after the last attempt fails
|
|
13
|
+
// }
|
|
14
|
+
//
|
|
15
|
+
// Throw an error with `retryable: false` (or a PermanentJobError) to fail
|
|
16
|
+
// at once, without the remaining attempts.
|
|
17
|
+
|
|
18
|
+
export class PermanentJobError extends Error {
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
super(message, options)
|
|
21
|
+
this.name = 'PermanentJobError'
|
|
22
|
+
this.retryable = false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class JobTimeoutError extends Error {
|
|
27
|
+
constructor(seconds) {
|
|
28
|
+
super(`Job timed out after ${seconds}s`)
|
|
29
|
+
this.name = 'JobTimeoutError'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const QUEUE_DEFAULTS = { tries: 1, backoff: 0, maxBackoff: 3600, timeout: 0 }
|
|
34
|
+
|
|
35
|
+
function positive(value, fallback) {
|
|
36
|
+
const number = Number(value)
|
|
37
|
+
return Number.isFinite(number) && number >= 0 ? number : fallback
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The job's settings: its own, then the queue config, then the defaults.
|
|
41
|
+
export function jobOptions(handler = {}, defaults = {}) {
|
|
42
|
+
const base = { ...QUEUE_DEFAULTS, ...defaults }
|
|
43
|
+
return {
|
|
44
|
+
tries: Math.max(1, Math.floor(positive(handler.tries ?? base.tries, 1))),
|
|
45
|
+
backoff: handler.backoff ?? base.backoff,
|
|
46
|
+
maxBackoff: positive(handler.maxBackoff ?? base.maxBackoff, 3600),
|
|
47
|
+
timeout: positive(handler.timeout ?? base.timeout, 0),
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Seconds to wait before the next attempt, after `attempt` (1-based)
|
|
52
|
+
// attempts have failed.
|
|
53
|
+
export function backoffSeconds(options, attempt, random = Math.random) {
|
|
54
|
+
const { backoff, maxBackoff } = options
|
|
55
|
+
let seconds
|
|
56
|
+
if (typeof backoff === 'function') seconds = backoff(attempt)
|
|
57
|
+
else if (Array.isArray(backoff)) seconds = backoff.length ? backoff[Math.min(attempt, backoff.length) - 1] : 0
|
|
58
|
+
else if (backoff === 'exponential') {
|
|
59
|
+
const exact = Math.min(2 ** (attempt - 1), maxBackoff)
|
|
60
|
+
// Full jitter on the upper half, so retries of many jobs spread out.
|
|
61
|
+
seconds = exact / 2 + random() * (exact / 2)
|
|
62
|
+
} else seconds = backoff
|
|
63
|
+
return Math.min(positive(seconds, 0), Math.max(maxBackoff, 0) || Infinity)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// What the worker does after an attempt threw: retry later, or fail.
|
|
67
|
+
export function afterFailure(options, attempt, error, now = Date.now()) {
|
|
68
|
+
const permanent = error?.retryable === false
|
|
69
|
+
if (permanent || attempt >= options.tries) return { retry: false, attempt }
|
|
70
|
+
const delay = backoffSeconds(options, attempt)
|
|
71
|
+
return { retry: true, attempt, delaySeconds: delay, availableAt: new Date(now + delay * 1000) }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Runs handle() with the job's timeout.
|
|
75
|
+
export async function runWithTimeout(work, seconds) {
|
|
76
|
+
if (!seconds) return work()
|
|
77
|
+
let timer
|
|
78
|
+
try {
|
|
79
|
+
return await Promise.race([
|
|
80
|
+
work(),
|
|
81
|
+
new Promise((_, reject) => {
|
|
82
|
+
timer = setTimeout(() => reject(new JobTimeoutError(seconds)), seconds * 1000)
|
|
83
|
+
timer.unref?.()
|
|
84
|
+
}),
|
|
85
|
+
])
|
|
86
|
+
} finally {
|
|
87
|
+
clearTimeout(timer)
|
|
88
|
+
}
|
|
89
|
+
}
|