X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Flive%2Flive-segment-sha-store.ts;h=1a0a93985598721cb4fd5ad17bfc8ed7a1a21c13;hb=ef2e6aabf755feeec96011e70ff2522a491c5cb3;hp=faf03dccfebcd23f4c17b90274040198854c7495;hpb=cea2fd90ddb3bf57c2fed77128938d12d4c2be6b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/live/live-segment-sha-store.ts b/server/lib/live/live-segment-sha-store.ts index faf03dccf..1a0a93985 100644 --- a/server/lib/live/live-segment-sha-store.ts +++ b/server/lib/live/live-segment-sha-store.ts @@ -1,4 +1,5 @@ -import { writeJson } from 'fs-extra' +import { rename, writeJson } from 'fs-extra' +import PQueue from 'p-queue' import { basename } from 'path' import { mapToJSON } from '@server/helpers/core-utils' import { logger, loggerTagsFactory } from '@server/helpers/logger' @@ -13,9 +14,13 @@ class LiveSegmentShaStore { private readonly segmentsSha256 = new Map() private readonly videoUUID: string + private readonly sha256Path: string + private readonly sha256PathTMP: string + private readonly streamingPlaylist: MStreamingPlaylistVideo private readonly sendToObjectStorage: boolean + private readonly writeQueue = new PQueue({ concurrency: 1 }) constructor (options: { videoUUID: string @@ -24,7 +29,10 @@ class LiveSegmentShaStore { sendToObjectStorage: boolean }) { this.videoUUID = options.videoUUID + this.sha256Path = options.sha256Path + this.sha256PathTMP = options.sha256Path + '.tmp' + this.streamingPlaylist = options.streamingPlaylist this.sendToObjectStorage = options.sendToObjectStorage } @@ -37,7 +45,11 @@ class LiveSegmentShaStore { const segmentName = basename(segmentPath) this.segmentsSha256.set(segmentName, shaResult) - await this.writeToDisk() + try { + await this.writeToDisk() + } catch (err) { + logger.error('Cannot write sha segments to disk.', { err }) + } } async removeSegmentSha (segmentPath: string) { @@ -46,7 +58,10 @@ class LiveSegmentShaStore { logger.debug('Removing live sha segment %s.', segmentPath, lTags(this.videoUUID)) if (!this.segmentsSha256.has(segmentName)) { - logger.warn('Unknown segment in files map for video %s and segment %s.', this.videoUUID, segmentPath, lTags(this.videoUUID)) + logger.warn( + 'Unknown segment in live segment hash store for video %s and segment %s.', + this.videoUUID, segmentPath, lTags(this.videoUUID) + ) return } @@ -55,19 +70,22 @@ class LiveSegmentShaStore { await this.writeToDisk() } - private async writeToDisk () { - await writeJson(this.sha256Path, mapToJSON(this.segmentsSha256)) + private writeToDisk () { + return this.writeQueue.add(async () => { + // Atomic write: use rename instead of move that is not atomic + await writeJson(this.sha256PathTMP, mapToJSON(this.segmentsSha256)) + await rename(this.sha256PathTMP, this.sha256Path) - if (this.sendToObjectStorage) { - const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path) + if (this.sendToObjectStorage) { + const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path) - if (this.streamingPlaylist.segmentsSha256Url !== url) { - this.streamingPlaylist.segmentsSha256Url = url - await this.streamingPlaylist.save() + if (this.streamingPlaylist.segmentsSha256Url !== url) { + this.streamingPlaylist.segmentsSha256Url = url + await this.streamingPlaylist.save() + } } - } + }) } - } export {