+function processVideosEventsRetryWrapper (eventData, fromPod, finalCallback) {
+ const options = {
+ arguments: [ eventData, fromPod ],
+ errorMessage: 'Cannot process videos events with many retries.'
+ }
+
+ databaseUtils.retryTransactionWrapper(processVideosEvents, options, finalCallback)
+}
+
+function processVideosEvents (eventData, fromPod, finalCallback) {
+ waterfall([
+ databaseUtils.startSerializableTransaction,
+
+ function findVideo (t, callback) {
+ fetchOwnedVideo(eventData.remoteId, function (err, videoInstance) {
+ return callback(err, t, videoInstance)
+ })
+ },
+
+ function updateVideoIntoDB (t, videoInstance, callback) {
+ const options = { transaction: t }
+
+ let columnToUpdate
+
+ switch (eventData.eventType) {
+ case constants.REQUEST_VIDEO_EVENT_TYPES.VIEWS:
+ columnToUpdate = 'views'
+ break
+
+ case constants.REQUEST_VIDEO_EVENT_TYPES.LIKES:
+ columnToUpdate = 'likes'
+ break
+
+ case constants.REQUEST_VIDEO_EVENT_TYPES.DISLIKES:
+ columnToUpdate = 'dislikes'
+ break
+
+ default:
+ return callback(new Error('Unknown video event type.'))
+ }
+
+ const query = {}
+ query[columnToUpdate] = eventData.count
+
+ videoInstance.increment(query, options).asCallback(function (err) {
+ return callback(err, t)
+ })
+ },
+
+ databaseUtils.commitTransaction
+
+ ], function (err, t) {
+ if (err) {
+ console.log(err)
+ logger.debug('Cannot process a video event.', { error: err })
+ return databaseUtils.rollbackTransaction(err, t, finalCallback)
+ }
+
+ logger.info('Remote video event processed for video %s.', eventData.remoteId)
+ return finalCallback(null)
+ })
+}
+
+function quickAndDirtyUpdateVideoRetryWrapper (videoData, fromPod, finalCallback) {
+ const options = {
+ arguments: [ videoData, fromPod ],
+ errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
+ }
+
+ databaseUtils.retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback)
+}
+
+function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) {
+ waterfall([
+ databaseUtils.startSerializableTransaction,
+
+ function findVideo (t, callback) {
+ fetchRemoteVideo(fromPod.host, videoData.remoteId, function (err, videoInstance) {
+ return callback(err, t, videoInstance)
+ })
+ },
+
+ function updateVideoIntoDB (t, videoInstance, callback) {
+ const options = { transaction: t }
+
+ if (videoData.views) {
+ videoInstance.set('views', videoData.views)
+ }
+
+ if (videoData.likes) {
+ videoInstance.set('likes', videoData.likes)
+ }
+
+ if (videoData.dislikes) {
+ videoInstance.set('dislikes', videoData.dislikes)
+ }
+
+ videoInstance.save(options).asCallback(function (err) {
+ return callback(err, t)
+ })
+ },
+
+ databaseUtils.commitTransaction
+
+ ], function (err, t) {
+ if (err) {
+ logger.debug('Cannot quick and dirty update the remote video.', { error: err })
+ return databaseUtils.rollbackTransaction(err, t, finalCallback)
+ }
+
+ logger.info('Remote video %s quick and dirty updated', videoData.name)
+ return finalCallback(null)
+ })
+}
+