@@ -29,7 +29,7 @@ public class TranscriptionManager : ServiceManager<TranscriptionOptions>
2929 private CancellationTokenSource ? _cancelToken ;
3030 private Task ? _consumer ;
3131 private readonly TaskStatus [ ] _notRunning = new TaskStatus [ ] { TaskStatus . Canceled , TaskStatus . Faulted , TaskStatus . RanToCompletion } ;
32- private readonly WorkOrderStatus [ ] _ignoreWorkOrders = new WorkOrderStatus [ ] { WorkOrderStatus . Completed } ;
32+ private readonly WorkOrderStatus [ ] _ignoreWorkOrders = new WorkOrderStatus [ ] { WorkOrderStatus . Completed , WorkOrderStatus . Cancelled } ;
3333 private int _retries = 0 ;
3434
3535 #endregion
@@ -369,43 +369,60 @@ private async Task UpdateTranscriptionAsync(TranscriptRequestModel request, Cont
369369 if ( ! String . IsNullOrEmpty ( safePath ) )
370370 {
371371 this . Logger . LogInformation ( "Validating work order. Content ID: {Id}, Path: {file}" , requestContentId , safePath ) ;
372- var hasWorkOrder = await UpdateWorkOrderAsync ( request , WorkOrderStatus . InProgress ) ;
373-
374- if ( hasWorkOrder )
372+ var workOrder = await UpdateWorkOrderAsync ( request , content . IsApproved ? WorkOrderStatus . Cancelled : WorkOrderStatus . InProgress ) ;
373+ if ( workOrder ? . Status == WorkOrderStatus . InProgress )
375374 {
376375 var original = content . Body ;
377376 var fileBytes = File . ReadAllBytes ( safePath ) ;
378377 var transcript = await RequestTranscriptionAsync ( requestContentId , fileBytes ) ; // TODO: Extract language from data source.
379378
380- // Fetch content again because it may have been updated by an external source.
381- // This can introduce issues if the transcript has been edited as now it will overwrite what was changed.
382- content = ( await this . Api . FindContentByIdAsync ( requestContentId ) ) ! ;
383- if ( content != null && ! String . IsNullOrWhiteSpace ( transcript ) )
384- {
385- // The transcription may have been edited during this process and now those changes will be lost.
386- if ( String . CompareOrdinal ( original , content . Body ) != 0 ) this . Logger . LogWarning ( "Transcription will be overwritten. Content ID: {Id}" , requestContentId ) ;
387-
388- content . Body = GetFormattedTranscript ( transcript ) ;
389- await this . Api . UpdateContentAsync ( content ) ; // TODO: This can result in an editor getting a optimistic concurrency error.
390- this . Logger . LogInformation ( "Transcription updated. Content ID: {Id}" , requestContentId ) ;
391-
392- await UpdateWorkOrderAsync ( request , WorkOrderStatus . Completed ) ;
393- }
394- else if ( String . IsNullOrWhiteSpace ( transcript ) )
379+ // Fetch latest work order to ensure they haven't changed during transcription process.
380+ workOrder = await this . Api . FindWorkOrderAsync ( workOrder . Id ) ;
381+ if ( workOrder ? . Status == WorkOrderStatus . Cancelled )
395382 {
396- this . Logger . LogWarning ( "Content did not generate a transcript. Content ID: {Id}" , requestContentId ) ;
397- var emptyTranscriptException = new EmptyTranscriptException ( requestContentId ) ;
398- await UpdateWorkOrderAsync ( request , WorkOrderStatus . Failed , emptyTranscriptException ) ;
383+ this . Logger . LogWarning ( "Work order has been cancelled during transcription process. Content ID: {id}, File: {path}" , requestContentId , safePath ) ;
399384 }
400385 else
401386 {
402- // The content is no longer available for some reason.
403- this . Logger . LogError ( "Content no longer exists. Content ID: {Id}" , requestContentId ) ;
404- var contentNotFoundException = new ContentNotFoundException ( requestContentId ) ;
405- await UpdateWorkOrderAsync ( request , WorkOrderStatus . Failed , contentNotFoundException ) ;
387+ // Fetch content again because it may have been updated by an external source.
388+ // This can introduce issues if the transcript has been edited as now it will overwrite what was changed.
389+ content = ( await this . Api . FindContentByIdAsync ( requestContentId ) ) ! ;
390+ if ( content != null && ! String . IsNullOrWhiteSpace ( transcript ) && ! content . IsApproved )
391+ {
392+ // The transcription may have been edited during this process and now those changes will be lost.
393+ if ( String . CompareOrdinal ( original , content . Body ) != 0 ) this . Logger . LogWarning ( "Transcription will be overwritten. Content ID: {Id}" , requestContentId ) ;
394+
395+ content . Body = GetFormattedTranscript ( transcript ) ;
396+ await this . Api . UpdateContentAsync ( content ) ; // TODO: This can result in an editor getting a optimistic concurrency error.
397+ this . Logger . LogInformation ( "Transcription updated. Content ID: {Id}" , requestContentId ) ;
398+
399+ await UpdateWorkOrderAsync ( request , WorkOrderStatus . Completed ) ;
400+ }
401+ else if ( content != null && content . IsApproved )
402+ {
403+ this . Logger . LogWarning ( "Content is approved, transcription will not be updated. Content ID: {Id}" , requestContentId ) ;
404+ await UpdateWorkOrderAsync ( request , WorkOrderStatus . Cancelled ) ;
405+ }
406+ else if ( String . IsNullOrWhiteSpace ( transcript ) )
407+ {
408+ this . Logger . LogWarning ( "Content did not generate a transcript. Content ID: {Id}" , requestContentId ) ;
409+ var emptyTranscriptException = new EmptyTranscriptException ( requestContentId ) ;
410+ await UpdateWorkOrderAsync ( request , WorkOrderStatus . Failed , emptyTranscriptException ) ;
411+ }
412+ else
413+ {
414+ // The content is no longer available for some reason.
415+ this . Logger . LogError ( "Content no longer exists. Content ID: {Id}" , requestContentId ) ;
416+ var contentNotFoundException = new ContentNotFoundException ( requestContentId ) ;
417+ await UpdateWorkOrderAsync ( request , WorkOrderStatus . Failed , contentNotFoundException ) ;
418+ }
406419 }
407420 }
408- else
421+ else if ( workOrder ? . Status == WorkOrderStatus . Cancelled )
422+ {
423+ this . Logger . LogWarning ( "Work order has been cancelled. Content ID: {id}, File: {path}" , requestContentId , safePath ) ;
424+ }
425+ else if ( workOrder == null )
409426 {
410427 this . Logger . LogWarning ( "Request ignored because it does not have a work order. Content ID: {id}, File: {path}" , requestContentId , safePath ) ;
411428 }
@@ -455,26 +472,25 @@ private static string GetFormattedTranscript(string transcript)
455472 /// <param name="request"></param>
456473 /// <param name="status"></param>
457474 /// <returns>Whether a work order exists or is not required.</returns>
458- private async Task < bool > UpdateWorkOrderAsync ( TranscriptRequestModel request , WorkOrderStatus status , Exception ? reason = null )
475+ private async Task < API . Areas . Services . Models . WorkOrder . WorkOrderModel ? > UpdateWorkOrderAsync ( TranscriptRequestModel request , WorkOrderStatus status , Exception ? reason = null )
459476 {
460477 if ( request . WorkOrderId > 0 )
461478 {
462479 var workOrder = await this . Api . FindWorkOrderAsync ( request . WorkOrderId ) ;
463480 if ( workOrder != null && ! _ignoreWorkOrders . Contains ( workOrder . Status ) )
464481 {
465482 workOrder . Status = status ;
466- await this . Api . UpdateWorkOrderAsync ( workOrder ) ;
483+ workOrder = await this . Api . UpdateWorkOrderAsync ( workOrder ) ;
467484
468485 if ( status == WorkOrderStatus . Failed && reason != null )
469486 {
470487 await this . SendErrorEmailAsync ( $ "Work order failed for Content ID: { request . ContentId } ", reason ) ;
471488 this . Logger . LogError ( reason , "Work order failed for Content ID: {ContentId}" , request . ContentId ) ;
472489 }
473-
474- return true ;
475490 }
491+ return workOrder ;
476492 }
477- return ! this . Options . AcceptOnlyWorkOrders ;
493+ return null ;
478494 }
479495
480496 /// <summary>
0 commit comments