@better-forge/notify 0.1.0 → 0.1.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/LICENSE +21 -21
- package/README.md +11 -11
- package/dist/migrations.js +59 -59
- package/package.json +59 -59
- package/src/alertPolicy.ts +149 -149
- package/src/channelInput.ts +153 -153
- package/src/channelSecrets.ts +20 -20
- package/src/engine.ts +423 -423
- package/src/index.ts +16 -16
- package/src/migrations.ts +88 -88
- package/src/notice.ts +68 -68
- package/src/routes.ts +362 -362
- package/src/tables.ts +96 -96
- package/src/template.ts +76 -76
- package/src/transports.ts +351 -351
- package/src/worker.ts +58 -58
package/src/worker.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import cron, { type ScheduledTask } from 'node-cron'
|
|
2
|
-
import type { NotifyEngine } from './engine.js'
|
|
3
|
-
|
|
4
|
-
/** Work the application runs on the same ticks as the deliveries. A rejection is logged under `failure`, never thrown. */
|
|
5
|
-
export interface WorkerTask {
|
|
6
|
-
/** The line logged when the task fails (the error follows it). */
|
|
7
|
-
failure: string
|
|
8
|
-
run: () => Promise<unknown>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface DeliveryWorkerOptions {
|
|
12
|
-
engine: Pick<NotifyEngine, 'processDue' | 'purgeOldDeliveries'>
|
|
13
|
-
/** Run every 30 seconds beside the sending of due deliveries (retries, digests that came due). */
|
|
14
|
-
retryTasks?: readonly WorkerTask[]
|
|
15
|
-
/** Run daily at 03:00 beside the purge of old delivery history. */
|
|
16
|
-
purgeTasks?: readonly WorkerTask[]
|
|
17
|
-
/** Prefix of the line logged when the worker starts. */
|
|
18
|
-
logPrefix?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Sends the deliveries that came due (retries and digests) every 30 seconds and purges old history daily. Does not
|
|
23
|
-
* wait for a pass to finish before the next one is due; the engine takes care of overlapping passes.
|
|
24
|
-
*/
|
|
25
|
-
export function createDeliveryWorker(options: DeliveryWorkerOptions) {
|
|
26
|
-
let retryTask: ScheduledTask | null = null
|
|
27
|
-
let purgeTask: ScheduledTask | null = null
|
|
28
|
-
|
|
29
|
-
const tasks = (list: readonly WorkerTask[]) => {
|
|
30
|
-
for (const task of list) task.run().catch((error) => console.error(task.failure, error))
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function start(): void {
|
|
34
|
-
if (retryTask || purgeTask) return
|
|
35
|
-
retryTask = cron.schedule('*/30 * * * * *', () => {
|
|
36
|
-
tasks([
|
|
37
|
-
{ failure: '[notifier] Retry worker failed:', run: () => options.engine.processDue() },
|
|
38
|
-
...(options.retryTasks ?? []),
|
|
39
|
-
])
|
|
40
|
-
})
|
|
41
|
-
purgeTask = cron.schedule('0 3 * * *', () => {
|
|
42
|
-
tasks([
|
|
43
|
-
{ failure: '[notifier] Delivery history purge failed:', run: () => options.engine.purgeOldDeliveries() },
|
|
44
|
-
...(options.purgeTasks ?? []),
|
|
45
|
-
])
|
|
46
|
-
})
|
|
47
|
-
console.log(`${options.logPrefix ?? '[notifier]'} Retry worker started`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function stop(): void {
|
|
51
|
-
retryTask?.destroy()
|
|
52
|
-
purgeTask?.destroy()
|
|
53
|
-
retryTask = null
|
|
54
|
-
purgeTask = null
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return { start, stop }
|
|
58
|
-
}
|
|
1
|
+
import cron, { type ScheduledTask } from 'node-cron'
|
|
2
|
+
import type { NotifyEngine } from './engine.js'
|
|
3
|
+
|
|
4
|
+
/** Work the application runs on the same ticks as the deliveries. A rejection is logged under `failure`, never thrown. */
|
|
5
|
+
export interface WorkerTask {
|
|
6
|
+
/** The line logged when the task fails (the error follows it). */
|
|
7
|
+
failure: string
|
|
8
|
+
run: () => Promise<unknown>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface DeliveryWorkerOptions {
|
|
12
|
+
engine: Pick<NotifyEngine, 'processDue' | 'purgeOldDeliveries'>
|
|
13
|
+
/** Run every 30 seconds beside the sending of due deliveries (retries, digests that came due). */
|
|
14
|
+
retryTasks?: readonly WorkerTask[]
|
|
15
|
+
/** Run daily at 03:00 beside the purge of old delivery history. */
|
|
16
|
+
purgeTasks?: readonly WorkerTask[]
|
|
17
|
+
/** Prefix of the line logged when the worker starts. */
|
|
18
|
+
logPrefix?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Sends the deliveries that came due (retries and digests) every 30 seconds and purges old history daily. Does not
|
|
23
|
+
* wait for a pass to finish before the next one is due; the engine takes care of overlapping passes.
|
|
24
|
+
*/
|
|
25
|
+
export function createDeliveryWorker(options: DeliveryWorkerOptions) {
|
|
26
|
+
let retryTask: ScheduledTask | null = null
|
|
27
|
+
let purgeTask: ScheduledTask | null = null
|
|
28
|
+
|
|
29
|
+
const tasks = (list: readonly WorkerTask[]) => {
|
|
30
|
+
for (const task of list) task.run().catch((error) => console.error(task.failure, error))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function start(): void {
|
|
34
|
+
if (retryTask || purgeTask) return
|
|
35
|
+
retryTask = cron.schedule('*/30 * * * * *', () => {
|
|
36
|
+
tasks([
|
|
37
|
+
{ failure: '[notifier] Retry worker failed:', run: () => options.engine.processDue() },
|
|
38
|
+
...(options.retryTasks ?? []),
|
|
39
|
+
])
|
|
40
|
+
})
|
|
41
|
+
purgeTask = cron.schedule('0 3 * * *', () => {
|
|
42
|
+
tasks([
|
|
43
|
+
{ failure: '[notifier] Delivery history purge failed:', run: () => options.engine.purgeOldDeliveries() },
|
|
44
|
+
...(options.purgeTasks ?? []),
|
|
45
|
+
])
|
|
46
|
+
})
|
|
47
|
+
console.log(`${options.logPrefix ?? '[notifier]'} Retry worker started`)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function stop(): void {
|
|
51
|
+
retryTask?.destroy()
|
|
52
|
+
purgeTask?.destroy()
|
|
53
|
+
retryTask = null
|
|
54
|
+
purgeTask = null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { start, stop }
|
|
58
|
+
}
|