@@ -178,9 +178,13 @@ export async function CommitIngestOperation(
178
178
// Ensure any adlibbed parts are updated to follow the segmentId of the previous part
179
179
await updateSegmentIdsForAdlibbedPartInstances ( context , ingestModel , beforePartMap )
180
180
181
+ // TODO: This whole section can probably be removed later, it's really unneccessary in the grand scheme of
182
+ // things, it's here only to debug some problems
181
183
if ( data . renamedSegments && data . renamedSegments . size > 0 ) {
182
- logger . debug ( `Renamed segments: ${ JSON . stringify ( Array . from ( data . renamedSegments . entries ( ) ) ) } ` )
184
+ logger . verbose ( `Renamed segments: ${ JSON . stringify ( Array . from ( data . renamedSegments . entries ( ) ) ) } ` )
183
185
}
186
+ // End of temporary section
187
+
184
188
// ensure instances have matching segmentIds with the parts
185
189
await updatePartInstancesSegmentIds ( context , ingestModel , data . renamedSegments , beforePartMap )
186
190
@@ -220,6 +224,8 @@ export async function CommitIngestOperation(
220
224
const pSaveIngest = ingestModel . saveAllToDatabase ( )
221
225
pSaveIngest . catch ( ( ) => null ) // Ensure promise isn't reported as unhandled
222
226
227
+ ensureNextPartInstanceIsNotDeleted ( playoutModel )
228
+
223
229
await validateAdlibTestingSegment ( context , playoutModel )
224
230
225
231
try {
@@ -273,14 +279,18 @@ function canRemoveSegment(
273
279
logger . warn ( `Not allowing removal of previous playing segment "${ segmentId } ", making segment unsynced instead` )
274
280
return false
275
281
}
276
- if (
277
- currentPartInstance ?. segmentId === segmentId ||
278
- ( nextPartInstance ?. segmentId === segmentId && isTooCloseToAutonext ( currentPartInstance , false ) )
279
- ) {
282
+ if ( currentPartInstance ?. segmentId === segmentId ) {
280
283
// Don't allow removing an active rundown
281
284
logger . warn ( `Not allowing removal of current playing segment "${ segmentId } ", making segment unsynced instead` )
282
285
return false
283
286
}
287
+ if ( nextPartInstance ?. segmentId === segmentId && isTooCloseToAutonext ( currentPartInstance , false ) ) {
288
+ // Don't allow removing an active rundown
289
+ logger . warn (
290
+ `Not allowing removal of nexted segment "${ segmentId } ", because it's too close to an auto-next, making segment unsynced instead`
291
+ )
292
+ return false
293
+ }
284
294
285
295
if ( nextPartInstance ?. segmentId === segmentId && nextPartInstance . orphaned === 'adlib-part' ) {
286
296
// Don't allow removing an active rundown
@@ -365,6 +375,8 @@ async function updatePartInstancesSegmentIds(
365
375
366
376
const writeOps : AnyBulkWriteOperation < DBPartInstance > [ ] = [ ]
367
377
378
+ logger . debug ( `updatePartInstancesSegmentIds: renameRules: ${ JSON . stringify ( renameRules ) } ` )
379
+
368
380
for ( const [ newSegmentId , rule ] of rulesInOrder ) {
369
381
if ( rule . fromSegmentIds . length ) {
370
382
writeOps . push ( {
@@ -402,7 +414,12 @@ async function updatePartInstancesSegmentIds(
402
414
if ( writeOps . length ) await context . directCollections . PartInstances . bulkWrite ( writeOps )
403
415
404
416
// Double check that there are no parts using the old segment ids:
405
- const oldSegmentIds = Array . from ( renameRules . keys ( ) )
417
+ // TODO: This whole section can probably be removed later, it's really unneccessary in the grand scheme of
418
+ // things, it's here only to debug some problems
419
+ const oldSegmentIds : SegmentId [ ] = [ ]
420
+ for ( const renameRule of renameRules . values ( ) ) {
421
+ oldSegmentIds . push ( ...renameRule . fromSegmentIds )
422
+ }
406
423
const [ badPartInstances , badParts ] = await Promise . all ( [
407
424
await context . directCollections . PartInstances . findFetch ( {
408
425
rundownId : ingestModel . rundownId ,
@@ -428,6 +445,7 @@ async function updatePartInstancesSegmentIds(
428
445
) } ": ${ JSON . stringify ( badParts ) } , writeOps: ${ JSON . stringify ( writeOps ) } `
429
446
)
430
447
}
448
+ // End of the temporary section
431
449
}
432
450
}
433
451
@@ -662,10 +680,27 @@ async function getSelectedPartInstances(
662
680
} )
663
681
: [ ]
664
682
683
+ const currentPartInstance = instances . find ( ( inst ) => inst . _id === playlist . currentPartInfo ?. partInstanceId )
684
+ const nextPartInstance = instances . find ( ( inst ) => inst . _id === playlist . nextPartInfo ?. partInstanceId )
685
+ const previousPartInstance = instances . find ( ( inst ) => inst . _id === playlist . previousPartInfo ?. partInstanceId )
686
+
687
+ if ( playlist . currentPartInfo ?. partInstanceId && ! currentPartInstance )
688
+ logger . error (
689
+ `playlist.currentPartInfo is set, but PartInstance "${ playlist . currentPartInfo ?. partInstanceId } " was not found!`
690
+ )
691
+ if ( playlist . nextPartInfo ?. partInstanceId && ! nextPartInstance )
692
+ logger . error (
693
+ `playlist.nextPartInfo is set, but PartInstance "${ playlist . nextPartInfo ?. partInstanceId } " was not found!`
694
+ )
695
+ if ( playlist . previousPartInfo ?. partInstanceId && ! previousPartInstance )
696
+ logger . error (
697
+ `playlist.previousPartInfo is set, but PartInstance "${ playlist . previousPartInfo ?. partInstanceId } " was not found!`
698
+ )
699
+
665
700
return {
666
- currentPartInstance : instances . find ( ( inst ) => inst . _id === playlist . currentPartInfo ?. partInstanceId ) ,
667
- nextPartInstance : instances . find ( ( inst ) => inst . _id === playlist . nextPartInfo ?. partInstanceId ) ,
668
- previousPartInstance : instances . find ( ( inst ) => inst . _id === playlist . previousPartInfo ?. partInstanceId ) ,
701
+ currentPartInstance,
702
+ nextPartInstance,
703
+ previousPartInstance,
669
704
}
670
705
}
671
706
@@ -815,6 +850,16 @@ async function removeSegments(
815
850
} )
816
851
}
817
852
for ( const segmentId of purgeSegmentIds ) {
853
+ logger . debug (
854
+ `IngestModel: Removing segment "${ segmentId } " (` +
855
+ `previousPartInfo?.partInstanceId: ${ newPlaylist . previousPartInfo ?. partInstanceId } ,` +
856
+ `currentPartInfo?.partInstanceId: ${ newPlaylist . currentPartInfo ?. partInstanceId } ,` +
857
+ `nextPartInfo?.partInstanceId: ${ newPlaylist . nextPartInfo ?. partInstanceId } ,` +
858
+ `previousPartInstance.segmentId: ${ ! previousPartInstance ? 'N/A' : previousPartInstance . segmentId } ,` +
859
+ `currentPartInstance.segmentId: ${ ! currentPartInstance ? 'N/A' : currentPartInstance . segmentId } ,` +
860
+ `nextPartInstance.segmentId: ${ ! nextPartInstance ? 'N/A' : nextPartInstance . segmentId } ` +
861
+ `)`
862
+ )
818
863
ingestModel . removeSegment ( segmentId )
819
864
}
820
865
}
@@ -824,3 +869,12 @@ async function validateAdlibTestingSegment(_context: JobContext, playoutModel: P
824
869
rundown . updateAdlibTestingSegmentRank ( )
825
870
}
826
871
}
872
+ function ensureNextPartInstanceIsNotDeleted ( playoutModel : PlayoutModel ) {
873
+ if ( playoutModel . nextPartInstance ) {
874
+ // Check if the segment of the nextPartInstance exists
875
+ if ( ! playoutModel . findSegment ( playoutModel . nextPartInstance . partInstance . segmentId ) ) {
876
+ // The segment doesn't exist, set nextPartInstance to null, it'll be set by ensureNextPartIsValid() later.
877
+ playoutModel . setPartInstanceAsNext ( null , false , false )
878
+ }
879
+ }
880
+ }
0 commit comments