@meith/tasks 0.16.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.md ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@meith/tasks",
3
+ "version": "0.16.0",
4
+ "license": "LGPL-3.0-or-later",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/meith-dev/meith.git",
8
+ "directory": "packages/tasks"
9
+ },
10
+ "type": "module",
11
+ "main": "./src/index.ts",
12
+ "types": "./src/index.ts",
13
+ "files": [
14
+ "src",
15
+ "!src/**/*.test.*",
16
+ "!src/**/*.type-test.*"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ }
21
+ }
package/src/builtin.ts ADDED
@@ -0,0 +1,455 @@
1
+ import type { TaskDefinition } from './types'
2
+
3
+ export interface TaskWorkers {
4
+ relayOutbox(batchSize: number): Promise<number>
5
+ deliverWebhooks(batchSize: number): Promise<{
6
+ readonly attempted: number
7
+ readonly delivered: number
8
+ readonly retried: number
9
+ readonly dead: number
10
+ }>
11
+ drainQueue(batchSize: number, signal: AbortSignal): Promise<number>
12
+ pruneSessions(): Promise<number>
13
+ pruneExpiredTokens(): Promise<number>
14
+ pruneRateLimits(): Promise<number>
15
+ pruneAuthEvents(): Promise<number>
16
+ reconcileCounters(batchSize: number): Promise<number>
17
+ flushThreadViews(batchSize: number): Promise<number>
18
+ backfillPostRenders(batchSize: number): Promise<number>
19
+ reindexSearch(batchSize: number): Promise<number>
20
+ refreshMarketplaceCatalog(): Promise<{ ok: boolean; listingCount: number; notified: number }>
21
+ applyPromotions(batchSize: number): Promise<number>
22
+ expireBans(batchSize: number): Promise<number>
23
+ expireGroupMemberships(batchSize: number): Promise<number>
24
+ expireWarnings(batchSize: number): Promise<number>
25
+ notifySubscribers(batchSize: number, signal: AbortSignal): Promise<number>
26
+ sendDigests(batchSize: number, signal: AbortSignal): Promise<number>
27
+ sweepAttachments(batchSize: number): Promise<{ deleted: number; failed: number }>
28
+ sweepAvatars(batchSize: number): Promise<number>
29
+ rollUpStatistics(): Promise<{ memberCount: number; online: number; record: boolean }>
30
+ }
31
+
32
+ function allDefinitions(workers: TaskWorkers): TaskDefinition[] {
33
+ return [
34
+ {
35
+ id: 'outbox.relay',
36
+ title: 'Relay domain events',
37
+ titleKey: 'adminSystem.task.outboxRelay.title',
38
+ description:
39
+ 'Moves committed events from the outbox onto the queue. Runs most ' +
40
+ 'frequently because user-visible side effects (notifications, search ' +
41
+ 'indexing) wait on it.',
42
+ descriptionKey: 'adminSystem.task.outboxRelay.description',
43
+ intervalSeconds: 60,
44
+ maxDurationSeconds: 30,
45
+ async run() {
46
+ const relayed = await workers.relayOutbox(200)
47
+ return { detail: { relayed } }
48
+ },
49
+ },
50
+
51
+ {
52
+ id: 'webhooks.deliver',
53
+ title: 'Deliver queued webhooks',
54
+ titleKey: 'adminSystem.task.webhooksDeliver.title',
55
+ description:
56
+ 'Sends pending webhook deliveries to their subscribers, then records ' +
57
+ 'each verdict: delivered, retried with backoff, or dead-lettered.',
58
+ descriptionKey: 'adminSystem.task.webhooksDeliver.description',
59
+ intervalSeconds: 60,
60
+ maxDurationSeconds: 240,
61
+ async run() {
62
+ const result = await workers.deliverWebhooks(20)
63
+ return { detail: { ...result } }
64
+ },
65
+ },
66
+
67
+ {
68
+ id: 'queue.drain',
69
+ title: 'Process queued jobs',
70
+ titleKey: 'adminSystem.task.queueDrain.title',
71
+ description:
72
+ 'Executes due jobs. Bounded per run: a large backlog is drained across ' +
73
+ 'several ticks rather than risking a function timeout mid-job. Stops ' +
74
+ 'between jobs when its budget is spent, handing back the jobs it did ' +
75
+ 'not reach.',
76
+ descriptionKey: 'adminSystem.task.queueDrain.description',
77
+ intervalSeconds: 60,
78
+ maxDurationSeconds: 45,
79
+ async run({ signal }) {
80
+ const processed = await workers.drainQueue(50, signal)
81
+ return { detail: { processed } }
82
+ },
83
+ },
84
+
85
+ {
86
+ id: 'sessions.prune',
87
+ title: 'Prune idle sessions',
88
+ titleKey: 'adminSystem.task.sessionsPrune.title',
89
+ description:
90
+ 'Deletes sessions past the idle timeout. Purely subtractive, so running ' +
91
+ 'it twice removes nothing extra.',
92
+ descriptionKey: 'adminSystem.task.sessionsPrune.description',
93
+ intervalSeconds: 3600,
94
+ maxDurationSeconds: 30,
95
+ async run() {
96
+ const removed = await workers.pruneSessions()
97
+ return { detail: { removed } }
98
+ },
99
+ },
100
+
101
+ {
102
+ id: 'tokens.prune',
103
+ title: 'Prune expired tokens',
104
+ titleKey: 'adminSystem.task.tokensPrune.title',
105
+ description: 'Clears spent activation and password-reset tokens.',
106
+ descriptionKey: 'adminSystem.task.tokensPrune.description',
107
+ intervalSeconds: 3600,
108
+ maxDurationSeconds: 30,
109
+ async run() {
110
+ const removed = await workers.pruneExpiredTokens()
111
+ return { detail: { removed } }
112
+ },
113
+ },
114
+
115
+ {
116
+ id: 'ratelimits.prune',
117
+ title: 'Prune rate-limit counters',
118
+ titleKey: 'adminSystem.task.rateLimitsPrune.title',
119
+ description:
120
+ 'Drops anti-spam counters whose window has passed. They are bookkeeping ' +
121
+ 'and grow with traffic rather than with content, so nothing is lost and ' +
122
+ 'a busy board would otherwise keep a row per member per hour forever.',
123
+ descriptionKey: 'adminSystem.task.rateLimitsPrune.description',
124
+ intervalSeconds: 3600,
125
+ maxDurationSeconds: 30,
126
+ async run() {
127
+ const removed = await workers.pruneRateLimits()
128
+ return { detail: { removed } }
129
+ },
130
+ },
131
+
132
+ {
133
+ id: 'authevents.prune',
134
+ title: 'Prune sign-in activity',
135
+ titleKey: 'adminSystem.task.authEventsPrune.title',
136
+ description:
137
+ 'Drops sign-in activity older than the retention the board is set to ' +
138
+ 'keep. Does nothing at all while that setting is 0, which is the ' +
139
+ 'default: the log is an audit trail, and one that expires without ' +
140
+ 'being asked to is worse than one that grows.',
141
+ descriptionKey: 'adminSystem.task.authEventsPrune.description',
142
+ intervalSeconds: 3600,
143
+ maxDurationSeconds: 30,
144
+ async run() {
145
+ const removed = await workers.pruneAuthEvents()
146
+ return { detail: { removed } }
147
+ },
148
+ },
149
+
150
+ {
151
+ id: 'counters.reconcile',
152
+ title: 'Reconcile denormalised counters',
153
+ titleKey: 'adminSystem.task.countersReconcile.title',
154
+ description:
155
+ 'Recomputes forum and thread counters from source rows. Counters are ' +
156
+ 'maintained incrementally on the write path for speed; this is the ' +
157
+ 'safety net that repairs drift from a crashed request or a bad import. ' +
158
+ 'Idempotent by construction — it writes a computed truth, not a delta.',
159
+ descriptionKey: 'adminSystem.task.countersReconcile.description',
160
+ intervalSeconds: 21_600,
161
+ maxDurationSeconds: 60,
162
+ async run() {
163
+ const corrected = await workers.reconcileCounters(500)
164
+ return { detail: { corrected } }
165
+ },
166
+ },
167
+
168
+ {
169
+ id: 'views.flush',
170
+ title: 'Flush buffered thread views',
171
+ titleKey: 'adminSystem.task.viewsFlush.title',
172
+ description:
173
+ 'Applies buffered view counts to their threads. Views are buffered on ' +
174
+ 'the write path so a busy thread does not rewrite the row behind the ' +
175
+ 'listing index on every page view; this is where they land. Bounded, ' +
176
+ 'and a skipped run only delays the numbers.',
177
+ descriptionKey: 'adminSystem.task.viewsFlush.description',
178
+ intervalSeconds: 300,
179
+ maxDurationSeconds: 30,
180
+ async run() {
181
+ const flushed = await workers.flushThreadViews(500)
182
+ return { detail: { flushed } }
183
+ },
184
+ },
185
+
186
+ {
187
+ id: 'posts.render_backfill',
188
+ title: 'Re-render stale post bodies',
189
+ titleKey: 'adminSystem.task.postsRenderBackfill.title',
190
+ description:
191
+ 'Rewrites the stored HTML of posts rendered by an older version of the ' +
192
+ 'renderer, and converts any body still stored as BBCode. A stale render ' +
193
+ 'is rendered live on read, so this never gates correctness — it stops a renderer ' +
194
+ 'change, or an import that wrote none, from making every thread page ' +
195
+ 'pay for it. Idempotent, and its own progress marker is the row.',
196
+ descriptionKey: 'adminSystem.task.postsRenderBackfill.description',
197
+ intervalSeconds: 600,
198
+ maxDurationSeconds: 45,
199
+ async run() {
200
+ const rendered = await workers.backfillPostRenders(200)
201
+ return { detail: { rendered } }
202
+ },
203
+ },
204
+
205
+ {
206
+ id: 'search.reindex',
207
+ title: 'Build the search index for posts that have none',
208
+ titleKey: 'adminSystem.task.searchReindex.title',
209
+ description:
210
+ 'Writes the search document for posts that were never indexed, or were ' +
211
+ 'indexed under an older definition of it. The write path indexes every post ' +
212
+ 'it creates, so this is only ever a catch-up: an import, a seeded board, a ' +
213
+ 'board adopting search, or a release that changed what the document holds.',
214
+ descriptionKey: 'adminSystem.task.searchReindex.description',
215
+ intervalSeconds: 600,
216
+ maxDurationSeconds: 45,
217
+ async run() {
218
+ const indexed = await workers.reindexSearch(200)
219
+ return { detail: { indexed } }
220
+ },
221
+ },
222
+
223
+ {
224
+ id: 'marketplace.refresh_catalog',
225
+ title: 'Refresh the plugin and theme catalog',
226
+ titleKey: 'adminSystem.task.marketplaceRefreshCatalog.title',
227
+ description:
228
+ 'Fetches the marketplace feed, validates it and caches it for the admin ' +
229
+ "panel's Browse tab, then notifies administrators once per plugin version " +
230
+ 'that becomes available for the first time. A board with no outbound network ' +
231
+ 'fails this quietly — logged, not alarmed — and the Browse tab falls back to a ' +
232
+ 'plain note that the catalog is unreachable.',
233
+ descriptionKey: 'adminSystem.task.marketplaceRefreshCatalog.description',
234
+ intervalSeconds: 86_400,
235
+ maxDurationSeconds: 30,
236
+ async run() {
237
+ const { ok, listingCount, notified } = await workers.refreshMarketplaceCatalog()
238
+ return { detail: { ok: ok ? 1 : 0, listingCount, notified } }
239
+ },
240
+ },
241
+
242
+ {
243
+ id: 'promotions.apply',
244
+ title: 'Apply group promotions',
245
+ titleKey: 'adminSystem.task.promotionsApply.title',
246
+ description:
247
+ 'Moves users into groups whose promotion criteria they now meet. ' +
248
+ 'Evaluates current post counts and registration age rather than tracking ' +
249
+ 'thresholds crossed, so a missed run catches up on the next tick.',
250
+ descriptionKey: 'adminSystem.task.promotionsApply.description',
251
+ intervalSeconds: 21_600,
252
+ maxDurationSeconds: 60,
253
+ async run() {
254
+ const promoted = await workers.applyPromotions(500)
255
+ return { detail: { promoted } }
256
+ },
257
+ },
258
+
259
+ {
260
+ id: 'bans.expire',
261
+ title: 'Expire temporary bans',
262
+ titleKey: 'adminSystem.task.bansExpire.title',
263
+ description:
264
+ 'Lifts bans whose expiry has passed, restoring each user to the group ' +
265
+ 'they held when banned. Acts on outstanding state rather than on what ' +
266
+ 'expired since the last run, so a skipped day costs a delay and nothing ' +
267
+ 'else, and a doubled tick lifts nothing twice.',
268
+ descriptionKey: 'adminSystem.task.bansExpire.description',
269
+ intervalSeconds: 900,
270
+ maxDurationSeconds: 30,
271
+ async run() {
272
+ const lifted = await workers.expireBans(200)
273
+ return { detail: { lifted } }
274
+ },
275
+ },
276
+
277
+ {
278
+ id: 'groups.expire',
279
+ title: 'Expire timed group memberships',
280
+ titleKey: 'adminSystem.task.groupsExpire.title',
281
+ description:
282
+ 'Deletes secondary group memberships whose expiry has passed and bumps ' +
283
+ 'the permission version so derived caches follow. Actor assembly already ' +
284
+ 'excludes a lapsed row, so access ended on time regardless — this tidies ' +
285
+ 'the table rather than enforcing the boundary, and a doubled tick ' +
286
+ 'deletes nothing twice.',
287
+ descriptionKey: 'adminSystem.task.groupsExpire.description',
288
+ intervalSeconds: 900,
289
+ maxDurationSeconds: 30,
290
+ async run() {
291
+ const removed = await workers.expireGroupMemberships(200)
292
+ return { detail: { removed } }
293
+ },
294
+ },
295
+
296
+ {
297
+ id: 'warnings.expire',
298
+ title: 'Expire warnings',
299
+ titleKey: 'adminSystem.task.warningsExpire.title',
300
+ description:
301
+ 'Recomputes the points total of every member whose cached total still ' +
302
+ 'counts a warning that has passed its expiry date, and re-evaluates the ' +
303
+ 'restriction their level implies — which is how a suspension ends when ' +
304
+ 'the warning behind it ages out. The live-warning predicate already ' +
305
+ 'excludes an expired row, so this corrects the cache rather than ' +
306
+ 'creating truth: a board whose tick has been down still reports honest ' +
307
+ 'totals the moment anything recalculates.',
308
+ descriptionKey: 'adminSystem.task.warningsExpire.description',
309
+ intervalSeconds: 3_600,
310
+ maxDurationSeconds: 60,
311
+ async run() {
312
+ const expired = await workers.expireWarnings(200)
313
+ return { detail: { expired } }
314
+ },
315
+ },
316
+ {
317
+ id: 'subscriptions.instant',
318
+ title: 'Notify subscribers',
319
+ titleKey: 'adminSystem.task.subscriptionsInstant.title',
320
+ description:
321
+ 'Tells members who follow a thread or forum "as it happens" about ' +
322
+ 'posts that have arrived since they were last told. Runs on the ' +
323
+ 'shortest interval the scheduler has, which is what "instant" means ' +
324
+ 'here: fanning out inside the posting request would put an unbounded ' +
325
+ "loop — one permission check per subscriber — on the board's hottest " +
326
+ 'write, and couple posting to the mail provider being up. Each ' +
327
+ 'subscription carries a watermark, so a skipped tick delays a ' +
328
+ 'notification and never loses one — which is also what lets it stop ' +
329
+ 'between members when its budget is spent.',
330
+ descriptionKey: 'adminSystem.task.subscriptionsInstant.description',
331
+ intervalSeconds: 60,
332
+ maxDurationSeconds: 45,
333
+ async run({ signal }) {
334
+ const notified = await workers.notifySubscribers(50, signal)
335
+ return { detail: { notified } }
336
+ },
337
+ },
338
+
339
+ {
340
+ id: 'subscriptions.digest',
341
+ title: 'Send subscription digests',
342
+ titleKey: 'adminSystem.task.subscriptionsDigest.title',
343
+ description:
344
+ 'Sends the daily and weekly digests that are due. The clock is per ' +
345
+ 'member and per cadence rather than per run, so somebody who ' +
346
+ 'subscribed on Sunday gets their first weekly digest a week later — ' +
347
+ 'not at whatever moment the board tick happened to fire. Hourly, ' +
348
+ 'because a digest that is due is a query that finds nobody most of the ' +
349
+ 'time, and asking often is what keeps "daily" within an hour of the ' +
350
+ 'same time each day.',
351
+ descriptionKey: 'adminSystem.task.subscriptionsDigest.description',
352
+ intervalSeconds: 3_600,
353
+ maxDurationSeconds: 60,
354
+ async run({ signal }) {
355
+ const notified = await workers.sendDigests(50, signal)
356
+ return { detail: { notified } }
357
+ },
358
+ },
359
+
360
+ {
361
+ id: 'attachments.sweep',
362
+ title: 'Collect abandoned attachment files',
363
+ titleKey: 'adminSystem.task.attachmentsSweep.title',
364
+ description:
365
+ 'Deletes objects in the file store that nothing owns, and fails ' +
366
+ 'uploads whose re-encoding never finished. An object key is recorded ' +
367
+ 'before its bytes are written and forgotten when a row takes ownership, ' +
368
+ 'so what this collects is exactly what a crash between those two steps ' +
369
+ 'left behind — a question that is an indexed query here and a full ' +
370
+ 'bucket listing anywhere else. Purely subtractive, and bounded by a ' +
371
+ 'grace period, so an upload in flight is never collected out from ' +
372
+ 'under itself.',
373
+ descriptionKey: 'adminSystem.task.attachmentsSweep.description',
374
+ intervalSeconds: 3_600,
375
+ maxDurationSeconds: 60,
376
+ async run() {
377
+ const { deleted, failed } = await workers.sweepAttachments(200)
378
+ return { detail: { deleted, failed } }
379
+ },
380
+ },
381
+
382
+ {
383
+ id: 'avatars.sweep',
384
+ title: 'Fail unfinished avatar uploads',
385
+ titleKey: 'adminSystem.task.avatarsSweep.title',
386
+ description:
387
+ 'Marks avatar uploads whose re-encode never finished, so the member is ' +
388
+ 'told to try again rather than left looking at a spinner that is not ' +
389
+ 'there. Separate from the attachment sweep because it acts on `users` ' +
390
+ 'rather than on the object ledger, and because an operator asking why ' +
391
+ 'an avatar is stuck should not have to read a task about attachments. ' +
392
+ 'Purely corrective, and safe to run twice.',
393
+ descriptionKey: 'adminSystem.task.avatarsSweep.description',
394
+ intervalSeconds: 3_600,
395
+ maxDurationSeconds: 60,
396
+ async run() {
397
+ const failed = await workers.sweepAvatars(200)
398
+ return { detail: { failed } }
399
+ },
400
+ },
401
+
402
+ {
403
+ id: 'stats.rollup',
404
+ title: 'Roll up board statistics',
405
+ titleKey: 'adminSystem.task.statsRollup.title',
406
+ description:
407
+ 'Recomputes the board totals shown on the index and raises the ' +
408
+ '"most ever online" record when the current count has beaten it. The ' +
409
+ 'totals are computed here rather than per page view because the member ' +
410
+ 'count is a count of `users`, and the index is the most-requested page ' +
411
+ 'on the board. The page shows when this last ran, so a number ten ' +
412
+ 'minutes old is honest rather than wrong. Writes a computed truth, not ' +
413
+ 'a delta, so a skipped tick costs freshness and a doubled one costs ' +
414
+ 'nothing.',
415
+ descriptionKey: 'adminSystem.task.statsRollup.description',
416
+ intervalSeconds: 300,
417
+ maxDurationSeconds: 30,
418
+ async run() {
419
+ const { memberCount, online, record } = await workers.rollUpStatistics()
420
+ return { detail: { memberCount, online, record: record ? 1 : 0 } }
421
+ },
422
+ },
423
+ ]
424
+ }
425
+
426
+ const REQUIRED_WORKER: Readonly<Record<string, keyof TaskWorkers>> = {
427
+ 'outbox.relay': 'relayOutbox',
428
+ 'webhooks.deliver': 'deliverWebhooks',
429
+ 'queue.drain': 'drainQueue',
430
+ 'sessions.prune': 'pruneSessions',
431
+ 'tokens.prune': 'pruneExpiredTokens',
432
+ 'ratelimits.prune': 'pruneRateLimits',
433
+ 'authevents.prune': 'pruneAuthEvents',
434
+ 'counters.reconcile': 'reconcileCounters',
435
+ 'views.flush': 'flushThreadViews',
436
+ 'posts.render_backfill': 'backfillPostRenders',
437
+ 'search.reindex': 'reindexSearch',
438
+ 'marketplace.refresh_catalog': 'refreshMarketplaceCatalog',
439
+ 'promotions.apply': 'applyPromotions',
440
+ 'bans.expire': 'expireBans',
441
+ 'groups.expire': 'expireGroupMemberships',
442
+ 'warnings.expire': 'expireWarnings',
443
+ 'subscriptions.instant': 'notifySubscribers',
444
+ 'subscriptions.digest': 'sendDigests',
445
+ 'attachments.sweep': 'sweepAttachments',
446
+ 'avatars.sweep': 'sweepAvatars',
447
+ 'stats.rollup': 'rollUpStatistics',
448
+ }
449
+
450
+ export function builtinTasks(workers: Partial<TaskWorkers>): TaskDefinition[] {
451
+ const supplied = workers as TaskWorkers
452
+ return allDefinitions(supplied).filter(
453
+ (task) => typeof supplied[REQUIRED_WORKER[task.id] as keyof TaskWorkers] === 'function',
454
+ )
455
+ }
package/src/health.ts ADDED
@@ -0,0 +1,62 @@
1
+ export interface TaskHealthInput {
2
+ readonly key: string
3
+ readonly intervalSeconds: number
4
+ readonly enabled: boolean
5
+ readonly lastRunAt: Date | null
6
+ readonly nextRunAt: Date | null
7
+ readonly consecutiveFailures: number
8
+ }
9
+
10
+ export type TaskHealthStatus = 'healthy' | 'late' | 'stale' | 'failing' | 'disabled' | 'never-run'
11
+
12
+ export interface TaskHealth extends TaskHealthInput {
13
+ readonly status: TaskHealthStatus
14
+ readonly ageSeconds: number | null
15
+ readonly intervalsLate: number
16
+ }
17
+
18
+ export const STALE_INTERVALS = 3
19
+
20
+ export const FAILING_THRESHOLD = 3
21
+
22
+ export function assessTask(task: TaskHealthInput, now: Date): TaskHealth {
23
+ const ageSeconds =
24
+ task.lastRunAt === null
25
+ ? null
26
+ : Math.max(0, Math.floor((now.getTime() - task.lastRunAt.getTime()) / 1000))
27
+
28
+ const interval = Math.max(1, task.intervalSeconds)
29
+ const intervalsLate = ageSeconds === null ? 0 : Math.floor(ageSeconds / interval)
30
+
31
+ const status = ((): TaskHealthStatus => {
32
+ if (!task.enabled) return 'disabled'
33
+ if (task.consecutiveFailures >= FAILING_THRESHOLD) return 'failing'
34
+ if (task.lastRunAt === null) return 'never-run'
35
+ if (intervalsLate >= STALE_INTERVALS) return 'stale'
36
+ if (intervalsLate >= 1) return 'late'
37
+ return 'healthy'
38
+ })()
39
+
40
+ return { ...task, status, ageSeconds, intervalsLate }
41
+ }
42
+
43
+ export interface SchedulerHealth {
44
+ readonly tasks: readonly TaskHealth[]
45
+ readonly schedulerStopped: boolean
46
+ readonly stale: number
47
+ readonly failing: number
48
+ }
49
+
50
+ export function assessScheduler(tasks: readonly TaskHealthInput[], now: Date): SchedulerHealth {
51
+ const assessed = tasks.map((task) => assessTask(task, now))
52
+ const enabled = assessed.filter((task) => task.status !== 'disabled')
53
+
54
+ return {
55
+ tasks: assessed,
56
+ schedulerStopped:
57
+ enabled.length > 0 &&
58
+ enabled.every((task) => task.status === 'stale' || task.status === 'never-run'),
59
+ stale: assessed.filter((task) => task.status === 'stale').length,
60
+ failing: assessed.filter((task) => task.status === 'failing').length,
61
+ }
62
+ }
package/src/index.ts ADDED
@@ -0,0 +1,23 @@
1
+ export { builtinTasks, type TaskWorkers } from './builtin'
2
+ export {
3
+ assessScheduler,
4
+ assessTask,
5
+ FAILING_THRESHOLD,
6
+ type SchedulerHealth,
7
+ STALE_INTERVALS,
8
+ type TaskHealth,
9
+ type TaskHealthInput,
10
+ type TaskHealthStatus,
11
+ } from './health'
12
+ export {
13
+ type TaskRepository,
14
+ type TickDeps,
15
+ type TickOutcome,
16
+ tick,
17
+ } from './scheduler'
18
+ export type {
19
+ TaskContext,
20
+ TaskDefinition,
21
+ TaskResult,
22
+ TaskRunRecord,
23
+ } from './types'
@@ -0,0 +1,129 @@
1
+ import type { TaskContext, TaskDefinition, TaskResult } from './types'
2
+
3
+ export interface TaskRepository {
4
+ claim(input: {
5
+ taskId: string
6
+ now: Date
7
+ staleBefore: Date
8
+ }): Promise<{ previousLastRunAt: Date | null } | null>
9
+
10
+ release(input: {
11
+ taskId: string
12
+ finishedAt: Date
13
+ success: boolean
14
+ detail?: Record<string, unknown>
15
+ error?: string
16
+ }): Promise<void>
17
+
18
+ ensureRegistered(tasks: readonly { id: string; intervalSeconds: number }[]): Promise<void>
19
+ }
20
+
21
+ export interface TickOutcome {
22
+ taskId: string
23
+ status: 'ran' | 'skipped' | 'failed'
24
+ durationMs: number
25
+ overran?: true
26
+ detail?: Record<string, unknown>
27
+ error?: string
28
+ }
29
+
30
+ export interface TickDeps {
31
+ repository: TaskRepository
32
+ tasks: readonly TaskDefinition[]
33
+ now?: Date
34
+ staleClaimSeconds?: number
35
+ onError?: (taskId: string, error: unknown) => void
36
+ signal?: AbortSignal
37
+ }
38
+
39
+ export async function tick({
40
+ repository,
41
+ tasks,
42
+ now = new Date(),
43
+ staleClaimSeconds = 900,
44
+ onError,
45
+ signal,
46
+ }: TickDeps): Promise<TickOutcome[]> {
47
+ await repository.ensureRegistered(
48
+ tasks.map((t) => ({ id: t.id, intervalSeconds: t.intervalSeconds })),
49
+ )
50
+
51
+ const outcomes: TickOutcome[] = []
52
+ const staleBefore = new Date(now.getTime() - staleClaimSeconds * 1000)
53
+
54
+ for (const task of tasks) {
55
+ const startedAt = Date.now()
56
+
57
+ if (signal?.aborted === true) {
58
+ outcomes.push({ taskId: task.id, status: 'skipped', durationMs: 0 })
59
+ continue
60
+ }
61
+
62
+ const claim = await repository.claim({ taskId: task.id, now, staleBefore })
63
+
64
+ if (!claim) {
65
+ outcomes.push({ taskId: task.id, status: 'skipped', durationMs: 0 })
66
+ continue
67
+ }
68
+
69
+ const budget = new AbortController()
70
+ const timeout = setTimeout(
71
+ () => budget.abort(new Error(`Task "${task.id}" exceeded its budget`)),
72
+ task.maxDurationSeconds * 1000,
73
+ )
74
+
75
+ const elapsedSince = () => new Date(now.getTime() + (Date.now() - startedAt))
76
+
77
+ try {
78
+ const elapsedSeconds = claim.previousLastRunAt
79
+ ? Math.max(0, (now.getTime() - claim.previousLastRunAt.getTime()) / 1000)
80
+ : task.intervalSeconds
81
+
82
+ const context: TaskContext = {
83
+ now,
84
+ lastRunAt: claim.previousLastRunAt,
85
+ elapsedSeconds,
86
+ signal: signal === undefined ? budget.signal : AbortSignal.any([signal, budget.signal]),
87
+ }
88
+
89
+ const result: TaskResult = await task.run(context)
90
+
91
+ await repository.release({
92
+ taskId: task.id,
93
+ finishedAt: elapsedSince(),
94
+ success: true,
95
+ ...(result.detail ? { detail: result.detail } : {}),
96
+ })
97
+
98
+ outcomes.push({
99
+ taskId: task.id,
100
+ status: 'ran',
101
+ durationMs: Date.now() - startedAt,
102
+ ...(budget.signal.aborted ? { overran: true as const } : {}),
103
+ ...(result.detail ? { detail: result.detail } : {}),
104
+ })
105
+ } catch (error) {
106
+ const message = error instanceof Error ? error.message : String(error)
107
+ onError?.(task.id, error)
108
+
109
+ await repository.release({
110
+ taskId: task.id,
111
+ finishedAt: elapsedSince(),
112
+ success: false,
113
+ error: message,
114
+ })
115
+
116
+ outcomes.push({
117
+ taskId: task.id,
118
+ status: 'failed',
119
+ durationMs: Date.now() - startedAt,
120
+ ...(budget.signal.aborted ? { overran: true as const } : {}),
121
+ error: message,
122
+ })
123
+ } finally {
124
+ clearTimeout(timeout)
125
+ }
126
+ }
127
+
128
+ return outcomes
129
+ }
package/src/types.ts ADDED
@@ -0,0 +1,30 @@
1
+ export interface TaskContext {
2
+ now: Date
3
+ lastRunAt: Date | null
4
+ elapsedSeconds: number
5
+ signal: AbortSignal
6
+ }
7
+
8
+ export interface TaskResult {
9
+ detail?: Record<string, number | string>
10
+ }
11
+
12
+ export interface TaskDefinition {
13
+ readonly id: string
14
+ readonly title: string
15
+ readonly description: string
16
+ readonly titleKey?: string
17
+ readonly descriptionKey?: string
18
+ readonly intervalSeconds: number
19
+ readonly maxDurationSeconds: number
20
+ run(context: TaskContext): Promise<TaskResult>
21
+ }
22
+
23
+ export interface TaskRunRecord {
24
+ taskId: string
25
+ startedAt: Date
26
+ finishedAt: Date | null
27
+ success: boolean | null
28
+ detail: Record<string, unknown> | null
29
+ error: string | null
30
+ }