From 90d4bb8125e80c8060416d4d135ddeaf0a622ede Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Jun 2018 14:27:40 +0200 Subject: Refractor retry transaction function --- server/lib/activitypub/actor.ts | 12 +----- server/lib/activitypub/process/process-announce.ts | 13 +----- server/lib/activitypub/process/process-create.ts | 37 +++-------------- server/lib/activitypub/process/process-delete.ts | 46 +++------------------ server/lib/activitypub/process/process-follow.ts | 13 +----- server/lib/activitypub/process/process-like.ts | 16 ++------ server/lib/activitypub/process/process-undo.ts | 48 +++------------------- server/lib/activitypub/process/process-update.ts | 26 ++---------- server/lib/activitypub/videos.ts | 7 +--- 9 files changed, 30 insertions(+), 188 deletions(-) (limited to 'server/lib/activitypub') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index f27733418..9257d7d20 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -62,18 +62,10 @@ async function getOrCreateActorAndServerAndModel (activityActor: string | Activi } } - const options = { - arguments: [ result, ownerActor ], - errorMessage: 'Cannot save actor and server with many retries.' - } - actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, options) + actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, result, ownerActor) } - const options = { - arguments: [ actor ], - errorMessage: 'Cannot refresh actor if needed with many retries.' - } - return retryTransactionWrapper(refreshActorIfNeeded, options) + return retryTransactionWrapper(refreshActorIfNeeded, actor) } function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) { diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 4e50da8d2..d8ca59425 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -11,7 +11,7 @@ import { getOrCreateAccountAndVideoAndChannel } from '../videos' async function processAnnounceActivity (activity: ActivityAnnounce) { const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) - return processVideoShare(actorAnnouncer, activity) + return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity) } // --------------------------------------------------------------------------- @@ -22,16 +22,7 @@ export { // --------------------------------------------------------------------------- -function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { - const options = { - arguments: [ actorAnnouncer, activity ], - errorMessage: 'Cannot share the video activity with many retries.' - } - - return retryTransactionWrapper(shareVideo, options) -} - -async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { +async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id let video: VideoModel diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 38dacf772..6364bf135 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -21,13 +21,13 @@ async function processCreateActivity (activity: ActivityCreate) { if (activityType === 'View') { return processCreateView(actor, activity) } else if (activityType === 'Dislike') { - return processCreateDislike(actor, activity) + return retryTransactionWrapper(processCreateDislike, actor, activity) } else if (activityType === 'Video') { return processCreateVideo(actor, activity) } else if (activityType === 'Flag') { - return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) + return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject) } else if (activityType === 'Note') { - return processCreateVideoComment(actor, activity) + return retryTransactionWrapper(processCreateVideoComment, actor, activity) } logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) @@ -54,15 +54,6 @@ async function processCreateVideo ( } async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { - const options = { - arguments: [ byActor, activity ], - errorMessage: 'Cannot dislike the video with many retries.' - } - - return retryTransactionWrapper(createVideoDislike, options) -} - -async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { const dislike = activity.object as DislikeObject const byAccount = byActor.Account @@ -109,16 +100,7 @@ async function processCreateView (byActor: ActorModel, activity: ActivityCreate) } } -function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { - const options = { - arguments: [ actor, videoAbuseToCreateData ], - errorMessage: 'Cannot insert the remote video abuse with many retries.' - } - - return retryTransactionWrapper(addRemoteVideoAbuse, options) -} - -async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { +async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) const account = actor.Account @@ -139,16 +121,7 @@ async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: V }) } -function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { - const options = { - arguments: [ byActor, activity ], - errorMessage: 'Cannot create video comment with many retries.' - } - - return retryTransactionWrapper(createVideoComment, options) -} - -async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { +async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { const comment = activity.object as VideoCommentObject const byAccount = byActor.Account diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 8310b70f0..ff0caa343 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts @@ -21,12 +21,12 @@ async function processDeleteActivity (activity: ActivityDelete) { if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.') actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel - return processDeleteAccount(actor.Account) + return retryTransactionWrapper(processDeleteAccount, actor.Account) } else if (actor.type === 'Group') { if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.') actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel - return processDeleteVideoChannel(actor.VideoChannel) + return retryTransactionWrapper(processDeleteVideoChannel, actor.VideoChannel) } } @@ -34,14 +34,14 @@ async function processDeleteActivity (activity: ActivityDelete) { { const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl) if (videoCommentInstance) { - return processDeleteVideoComment(actor, videoCommentInstance, activity) + return retryTransactionWrapper(processDeleteVideoComment, actor, videoCommentInstance, activity) } } { const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) if (videoInstance) { - return processDeleteVideo(actor, videoInstance) + return retryTransactionWrapper(processDeleteVideo, actor, videoInstance) } } @@ -57,15 +57,6 @@ export { // --------------------------------------------------------------------------- async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) { - const options = { - arguments: [ actor, videoToDelete ], - errorMessage: 'Cannot remove the remote video with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideo, options) -} - -async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) { logger.debug('Removing remote video "%s".', videoToDelete.uuid) await sequelizeTypescript.transaction(async t => { @@ -80,15 +71,6 @@ async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) } async function processDeleteAccount (accountToRemove: AccountModel) { - const options = { - arguments: [ accountToRemove ], - errorMessage: 'Cannot remove the remote account with many retries.' - } - - await retryTransactionWrapper(deleteRemoteAccount, options) -} - -async function deleteRemoteAccount (accountToRemove: AccountModel) { logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid) await sequelizeTypescript.transaction(async t => { @@ -99,15 +81,6 @@ async function deleteRemoteAccount (accountToRemove: AccountModel) { } async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) { - const options = { - arguments: [ videoChannelToRemove ], - errorMessage: 'Cannot remove the remote video channel with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideoChannel, options) -} - -async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) { logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid) await sequelizeTypescript.transaction(async t => { @@ -117,16 +90,7 @@ async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid) } -async function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { - const options = { - arguments: [ byActor, videoComment, activity ], - errorMessage: 'Cannot remove the remote video comment with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideoComment, options) -} - -function deleteRemoteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { +function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { logger.debug('Removing remote video comment "%s".', videoComment.url) return sequelizeTypescript.transaction(async t => { diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index dc1d542b5..f34fd66cc 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -11,7 +11,7 @@ async function processFollowActivity (activity: ActivityFollow) { const activityObject = activity.object const actor = await getOrCreateActorAndServerAndModel(activity.actor) - return processFollow(actor, activityObject) + return retryTransactionWrapper(processFollow, actor, activityObject) } // --------------------------------------------------------------------------- @@ -22,16 +22,7 @@ export { // --------------------------------------------------------------------------- -function processFollow (actor: ActorModel, targetActorURL: string) { - const options = { - arguments: [ actor, targetActorURL ], - errorMessage: 'Cannot follow with many retries.' - } - - return retryTransactionWrapper(follow, options) -} - -async function follow (actor: ActorModel, targetActorURL: string) { +async function processFollow (actor: ActorModel, targetActorURL: string) { await sequelizeTypescript.transaction(async t => { const targetActor = await ActorModel.loadByUrl(targetActorURL, t) diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index f1642f038..d0865b78c 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts @@ -4,14 +4,13 @@ import { sequelizeTypescript } from '../../../initializers' import { AccountVideoRateModel } from '../../../models/account/account-video-rate' import { ActorModel } from '../../../models/activitypub/actor' import { getOrCreateActorAndServerAndModel } from '../actor' -import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' +import { forwardVideoRelatedActivity } from '../send/utils' import { getOrCreateAccountAndVideoAndChannel } from '../videos' -import { getActorsInvolvedInVideo } from '../audience' async function processLikeActivity (activity: ActivityLike) { const actor = await getOrCreateActorAndServerAndModel(activity.actor) - return processLikeVideo(actor, activity) + return retryTransactionWrapper(processLikeVideo, actor, activity) } // --------------------------------------------------------------------------- @@ -22,16 +21,7 @@ export { // --------------------------------------------------------------------------- -async function processLikeVideo (actor: ActorModel, activity: ActivityLike) { - const options = { - arguments: [ actor, activity ], - errorMessage: 'Cannot like the video with many retries.' - } - - return retryTransactionWrapper(createVideoLike, options) -} - -async function createVideoLike (byActor: ActorModel, activity: ActivityLike) { +async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) { const videoUrl = activity.object const byAccount = byActor.Account diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 37db58e1a..b6de107ad 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -18,13 +18,13 @@ async function processUndoActivity (activity: ActivityUndo) { const actorUrl = getActorUrl(activity.actor) if (activityToUndo.type === 'Like') { - return processUndoLike(actorUrl, activity) + return retryTransactionWrapper(processUndoLike, actorUrl, activity) } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { - return processUndoDislike(actorUrl, activity) + return retryTransactionWrapper(processUndoDislike, actorUrl, activity) } else if (activityToUndo.type === 'Follow') { - return processUndoFollow(actorUrl, activityToUndo) + return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo) } else if (activityToUndo.type === 'Announce') { - return processUndoAnnounce(actorUrl, activityToUndo) + return retryTransactionWrapper(processUndoAnnounce, actorUrl, activityToUndo) } logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) @@ -40,16 +40,7 @@ export { // --------------------------------------------------------------------------- -function processUndoLike (actorUrl: string, activity: ActivityUndo) { - const options = { - arguments: [ actorUrl, activity ], - errorMessage: 'Cannot undo like with many retries.' - } - - return retryTransactionWrapper(undoLike, options) -} - -async function undoLike (actorUrl: string, activity: ActivityUndo) { +async function processUndoLike (actorUrl: string, activity: ActivityUndo) { const likeActivity = activity.object as ActivityLike const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object) @@ -73,16 +64,7 @@ async function undoLike (actorUrl: string, activity: ActivityUndo) { }) } -function processUndoDislike (actorUrl: string, activity: ActivityUndo) { - const options = { - arguments: [ actorUrl, activity ], - errorMessage: 'Cannot undo dislike with many retries.' - } - - return retryTransactionWrapper(undoDislike, options) -} - -async function undoDislike (actorUrl: string, activity: ActivityUndo) { +async function processUndoDislike (actorUrl: string, activity: ActivityUndo) { const dislike = activity.object.object as DislikeObject const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) @@ -107,15 +89,6 @@ async function undoDislike (actorUrl: string, activity: ActivityUndo) { } function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) { - const options = { - arguments: [ actorUrl, followActivity ], - errorMessage: 'Cannot undo follow with many retries.' - } - - return retryTransactionWrapper(undoFollow, options) -} - -function undoFollow (actorUrl: string, followActivity: ActivityFollow) { return sequelizeTypescript.transaction(async t => { const follower = await ActorModel.loadByUrl(actorUrl, t) const following = await ActorModel.loadByUrl(followActivity.object, t) @@ -130,15 +103,6 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) { } function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { - const options = { - arguments: [ actorUrl, announceActivity ], - errorMessage: 'Cannot undo announce with many retries.' - } - - return retryTransactionWrapper(undoAnnounce, options) -} - -function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { return sequelizeTypescript.transaction(async t => { const byAccount = await AccountModel.loadByUrl(actorUrl, t) if (!byAccount) throw new Error('Unknown account ' + actorUrl) diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 1ebda46d3..73db461c3 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts @@ -25,9 +25,9 @@ async function processUpdateActivity (activity: ActivityUpdate) { const objectType = activity.object.type if (objectType === 'Video') { - return processUpdateVideo(actor, activity) + return retryTransactionWrapper(processUpdateVideo, actor, activity) } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') { - return processUpdateActor(actor, activity) + return retryTransactionWrapper(processUpdateActor, actor, activity) } return undefined @@ -41,16 +41,7 @@ export { // --------------------------------------------------------------------------- -function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { - const options = { - arguments: [ actor, activity ], - errorMessage: 'Cannot update the remote video with many retries' - } - - return retryTransactionWrapper(updateRemoteVideo, options) -} - -async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { +async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { const videoObject = activity.object as VideoTorrentObject if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) { @@ -136,16 +127,7 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { } } -function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { - const options = { - arguments: [ actor, activity ], - errorMessage: 'Cannot update the remote actor with many retries' - } - - return retryTransactionWrapper(updateRemoteActor, options) -} - -async function updateRemoteActor (actor: ActorModel, activity: ActivityUpdate) { +async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) { const actorAttributesToUpdate = activity.object as ActivityPubActor logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid) diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 7ec8ca193..a16828fda 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -228,12 +228,7 @@ async function getOrCreateAccountAndVideoAndChannel (videoObject: VideoTorrentOb const channelActor = await getOrCreateVideoChannel(videoObject) - const options = { - arguments: [ videoObject, channelActor ], - errorMessage: 'Cannot insert the remote video with many retries.' - } - - const video = await retryTransactionWrapper(getOrCreateVideo, options) + const video = await retryTransactionWrapper(getOrCreateVideo, videoObject, channelActor) // Process outside the transaction because we could fetch remote data logger.info('Adding likes of video %s.', video.uuid) -- cgit v1.2.3