@evanp/activitypub-bot 0.45.13 → 0.45.15
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 +20 -0
- package/lib/activitydeliverer.js +14 -2
- package/lib/activitydistributor.js +71 -2
- package/lib/fanoutworker.js +14 -2
- package/lib/intakeworker.js +14 -2
- package/lib/jobqueue.js +19 -6
- package/lib/migrations/011-job-last-error.js +12 -0
- package/lib/requestthrottler.js +6 -1
- package/lib/worker.js +12 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,26 @@ and this project adheres to
|
|
|
9
9
|
|
|
10
10
|
## [Unreleased]
|
|
11
11
|
|
|
12
|
+
## [0.45.15] - 2026-05-11
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- Heuristic to check if an integer in X-RateLimit-Reset is probably an offset or
|
|
17
|
+
probably a Unix epoch in seconds. Anything less than 30 days is treated as an
|
|
18
|
+
offset; otherwise it's an epoch.
|
|
19
|
+
- Fix job queue to clamp job retry delays between 0 and 30 days.
|
|
20
|
+
- Fix workers to continue operating even if retry or fail calls
|
|
21
|
+
fail.
|
|
22
|
+
|
|
23
|
+
## [0.45.14] - 2026-05-10
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- Depend on inbox forwarding for distribution to remote collections.
|
|
28
|
+
- More robust handling of client request failures in delivery and distribution.
|
|
29
|
+
- More robust handling of intake, fanout throttle errors.
|
|
30
|
+
- Store error stack for failing jobs in queue job entries.
|
|
31
|
+
|
|
12
32
|
## [0.45.13] - 2026-05-10
|
|
13
33
|
|
|
14
34
|
### Changed
|
package/lib/activitydeliverer.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as ttlcachePkg from '@isaacs/ttlcache'
|
|
|
4
4
|
|
|
5
5
|
import as2 from './activitystreams.js'
|
|
6
6
|
import BotMaker from './botmaker.js'
|
|
7
|
+
import { ThrottleError } from './requestthrottler.js'
|
|
7
8
|
|
|
8
9
|
const TTLCache =
|
|
9
10
|
ttlcachePkg.TTLCache ?? ttlcachePkg.default ?? ttlcachePkg
|
|
@@ -128,6 +129,7 @@ export class ActivityDeliverer {
|
|
|
128
129
|
const deliveredTo = new Set()
|
|
129
130
|
const actor = this.getActor(activity)
|
|
130
131
|
const recipients = this.getRecipients(activity)
|
|
132
|
+
let fullActor
|
|
131
133
|
|
|
132
134
|
for (const recipient of recipients) {
|
|
133
135
|
this.#logger.debug({ recipient: recipient.id }, 'Checking recipient')
|
|
@@ -152,8 +154,18 @@ export class ActivityDeliverer {
|
|
|
152
154
|
)
|
|
153
155
|
}
|
|
154
156
|
} else {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
fullActor = fullActor ?? await this.#client.get(actor.id)
|
|
158
|
+
let fullRecipient
|
|
159
|
+
try {
|
|
160
|
+
fullRecipient = await this.#client.get(recipient.id)
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if (err instanceof ThrottleError) throw err
|
|
163
|
+
this.#logger.warn(
|
|
164
|
+
{ recipient: recipient.id },
|
|
165
|
+
'Unreachable remote recipient'
|
|
166
|
+
)
|
|
167
|
+
continue
|
|
168
|
+
}
|
|
157
169
|
if (await this.#isRemoteActor(fullRecipient)) {
|
|
158
170
|
this.#logger.warn({ recipient: recipient.id }, 'Skipping remote actor')
|
|
159
171
|
} else if (await this.#isRemoteFollowersCollection(fullActor, fullRecipient)) {
|
|
@@ -164,6 +164,14 @@ export class ActivityDistributor {
|
|
|
164
164
|
yield id
|
|
165
165
|
} else if (this.#isRemoteCollection(obj)) {
|
|
166
166
|
this.#logger.debug({ id }, 'Remote collection')
|
|
167
|
+
const owner = await this.#getCollectionOwner(obj, username)
|
|
168
|
+
if (owner && this.#isRecipient(activity, owner)) {
|
|
169
|
+
this.#logger.debug(
|
|
170
|
+
{ id, owner: owner.id },
|
|
171
|
+
'Leaving remote collection up to inbox forwarding'
|
|
172
|
+
)
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
167
175
|
try {
|
|
168
176
|
for await (const item of this.#client.items(obj.id, username)) {
|
|
169
177
|
this.#logger.debug({ id: item.id }, 'Remote collection member')
|
|
@@ -194,7 +202,16 @@ export class ActivityDistributor {
|
|
|
194
202
|
return sharedInbox
|
|
195
203
|
}
|
|
196
204
|
|
|
197
|
-
|
|
205
|
+
let obj
|
|
206
|
+
try {
|
|
207
|
+
obj = await this.#client.get(actorId, username)
|
|
208
|
+
} catch (err) {
|
|
209
|
+
this.#logger.warn(
|
|
210
|
+
{ actorId, username, err },
|
|
211
|
+
'Could not get actor in #getInbox'
|
|
212
|
+
)
|
|
213
|
+
return null
|
|
214
|
+
}
|
|
198
215
|
|
|
199
216
|
// Get the shared inbox if it exists
|
|
200
217
|
|
|
@@ -232,7 +249,16 @@ export class ActivityDistributor {
|
|
|
232
249
|
return directInbox
|
|
233
250
|
}
|
|
234
251
|
|
|
235
|
-
|
|
252
|
+
let obj
|
|
253
|
+
try {
|
|
254
|
+
obj = await this.#client.get(actorId, username)
|
|
255
|
+
} catch (err) {
|
|
256
|
+
this.#logger.warn(
|
|
257
|
+
{ actorId, username, err },
|
|
258
|
+
'Could not get actor in #getDirectInbox'
|
|
259
|
+
)
|
|
260
|
+
return null
|
|
261
|
+
}
|
|
236
262
|
|
|
237
263
|
if (!obj.inbox) {
|
|
238
264
|
return null
|
|
@@ -274,4 +300,47 @@ export class ActivityDistributor {
|
|
|
274
300
|
? obj.type.some(t => COLLECTION_TYPES.includes(t))
|
|
275
301
|
: COLLECTION_TYPES.includes(obj.type)
|
|
276
302
|
}
|
|
303
|
+
|
|
304
|
+
async #getCollectionOwner (obj, username) {
|
|
305
|
+
if (obj.attributedTo) {
|
|
306
|
+
return obj.attributedTo.first
|
|
307
|
+
} else {
|
|
308
|
+
let ownerId
|
|
309
|
+
let owner
|
|
310
|
+
|
|
311
|
+
if (obj.id.endsWith('/followers')) {
|
|
312
|
+
ownerId = obj.id.replace('/followers', '')
|
|
313
|
+
} else if (obj.id.endsWith('/following')) {
|
|
314
|
+
ownerId = obj.id.replace('/following', '')
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (ownerId) {
|
|
318
|
+
try {
|
|
319
|
+
owner = await this.#client.get(ownerId, username)
|
|
320
|
+
} catch (err) {
|
|
321
|
+
this.#logger.debug(
|
|
322
|
+
{ ownerId },
|
|
323
|
+
'Owner unreachable'
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return owner
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
#isRecipient (activity, obj) {
|
|
333
|
+
const props = ['to', 'cc', 'audience', 'bto', 'bcc']
|
|
334
|
+
for (const prop of props) {
|
|
335
|
+
const p = activity.get(prop)
|
|
336
|
+
if (p) {
|
|
337
|
+
for (const value of p) {
|
|
338
|
+
if (value.id === obj.id) {
|
|
339
|
+
return true
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return false
|
|
345
|
+
}
|
|
277
346
|
}
|
package/lib/fanoutworker.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
2
|
|
|
3
3
|
import as2 from './activitystreams.js'
|
|
4
|
-
import { Worker } from './worker.js'
|
|
4
|
+
import { Worker, RecoverableError } from './worker.js'
|
|
5
|
+
import { ThrottleError } from './requestthrottler.js'
|
|
5
6
|
|
|
6
7
|
export class FanoutWorker extends Worker {
|
|
7
8
|
#distributor
|
|
@@ -15,6 +16,17 @@ export class FanoutWorker extends Worker {
|
|
|
15
16
|
async doJob (payload, attempts) {
|
|
16
17
|
const activity = await as2.import(payload.activity)
|
|
17
18
|
const { username } = payload
|
|
18
|
-
|
|
19
|
+
try {
|
|
20
|
+
await this.#distributor.fanout(activity, username)
|
|
21
|
+
} catch (err) {
|
|
22
|
+
if (err instanceof ThrottleError) {
|
|
23
|
+
this._logger.warn(
|
|
24
|
+
{ err, activity: activity.id, username },
|
|
25
|
+
'Throttled fanout, waiting to retry')
|
|
26
|
+
throw new RecoverableError(err.message, err.waitTime)
|
|
27
|
+
} else {
|
|
28
|
+
throw err
|
|
29
|
+
}
|
|
30
|
+
}
|
|
19
31
|
}
|
|
20
32
|
}
|
package/lib/intakeworker.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
2
|
|
|
3
3
|
import as2 from './activitystreams.js'
|
|
4
|
-
import { Worker } from './worker.js'
|
|
4
|
+
import { Worker, RecoverableError } from './worker.js'
|
|
5
|
+
import { ThrottleError } from './requestthrottler.js'
|
|
5
6
|
|
|
6
7
|
export class IntakeWorker extends Worker {
|
|
7
8
|
#deliverer
|
|
@@ -17,6 +18,17 @@ export class IntakeWorker extends Worker {
|
|
|
17
18
|
async doJob (payload, attempts) {
|
|
18
19
|
const raw = payload.activity
|
|
19
20
|
const activity = await as2.import(raw)
|
|
20
|
-
|
|
21
|
+
try {
|
|
22
|
+
await this.#deliverer.deliverToAll(activity, this.#bots)
|
|
23
|
+
} catch (err) {
|
|
24
|
+
if (err instanceof ThrottleError) {
|
|
25
|
+
this._logger.warn(
|
|
26
|
+
{ err, activity: activity.id },
|
|
27
|
+
'Throttled intake, waiting to retry')
|
|
28
|
+
throw new RecoverableError(err.message, err.waitTime)
|
|
29
|
+
} else {
|
|
30
|
+
throw err
|
|
31
|
+
}
|
|
32
|
+
}
|
|
21
33
|
}
|
|
22
34
|
}
|
package/lib/jobqueue.js
CHANGED
|
@@ -6,6 +6,7 @@ import { QueryTypes } from 'sequelize'
|
|
|
6
6
|
|
|
7
7
|
const MAX_DELAY = 30000
|
|
8
8
|
const DEFAULT_PRIORITY = 1000000
|
|
9
|
+
const MAX_RETRY = 30 * 24 * 60 * 60 * 1000
|
|
9
10
|
|
|
10
11
|
export class JobQueue {
|
|
11
12
|
#connection
|
|
@@ -87,24 +88,28 @@ export class JobQueue {
|
|
|
87
88
|
this.#logger.debug({ method: 'release', jobRunnerId, jobId }, 'released job')
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
async retryAfter (jobId, jobRunnerId, delay) {
|
|
91
|
+
async retryAfter (jobId, jobRunnerId, delay, lastError = null) {
|
|
91
92
|
assert.ok(jobId)
|
|
92
93
|
assert.strictEqual(typeof jobId, 'string')
|
|
93
94
|
assert.ok(jobRunnerId)
|
|
94
95
|
assert.strictEqual(typeof jobRunnerId, 'string')
|
|
95
|
-
assert.ok(delay)
|
|
96
96
|
assert.strictEqual(typeof delay, 'number')
|
|
97
97
|
|
|
98
|
-
const retryAfter =
|
|
98
|
+
const retryAfter = (isNaN(delay))
|
|
99
|
+
? new Date(Date.now() + 1000)
|
|
100
|
+
: (!isFinite(delay))
|
|
101
|
+
? new Date(Date.now() + MAX_RETRY)
|
|
102
|
+
: new Date(Date.now() + Math.max(0, Math.min(delay, MAX_RETRY)))
|
|
99
103
|
|
|
100
104
|
await this.#connection.query(`
|
|
101
105
|
UPDATE job
|
|
102
106
|
SET claimed_by = NULL,
|
|
103
107
|
claimed_at = NULL,
|
|
104
108
|
updated_at = CURRENT_TIMESTAMP,
|
|
105
|
-
retry_after =
|
|
109
|
+
retry_after = ?,
|
|
110
|
+
last_error = ?
|
|
106
111
|
WHERE job_id = ? AND claimed_by = ?;`,
|
|
107
|
-
{ replacements: [retryAfter, jobId, jobRunnerId] }
|
|
112
|
+
{ replacements: [retryAfter, lastError, jobId, jobRunnerId] }
|
|
108
113
|
)
|
|
109
114
|
this.#logger.debug(
|
|
110
115
|
{ method: 'retry', jobRunnerId, jobId, delay },
|
|
@@ -157,7 +162,7 @@ export class JobQueue {
|
|
|
157
162
|
this.#ac.abort()
|
|
158
163
|
}
|
|
159
164
|
|
|
160
|
-
async fail (jobId, jobRunnerId) {
|
|
165
|
+
async fail (jobId, jobRunnerId, lastError = null) {
|
|
161
166
|
await this.#connection.query(`
|
|
162
167
|
INSERT INTO failed_job
|
|
163
168
|
(job_id, queue_id, priority, payload, claimed_at, claimed_by, attempts, retry_after, created_at, updated_at)
|
|
@@ -170,6 +175,14 @@ export class JobQueue {
|
|
|
170
175
|
DELETE FROM job
|
|
171
176
|
WHERE job_id = ? AND claimed_by = ?;`,
|
|
172
177
|
{ replacements: [jobId, jobRunnerId] })
|
|
178
|
+
|
|
179
|
+
if (lastError) {
|
|
180
|
+
await this.#connection.query(`
|
|
181
|
+
UPDATE failed_job
|
|
182
|
+
SET last_error = ?
|
|
183
|
+
WHERE job_id = ?;`,
|
|
184
|
+
{ replacements: [lastError, jobId] })
|
|
185
|
+
}
|
|
173
186
|
this.#logger.debug({ method: 'complete', jobRunnerId, jobId }, 'completed job')
|
|
174
187
|
}
|
|
175
188
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const id = '011-job-last-error'
|
|
2
|
+
|
|
3
|
+
export async function up (connection, queryOptions = {}) {
|
|
4
|
+
await connection.query(`
|
|
5
|
+
ALTER TABLE job
|
|
6
|
+
ADD COLUMN last_error TEXT;
|
|
7
|
+
`, queryOptions)
|
|
8
|
+
await connection.query(`
|
|
9
|
+
ALTER TABLE failed_job
|
|
10
|
+
ADD COLUMN last_error TEXT;
|
|
11
|
+
`, queryOptions)
|
|
12
|
+
}
|
package/lib/requestthrottler.js
CHANGED
|
@@ -2,6 +2,7 @@ import { setTimeout as sleep } from 'node:timers/promises'
|
|
|
2
2
|
import assert from 'node:assert'
|
|
3
3
|
|
|
4
4
|
const BETA = 0.75
|
|
5
|
+
const EPOCH_THRESHOLD = 30 * 24 * 60 * 60
|
|
5
6
|
|
|
6
7
|
export class ThrottleError extends Error {
|
|
7
8
|
constructor (message, waitTime) {
|
|
@@ -51,7 +52,11 @@ export class RequestThrottler {
|
|
|
51
52
|
let reset
|
|
52
53
|
if (resetHeader.match(/^\d+$/)) {
|
|
53
54
|
resetSeconds = parseInt(resetHeader)
|
|
54
|
-
|
|
55
|
+
if (resetSeconds < EPOCH_THRESHOLD) {
|
|
56
|
+
reset = new Date(Date.now() + (resetSeconds * 1000))
|
|
57
|
+
} else {
|
|
58
|
+
reset = new Date(resetSeconds * 1000)
|
|
59
|
+
}
|
|
55
60
|
} else {
|
|
56
61
|
reset = new Date(resetHeader)
|
|
57
62
|
resetSeconds = reset - Date.now()
|
package/lib/worker.js
CHANGED
|
@@ -55,7 +55,7 @@ export class Worker {
|
|
|
55
55
|
this.#logger.warn({ err, jobId }, 'error before job, retrying')
|
|
56
56
|
if (jobId) {
|
|
57
57
|
const delay = this.#retryDelay(attempts ?? 1)
|
|
58
|
-
await this.#jobQueue.retryAfter(jobId, this.#workerId, delay)
|
|
58
|
+
await this.#jobQueue.retryAfter(jobId, this.#workerId, delay, err.stack)
|
|
59
59
|
}
|
|
60
60
|
continue
|
|
61
61
|
}
|
|
@@ -68,10 +68,17 @@ export class Worker {
|
|
|
68
68
|
await this.doJob(payload, attempts)
|
|
69
69
|
await this.#jobQueue.complete(jobId, this.#workerId)
|
|
70
70
|
} catch (err) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
try {
|
|
72
|
+
if (err instanceof RecoverableError) {
|
|
73
|
+
await this.#jobQueue.retryAfter(jobId, this.#workerId, err.delay, err.stack)
|
|
74
|
+
} else {
|
|
75
|
+
await this.#jobQueue.fail(jobId, this.#workerId, err.stack)
|
|
76
|
+
}
|
|
77
|
+
} catch (queueError) {
|
|
78
|
+
this.#logger.error(
|
|
79
|
+
{ err: queueError, original: err, jobId, workerId: this.#workerId },
|
|
80
|
+
'job retry/fail error'
|
|
81
|
+
)
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
}
|