PeerTube/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-schedu...

48 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-11-17 04:35:10 -06:00
import { JobScheduler, JobHandler } from '../job-scheduler'
import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler'
2017-11-22 03:29:55 -06:00
import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler'
2017-11-17 04:35:10 -06:00
import { JobCategory } from '../../../../shared'
2017-11-23 07:19:55 -06:00
import { ACTIVITY_PUB } from '../../../initializers/constants'
import { logger } from '../../../helpers/logger'
2017-11-17 04:35:10 -06:00
type ActivityPubHttpPayload = {
uris: string[]
2017-11-22 03:29:55 -06:00
signatureAccountId?: number
body?: any
2017-11-23 07:19:55 -06:00
attemptNumber?: number
2017-11-17 04:35:10 -06:00
}
2017-11-23 07:19:55 -06:00
2017-11-17 04:35:10 -06:00
const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = {
activitypubHttpBroadcastHandler,
2017-11-22 03:29:55 -06:00
activitypubHttpUnicastHandler,
activitypubHttpFetcherHandler
2017-11-17 04:35:10 -06:00
}
const jobCategory: JobCategory = 'activitypub-http'
const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers)
2017-11-23 07:19:55 -06:00
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)
}
}
2017-11-17 04:35:10 -06:00
export {
ActivityPubHttpPayload,
2017-11-23 07:19:55 -06:00
activitypubHttpJobScheduler,
maybeRetryRequestLater
2017-11-17 04:35:10 -06:00
}