|
1 | 1 | import type { CleanupOrphanedExpectedPackageReferencesProps } from '@sofie-automation/corelib/dist/worker/studio' |
2 | 2 | import type { JobContext } from '../jobs' |
| 3 | +import { runWithPlaylistLock } from './lock' |
| 4 | +import { |
| 5 | + ExpectedPackageDB, |
| 6 | + isPackageReferencedByPlayout, |
| 7 | +} from '@sofie-automation/corelib/dist/dataModel/ExpectedPackages' |
| 8 | +import { PieceInstance } from '@sofie-automation/corelib/dist/dataModel/PieceInstance' |
| 9 | +import { AnyBulkWriteOperation } from 'mongodb' |
| 10 | +import { ExpectedPackageId, PieceInstanceId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
3 | 11 |
|
4 | 12 | export async function handleCleanupOrphanedExpectedPackageReferences( |
5 | 13 | context: JobContext, |
6 | 14 | data: CleanupOrphanedExpectedPackageReferencesProps |
7 | 15 | ): Promise<void> { |
8 | | - // TODO |
| 16 | + // Something has changed in the PieceInstances, we need to check that the ExpectedPackages have only valid PieceInstances as owners, and remove any which no longer have owners |
| 17 | + |
| 18 | + await runWithPlaylistLock(context, data.playlistId, async () => { |
| 19 | + const [existingPackages, validPieceInstances] = await Promise.all([ |
| 20 | + context.directCollections.ExpectedPackages.findFetch( |
| 21 | + { |
| 22 | + studioId: context.studioId, |
| 23 | + rundownId: data.rundownId, |
| 24 | + bucketId: null, |
| 25 | + }, |
| 26 | + { |
| 27 | + projection: { |
| 28 | + _id: 1, |
| 29 | + playoutSources: 1, |
| 30 | + // We only need to know if there are any entries, so project them to be as minimal as possible |
| 31 | + 'ingestSources.fromPieceType': 1, |
| 32 | + }, |
| 33 | + } |
| 34 | + ) as Promise< |
| 35 | + Array< |
| 36 | + Pick<ExpectedPackageDB, '_id' | 'playoutSources' | 'ingestSources'> & { |
| 37 | + ingestSources: unknown[] |
| 38 | + } |
| 39 | + > |
| 40 | + >, |
| 41 | + context.directCollections.PieceInstances.findFetch( |
| 42 | + { |
| 43 | + rundownId: data.rundownId, |
| 44 | + reset: { $ne: true }, |
| 45 | + }, |
| 46 | + { |
| 47 | + projection: { |
| 48 | + _id: 1, |
| 49 | + neededExpectedPackageIds: 1, |
| 50 | + }, |
| 51 | + } |
| 52 | + ) as Promise<Array<Pick<PieceInstance, '_id' | 'neededExpectedPackageIds'>>>, |
| 53 | + ]) |
| 54 | + |
| 55 | + const pieceInstancePackageMap = new Map<PieceInstanceId, Set<ExpectedPackageId>>() |
| 56 | + for (const pieceInstance of validPieceInstances) { |
| 57 | + if (pieceInstance.neededExpectedPackageIds && pieceInstance.neededExpectedPackageIds.length > 0) |
| 58 | + pieceInstancePackageMap.set(pieceInstance._id, new Set(pieceInstance.neededExpectedPackageIds)) |
| 59 | + } |
| 60 | + |
| 61 | + const writeOps: AnyBulkWriteOperation<ExpectedPackageDB>[] = [] |
| 62 | + |
| 63 | + for (const expectedPackage of existingPackages) { |
| 64 | + // Find the pieceInstanceIds that are stale |
| 65 | + const pieceInstanceIdsToRemove: PieceInstanceId[] = [] |
| 66 | + for (const pieceInstanceId of expectedPackage.playoutSources.pieceInstanceIds) { |
| 67 | + const pieceInstancePackageIds = pieceInstancePackageMap.get(pieceInstanceId) |
| 68 | + if (!pieceInstancePackageIds || !pieceInstancePackageIds.has(expectedPackage._id)) { |
| 69 | + // This pieceInstanceId is no longer valid, queue it to be removed |
| 70 | + pieceInstanceIdsToRemove.push(pieceInstanceId) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Queue the write |
| 75 | + if (pieceInstanceIdsToRemove.length === expectedPackage.playoutSources.pieceInstanceIds.length) { |
| 76 | + // It looks like all the pieceInstanceIds are being removed |
| 77 | + |
| 78 | + if ( |
| 79 | + expectedPackage.ingestSources.length === 0 && |
| 80 | + !isPackageReferencedByPlayout({ |
| 81 | + // Test with a fake package |
| 82 | + ...expectedPackage, |
| 83 | + playoutSources: { |
| 84 | + ...expectedPackage.playoutSources, |
| 85 | + pieceInstanceIds: [], |
| 86 | + }, |
| 87 | + }) |
| 88 | + ) { |
| 89 | + // This package is not referenced by anything, so we can delete it |
| 90 | + writeOps.push({ |
| 91 | + deleteOne: { |
| 92 | + filter: { |
| 93 | + _id: expectedPackage._id, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }) |
| 97 | + } else { |
| 98 | + // This package is still referenced by something, so we need to keep it |
| 99 | + writeOps.push({ |
| 100 | + updateOne: { |
| 101 | + filter: { |
| 102 | + _id: expectedPackage._id, |
| 103 | + }, |
| 104 | + update: { |
| 105 | + $set: { |
| 106 | + 'playoutSources.pieceInstanceIds': [], |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }) |
| 111 | + } |
| 112 | + } else if (pieceInstanceIdsToRemove.length > 0) { |
| 113 | + // Some of the pieceInstanceIds are being removed |
| 114 | + writeOps.push({ |
| 115 | + updateOne: { |
| 116 | + filter: { |
| 117 | + _id: expectedPackage._id, |
| 118 | + }, |
| 119 | + update: { |
| 120 | + $pull: { |
| 121 | + 'playoutSources.pieceInstanceIds': { $in: pieceInstanceIdsToRemove }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + await context.directCollections.ExpectedPackages.bulkWrite(writeOps) |
| 130 | + }) |
9 | 131 | } |
0 commit comments