aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts10
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts9
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts8
-rw-r--r--server/lib/schedulers/abstract-scheduler.ts16
-rw-r--r--server/lib/schedulers/bad-actor-follow-scheduler.ts24
5 files changed, 63 insertions, 4 deletions
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts
index c20a48a4e..3f780e319 100644
--- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts
+++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts
@@ -1,5 +1,6 @@
1import { logger } from '../../../helpers/logger' 1import { logger } from '../../../helpers/logger'
2import { doRequest } from '../../../helpers/requests' 2import { doRequest } from '../../../helpers/requests'
3import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' 4import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
4 5
5async function process (payload: ActivityPubHttpPayload, jobId: number) { 6async function process (payload: ActivityPubHttpPayload, jobId: number) {
@@ -15,15 +16,22 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) {
15 httpSignature: httpSignatureOptions 16 httpSignature: httpSignatureOptions
16 } 17 }
17 18
19 const badUrls: string[] = []
20 const goodUrls: string[] = []
21
18 for (const uri of payload.uris) { 22 for (const uri of payload.uris) {
19 options.uri = uri 23 options.uri = uri
20 24
21 try { 25 try {
22 await doRequest(options) 26 await doRequest(options)
27 goodUrls.push(uri)
23 } catch (err) { 28 } catch (err) {
24 await maybeRetryRequestLater(err, payload, uri) 29 const isRetryingLater = await maybeRetryRequestLater(err, payload, uri)
30 if (isRetryingLater === false) badUrls.push(uri)
25 } 31 }
26 } 32 }
33
34 return ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes(goodUrls, badUrls, undefined)
27} 35}
28 36
29function onError (err: Error, jobId: number) { 37function onError (err: Error, jobId: number) {
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts
index d576cd42e..884ede5a3 100644
--- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts
+++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts
@@ -4,6 +4,7 @@ import { logger } from '../../../helpers/logger'
4import { getServerActor } from '../../../helpers/utils' 4import { getServerActor } from '../../../helpers/utils'
5import { ACTIVITY_PUB } from '../../../initializers' 5import { ACTIVITY_PUB } from '../../../initializers'
6import { ActorModel } from '../../../models/activitypub/actor' 6import { ActorModel } from '../../../models/activitypub/actor'
7import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7import { JobHandler, JobScheduler } from '../job-scheduler' 8import { JobHandler, JobScheduler } from '../job-scheduler'
8 9
9import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' 10import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
@@ -26,7 +27,7 @@ const jobCategory: JobCategory = 'activitypub-http'
26 27
27const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers) 28const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers)
28 29
29function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) { 30async function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) {
30 logger.warn('Cannot make request to %s.', uri, err) 31 logger.warn('Cannot make request to %s.', uri, err)
31 32
32 let attemptNumber = payload.attemptNumber || 1 33 let attemptNumber = payload.attemptNumber || 1
@@ -39,8 +40,12 @@ function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, ur
39 uris: [ uri ], 40 uris: [ uri ],
40 attemptNumber 41 attemptNumber
41 }) 42 })
42 return activitypubHttpJobScheduler.createJob(undefined, 'activitypubHttpUnicastHandler', newPayload) 43 await activitypubHttpJobScheduler.createJob(undefined, 'activitypubHttpUnicastHandler', newPayload)
44
45 return true
43 } 46 }
47
48 return false
44} 49}
45 50
46async function computeBody (payload: ActivityPubHttpPayload) { 51async function computeBody (payload: ActivityPubHttpPayload) {
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
index 175ec6642..e02bd698e 100644
--- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
+++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
@@ -1,5 +1,6 @@
1import { logger } from '../../../helpers/logger' 1import { logger } from '../../../helpers/logger'
2import { doRequest } from '../../../helpers/requests' 2import { doRequest } from '../../../helpers/requests'
3import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' 4import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
4 5
5async function process (payload: ActivityPubHttpPayload, jobId: number) { 6async function process (payload: ActivityPubHttpPayload, jobId: number) {
@@ -18,8 +19,13 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) {
18 19
19 try { 20 try {
20 await doRequest(options) 21 await doRequest(options)
22 await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([ uri ], [], undefined)
21 } catch (err) { 23 } catch (err) {
22 await maybeRetryRequestLater(err, payload, uri) 24 const isRetryingLater = await maybeRetryRequestLater(err, payload, uri)
25 if (isRetryingLater === false) {
26 await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([], [ uri ], undefined)
27 }
28
23 throw err 29 throw err
24 } 30 }
25} 31}
diff --git a/server/lib/schedulers/abstract-scheduler.ts b/server/lib/schedulers/abstract-scheduler.ts
new file mode 100644
index 000000000..473544ddf
--- /dev/null
+++ b/server/lib/schedulers/abstract-scheduler.ts
@@ -0,0 +1,16 @@
1import { SCHEDULER_INTERVAL } from '../../initializers'
2
3export abstract class AbstractScheduler {
4
5 private interval: NodeJS.Timer
6
7 enable () {
8 this.interval = setInterval(() => this.execute(), SCHEDULER_INTERVAL)
9 }
10
11 disable () {
12 clearInterval(this.interval)
13 }
14
15 protected abstract execute ()
16}
diff --git a/server/lib/schedulers/bad-actor-follow-scheduler.ts b/server/lib/schedulers/bad-actor-follow-scheduler.ts
new file mode 100644
index 000000000..c6c285ece
--- /dev/null
+++ b/server/lib/schedulers/bad-actor-follow-scheduler.ts
@@ -0,0 +1,24 @@
1import { logger } from '../../helpers/logger'
2import { ActorFollowModel } from '../../models/activitypub/actor-follow'
3import { AbstractScheduler } from './abstract-scheduler'
4
5export class BadActorFollowScheduler extends AbstractScheduler {
6
7 private static instance: AbstractScheduler
8
9 private constructor () {
10 super()
11 }
12
13 async execute () {
14 try {
15 await ActorFollowModel.removeBadActorFollows()
16 } catch (err) {
17 logger.error('Error in bad actor follows scheduler.', err)
18 }
19 }
20
21 static get Instance () {
22 return this.instance || (this.instance = new this())
23 }
24}