diff options
author | Chocobozzz <me@florianbigard.com> | 2018-01-11 09:35:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-01-11 09:35:50 +0100 |
commit | 60650c77c8a2a98e92d869b237ae4900f369a8fc (patch) | |
tree | 7304a7591b5b23b99d219c4d06c6bd5c4c58c1c2 /server/lib | |
parent | 7ae71355c40e9065f83d3fc77b6750d1929ac201 (diff) | |
download | PeerTube-60650c77c8a2a98e92d869b237ae4900f369a8fc.tar.gz PeerTube-60650c77c8a2a98e92d869b237ae4900f369a8fc.tar.zst PeerTube-60650c77c8a2a98e92d869b237ae4900f369a8fc.zip |
Add scores to follows and remove bad ones
Diffstat (limited to 'server/lib')
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 @@ | |||
1 | import { logger } from '../../../helpers/logger' | 1 | import { logger } from '../../../helpers/logger' |
2 | import { doRequest } from '../../../helpers/requests' | 2 | import { doRequest } from '../../../helpers/requests' |
3 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
3 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' | 4 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' |
4 | 5 | ||
5 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | 6 | async 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 | ||
29 | function onError (err: Error, jobId: number) { | 37 | function 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' | |||
4 | import { getServerActor } from '../../../helpers/utils' | 4 | import { getServerActor } from '../../../helpers/utils' |
5 | import { ACTIVITY_PUB } from '../../../initializers' | 5 | import { ACTIVITY_PUB } from '../../../initializers' |
6 | import { ActorModel } from '../../../models/activitypub/actor' | 6 | import { ActorModel } from '../../../models/activitypub/actor' |
7 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
7 | import { JobHandler, JobScheduler } from '../job-scheduler' | 8 | import { JobHandler, JobScheduler } from '../job-scheduler' |
8 | 9 | ||
9 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' | 10 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' |
@@ -26,7 +27,7 @@ const jobCategory: JobCategory = 'activitypub-http' | |||
26 | 27 | ||
27 | const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers) | 28 | const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers) |
28 | 29 | ||
29 | function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) { | 30 | async 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 | ||
46 | async function computeBody (payload: ActivityPubHttpPayload) { | 51 | async 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 @@ | |||
1 | import { logger } from '../../../helpers/logger' | 1 | import { logger } from '../../../helpers/logger' |
2 | import { doRequest } from '../../../helpers/requests' | 2 | import { doRequest } from '../../../helpers/requests' |
3 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
3 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' | 4 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' |
4 | 5 | ||
5 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | 6 | async 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 @@ | |||
1 | import { SCHEDULER_INTERVAL } from '../../initializers' | ||
2 | |||
3 | export 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 @@ | |||
1 | import { logger } from '../../helpers/logger' | ||
2 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | ||
3 | import { AbstractScheduler } from './abstract-scheduler' | ||
4 | |||
5 | export 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 | } | ||