From 0032ebe94aa83fab761c7de3ceb6210ac4532824 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 23 Nov 2017 14:19:55 +0100 Subject: Federate likes/dislikes --- .../activitypub-http-broadcast-handler.ts | 9 ++++++-- .../activitypub-http-job-scheduler.ts | 24 +++++++++++++++++++++- .../activitypub-http-unicast-handler.ts | 9 ++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) (limited to 'server/lib/jobs/activitypub-http-job-scheduler') 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 111fc88a4..5b4c65b81 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 @@ -2,7 +2,7 @@ import { logger } from '../../../helpers' import { buildSignedActivity } from '../../../helpers/activitypub' import { doRequest } from '../../../helpers/requests' import { database as db } from '../../../initializers' -import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' +import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { logger.info('Processing ActivityPub broadcast in job %d.', jobId) @@ -20,7 +20,12 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) { for (const uri of payload.uris) { options.uri = uri - await doRequest(options) + + try { + await doRequest(options) + } catch (err) { + await maybeRetryRequestLater(err, payload, uri) + } } } 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 aef217ce7..ccf109935 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,12 +4,16 @@ import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-h import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler' import { JobCategory } from '../../../../shared' +import { ACTIVITY_PUB } from '../../../initializers/constants' +import { logger } from '../../../helpers/logger' type ActivityPubHttpPayload = { uris: string[] signatureAccountId?: number body?: any + attemptNumber?: number } + const jobHandlers: { [ handlerName: string ]: JobHandler } = { activitypubHttpBroadcastHandler, activitypubHttpUnicastHandler, @@ -19,7 +23,25 @@ const jobCategory: JobCategory = 'activitypub-http' const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers) +function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) { + logger.warn('Cannot make request to %s.', uri, err) + + let attemptNumber = payload.attemptNumber || 1 + attemptNumber += 1 + + if (attemptNumber < ACTIVITY_PUB.MAX_HTTP_ATTEMPT) { + logger.debug('Retrying request to %s (attempt %d/%d).', uri, attemptNumber, ACTIVITY_PUB.MAX_HTTP_ATTEMPT, err) + + const newPayload = Object.assign(payload, { + uris: [ uri ], + attemptNumber + }) + return activitypubHttpJobScheduler.createJob(undefined, 'activitypubHttpUnicastHandler', newPayload) + } +} + export { ActivityPubHttpPayload, - activitypubHttpJobScheduler + activitypubHttpJobScheduler, + maybeRetryRequestLater } 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 8d3b755ad..f7f3dabbd 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,6 +1,6 @@ import { logger } from '../../../helpers' import { doRequest } from '../../../helpers/requests' -import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' +import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler' import { database as db } from '../../../initializers/database' import { buildSignedActivity } from '../../../helpers/activitypub' @@ -18,7 +18,12 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) { json: signedBody } - await doRequest(options) + try { + await doRequest(options) + } catch (err) { + await maybeRetryRequestLater(err, payload, uri) + throw err + } } function onError (err: Error, jobId: number) { -- cgit v1.2.3