From 908e6ead78cc0a42545645be1610024d2ae65746 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 3 Jun 2021 16:56:42 +0200 Subject: [PATCH] Refactor AP video logger tags --- server/lib/activitypub/videos/refresh.ts | 10 +++++----- .../activitypub/videos/shared/abstract-builder.ts | 2 +- server/lib/activitypub/videos/shared/creator.ts | 10 ++++++---- .../videos/shared/video-sync-attributes.ts | 6 +++--- server/lib/activitypub/videos/updater.ts | 12 +++++++----- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/server/lib/activitypub/videos/refresh.ts b/server/lib/activitypub/videos/refresh.ts index da068cde9..71a4e75b0 100644 --- a/server/lib/activitypub/videos/refresh.ts +++ b/server/lib/activitypub/videos/refresh.ts @@ -8,8 +8,6 @@ import { HttpStatusCode } from '@shared/core-utils' import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' import { APVideoUpdater } from './updater' -const lTags = loggerTagsFactory('ap', 'video', 'refresh') - async function refreshVideoIfNeeded (options: { video: MVideoThumbnail fetchedType: VideoFetchByUrlType @@ -22,11 +20,13 @@ async function refreshVideoIfNeeded (options: { ? options.video as MVideoAccountLightBlacklistAllFiles : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) + const lTags = loggerTagsFactory('ap', 'video', 'refresh', video.uuid, video.url) + try { const { videoObject } = await fetchRemoteVideo(video.url) if (videoObject === undefined) { - logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags(video.uuid)) + logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags()) await video.setAsRefreshed() return video @@ -42,14 +42,14 @@ async function refreshVideoIfNeeded (options: { return video } catch (err) { if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { - logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags(video.uuid)) + logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags()) // Video does not exist anymore await video.destroy() return undefined } - logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags(video.uuid) }) + logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags() }) ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) diff --git a/server/lib/activitypub/videos/shared/abstract-builder.ts b/server/lib/activitypub/videos/shared/abstract-builder.ts index f8e4d6aa3..4bd38b40c 100644 --- a/server/lib/activitypub/videos/shared/abstract-builder.ts +++ b/server/lib/activitypub/videos/shared/abstract-builder.ts @@ -43,7 +43,7 @@ export abstract class APVideoAbstractBuilder { video, type: ThumbnailType.MINIATURE }).catch(err => { - logger.warn('Cannot generate thumbnail of %s.', this.videoObject.id, { err, ...this.lTags(video.uuid) }) + logger.warn('Cannot generate thumbnail of %s.', this.videoObject.id, { err, ...this.lTags() }) return undefined }) diff --git a/server/lib/activitypub/videos/shared/creator.ts b/server/lib/activitypub/videos/shared/creator.ts index ba3f6f77f..767bf0695 100644 --- a/server/lib/activitypub/videos/shared/creator.ts +++ b/server/lib/activitypub/videos/shared/creator.ts @@ -1,5 +1,5 @@ -import { logger, loggerTagsFactory } from '@server/helpers/logger' +import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger' import { sequelizeTypescript } from '@server/initializers/database' import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist' import { VideoModel } from '@server/models/video/video' @@ -9,14 +9,16 @@ import { APVideoAbstractBuilder } from './abstract-builder' import { getVideoAttributesFromObject } from './object-to-model-attributes' export class APVideoCreator extends APVideoAbstractBuilder { - protected lTags = loggerTagsFactory('ap', 'video', 'create') + protected lTags: LoggerTagsFn constructor (protected readonly videoObject: VideoObject) { super() + + this.lTags = loggerTagsFactory('ap', 'video', 'create', this.videoObject.uuid, this.videoObject.id) } async create (waitThumbnail = false) { - logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags(this.videoObject.uuid)) + logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags()) const channelActor = await this.getOrCreateVideoChannelFromVideoObject() const channel = channelActor.VideoChannel @@ -57,7 +59,7 @@ export class APVideoCreator extends APVideoAbstractBuilder { transaction: t }) - logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags(videoCreated.uuid)) + logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags()) return { autoBlacklisted, videoCreated } } catch (err) { diff --git a/server/lib/activitypub/videos/shared/video-sync-attributes.ts b/server/lib/activitypub/videos/shared/video-sync-attributes.ts index 102d40621..c4e101005 100644 --- a/server/lib/activitypub/videos/shared/video-sync-attributes.ts +++ b/server/lib/activitypub/videos/shared/video-sync-attributes.ts @@ -62,7 +62,7 @@ function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: Video const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate) return crawlCollectionPage(uri, handler, cleaner) - .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid) })) + .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) } function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { @@ -76,7 +76,7 @@ function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) return crawlCollectionPage(uri, handler, cleaner) - .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid) })) + .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) } function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { @@ -90,5 +90,5 @@ function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) return crawlCollectionPage(uri, handler, cleaner) - .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid) })) + .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) } diff --git a/server/lib/activitypub/videos/updater.ts b/server/lib/activitypub/videos/updater.ts index 6745e2efd..3339611fc 100644 --- a/server/lib/activitypub/videos/updater.ts +++ b/server/lib/activitypub/videos/updater.ts @@ -1,6 +1,6 @@ import { Transaction } from 'sequelize/types' import { resetSequelizeInstance } from '@server/helpers/database-utils' -import { logger, loggerTagsFactory } from '@server/helpers/logger' +import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger' import { sequelizeTypescript } from '@server/initializers/database' import { Notifier } from '@server/lib/notifier' import { PeerTubeSocket } from '@server/lib/peertube-socket' @@ -19,7 +19,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder { private readonly oldVideoChannel: MChannelAccountLight - protected lTags = loggerTagsFactory('ap', 'video', 'update') + protected lTags: LoggerTagsFn constructor ( protected readonly videoObject: VideoObject, @@ -33,12 +33,14 @@ export class APVideoUpdater extends APVideoAbstractBuilder { this.oldVideoChannel = this.video.VideoChannel this.videoFieldsSave = this.video.toJSON() + + this.lTags = loggerTagsFactory('ap', 'video', 'update', video.uuid, video.url) } async update (overrideTo?: string[]) { logger.debug( 'Updating remote video "%s".', this.videoObject.uuid, - { videoObject: this.videoObject, ...this.lTags(this.videoObject.uuid) } + { videoObject: this.videoObject, ...this.lTags() } ) try { @@ -82,7 +84,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder { PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated) } - logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags(this.videoObject.uuid)) + logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags()) return videoUpdated } catch (err) { @@ -158,7 +160,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder { } // This is just a debug because we will retry the insert - logger.debug('Cannot update the remote video.', { err, ...this.lTags(this.videoObject.uuid) }) + logger.debug('Cannot update the remote video.', { err, ...this.lTags() }) throw err } } -- 2.41.0