@@ -275,160 +275,167 @@ internal static Timestamp ToTimestamp(this DateTime dateTime)
275275 /// value that was provided by the corresponding <see cref="P.WorkItem"/> that triggered the orchestrator execution.
276276 /// </param>
277277 /// <param name="entityConversionState">The entity conversion state, or null if no conversion is required.</param>
278+ /// <param name="requiresHistory">Whether or not a history is required to complete the orchestration request and none was provided.</param>
278279 /// <returns>The orchestrator response.</returns>
279280 /// <exception cref="NotSupportedException">When an orchestrator action is unknown.</exception>
280281 internal static P . OrchestratorResponse ConstructOrchestratorResponse (
281282 string instanceId ,
282283 string ? customStatus ,
283- IEnumerable < OrchestratorAction > actions ,
284+ IEnumerable < OrchestratorAction > ? actions ,
284285 string completionToken ,
285- EntityConversionState ? entityConversionState )
286+ EntityConversionState ? entityConversionState ,
287+ bool requiresHistory = false )
286288 {
287- Check . NotNull ( actions ) ;
288289 var response = new P . OrchestratorResponse
289290 {
290291 InstanceId = instanceId ,
291292 CustomStatus = customStatus ,
292293 CompletionToken = completionToken ,
294+ RequiresHistory = requiresHistory ,
293295 } ;
294296
295- foreach ( OrchestratorAction action in actions )
297+ // If a history is required and the orchestration request was not completed, then there is no list of actions.
298+ if ( ! requiresHistory )
296299 {
297- var protoAction = new P . OrchestratorAction { Id = action . Id } ;
298-
299- switch ( action . OrchestratorActionType )
300+ Check . NotNull ( actions ) ;
301+ foreach ( OrchestratorAction action in actions )
300302 {
301- case OrchestratorActionType . ScheduleOrchestrator :
302- var scheduleTaskAction = ( ScheduleTaskOrchestratorAction ) action ;
303- protoAction . ScheduleTask = new P . ScheduleTaskAction
304- {
305- Name = scheduleTaskAction . Name ,
306- Version = scheduleTaskAction . Version ,
307- Input = scheduleTaskAction . Input ,
308- } ;
303+ var protoAction = new P . OrchestratorAction { Id = action . Id } ;
309304
310- if ( scheduleTaskAction . Tags != null )
311- {
312- foreach ( KeyValuePair < string , string > tag in scheduleTaskAction . Tags )
305+ switch ( action . OrchestratorActionType )
306+ {
307+ case OrchestratorActionType . ScheduleOrchestrator :
308+ var scheduleTaskAction = ( ScheduleTaskOrchestratorAction ) action ;
309+ protoAction . ScheduleTask = new P . ScheduleTaskAction
313310 {
314- protoAction . ScheduleTask . Tags [ tag . Key ] = tag . Value ;
311+ Name = scheduleTaskAction . Name ,
312+ Version = scheduleTaskAction . Version ,
313+ Input = scheduleTaskAction . Input ,
314+ } ;
315+
316+ if ( scheduleTaskAction . Tags != null )
317+ {
318+ foreach ( KeyValuePair < string , string > tag in scheduleTaskAction . Tags )
319+ {
320+ protoAction . ScheduleTask . Tags [ tag . Key ] = tag . Value ;
321+ }
315322 }
316- }
317323
318- break ;
319- case OrchestratorActionType . CreateSubOrchestration :
320- var subOrchestrationAction = ( CreateSubOrchestrationAction ) action ;
321- protoAction . CreateSubOrchestration = new P . CreateSubOrchestrationAction
322- {
323- Input = subOrchestrationAction . Input ,
324- InstanceId = subOrchestrationAction . InstanceId ,
325- Name = subOrchestrationAction . Name ,
326- Version = subOrchestrationAction . Version ,
327- } ;
328- break ;
329- case OrchestratorActionType . CreateTimer :
330- var createTimerAction = ( CreateTimerOrchestratorAction ) action ;
331- protoAction . CreateTimer = new P . CreateTimerAction
332- {
333- FireAt = createTimerAction . FireAt . ToTimestamp ( ) ,
334- } ;
335- break ;
336- case OrchestratorActionType . SendEvent :
337- var sendEventAction = ( SendEventOrchestratorAction ) action ;
338- if ( sendEventAction . Instance == null )
339- {
340- throw new ArgumentException (
341- $ "{ nameof ( SendEventOrchestratorAction ) } cannot have a null Instance property!") ;
342- }
343-
344- if ( entityConversionState is not null
345- && DTCore . Common . Entities . IsEntityInstance ( sendEventAction . Instance . InstanceId )
346- && sendEventAction . EventName is not null
347- && sendEventAction . EventData is not null )
348- {
349- P . SendEntityMessageAction sendAction = new P . SendEntityMessageAction ( ) ;
350- protoAction . SendEntityMessage = sendAction ;
324+ break ;
325+ case OrchestratorActionType . CreateSubOrchestration :
326+ var subOrchestrationAction = ( CreateSubOrchestrationAction ) action ;
327+ protoAction . CreateSubOrchestration = new P . CreateSubOrchestrationAction
328+ {
329+ Input = subOrchestrationAction . Input ,
330+ InstanceId = subOrchestrationAction . InstanceId ,
331+ Name = subOrchestrationAction . Name ,
332+ Version = subOrchestrationAction . Version ,
333+ } ;
334+ break ;
335+ case OrchestratorActionType . CreateTimer :
336+ var createTimerAction = ( CreateTimerOrchestratorAction ) action ;
337+ protoAction . CreateTimer = new P . CreateTimerAction
338+ {
339+ FireAt = createTimerAction . FireAt . ToTimestamp ( ) ,
340+ } ;
341+ break ;
342+ case OrchestratorActionType . SendEvent :
343+ var sendEventAction = ( SendEventOrchestratorAction ) action ;
344+ if ( sendEventAction . Instance == null )
345+ {
346+ throw new ArgumentException (
347+ $ "{ nameof ( SendEventOrchestratorAction ) } cannot have a null Instance property!") ;
348+ }
351349
352- EntityConversions . DecodeEntityMessageAction (
353- sendEventAction . EventName ,
354- sendEventAction . EventData ,
355- sendEventAction . Instance . InstanceId ,
356- sendAction ,
357- out string requestId ) ;
350+ if ( entityConversionState is not null
351+ && DTCore . Common . Entities . IsEntityInstance ( sendEventAction . Instance . InstanceId )
352+ && sendEventAction . EventName is not null
353+ && sendEventAction . EventData is not null )
354+ {
355+ P . SendEntityMessageAction sendAction = new P . SendEntityMessageAction ( ) ;
356+ protoAction . SendEntityMessage = sendAction ;
358357
359- entityConversionState . EntityRequestIds . Add ( requestId ) ;
358+ EntityConversions . DecodeEntityMessageAction (
359+ sendEventAction . EventName ,
360+ sendEventAction . EventData ,
361+ sendEventAction . Instance . InstanceId ,
362+ sendAction ,
363+ out string requestId ) ;
360364
361- switch ( sendAction . EntityMessageTypeCase )
362- {
363- case P . SendEntityMessageAction . EntityMessageTypeOneofCase . EntityLockRequested :
364- entityConversionState . AddUnlockObligations ( sendAction . EntityLockRequested ) ;
365- break ;
366- case P . SendEntityMessageAction . EntityMessageTypeOneofCase . EntityUnlockSent :
367- entityConversionState . RemoveUnlockObligation ( sendAction . EntityUnlockSent . TargetInstanceId ) ;
368- break ;
369- default :
370- break ;
365+ entityConversionState . EntityRequestIds . Add ( requestId ) ;
366+
367+ switch ( sendAction . EntityMessageTypeCase )
368+ {
369+ case P . SendEntityMessageAction . EntityMessageTypeOneofCase . EntityLockRequested :
370+ entityConversionState . AddUnlockObligations ( sendAction . EntityLockRequested ) ;
371+ break ;
372+ case P . SendEntityMessageAction . EntityMessageTypeOneofCase . EntityUnlockSent :
373+ entityConversionState . RemoveUnlockObligation ( sendAction . EntityUnlockSent . TargetInstanceId ) ;
374+ break ;
375+ default :
376+ break ;
377+ }
371378 }
372- }
373- else
374- {
375- protoAction . SendEvent = new P . SendEventAction
379+ else
376380 {
377- Instance = sendEventAction . Instance . ToProtobuf ( ) ,
378- Name = sendEventAction . EventName ,
379- Data = sendEventAction . EventData ,
380- } ;
381- }
381+ protoAction . SendEvent = new P . SendEventAction
382+ {
383+ Instance = sendEventAction . Instance . ToProtobuf ( ) ,
384+ Name = sendEventAction . EventName ,
385+ Data = sendEventAction . EventData ,
386+ } ;
387+ }
382388
383- break ;
384- case OrchestratorActionType . OrchestrationComplete :
389+ break ;
390+ case OrchestratorActionType . OrchestrationComplete :
385391
386- if ( entityConversionState is not null )
387- {
388- // as a precaution, unlock any entities that were not unlocked for some reason, before
389- // completing the orchestration.
390- foreach ( ( string target , string criticalSectionId ) in entityConversionState . ResetObligations ( ) )
392+ if ( entityConversionState is not null )
391393 {
392- response . Actions . Add ( new P . OrchestratorAction
394+ // as a precaution, unlock any entities that were not unlocked for some reason, before
395+ // completing the orchestration.
396+ foreach ( ( string target , string criticalSectionId ) in entityConversionState . ResetObligations ( ) )
393397 {
394- Id = action . Id ,
395- SendEntityMessage = new P . SendEntityMessageAction
398+ response . Actions . Add ( new P . OrchestratorAction
396399 {
397- EntityUnlockSent = new P . EntityUnlockSentEvent
400+ Id = action . Id ,
401+ SendEntityMessage = new P . SendEntityMessageAction
398402 {
399- CriticalSectionId = criticalSectionId ,
400- TargetInstanceId = target ,
401- ParentInstanceId = entityConversionState . CurrentInstance ? . InstanceId ,
403+ EntityUnlockSent = new P . EntityUnlockSentEvent
404+ {
405+ CriticalSectionId = criticalSectionId ,
406+ TargetInstanceId = target ,
407+ ParentInstanceId = entityConversionState . CurrentInstance ? . InstanceId ,
408+ } ,
402409 } ,
403- } ,
404- } ) ;
410+ } ) ;
411+ }
405412 }
406- }
407413
408- var completeAction = ( OrchestrationCompleteOrchestratorAction ) action ;
409- protoAction . CompleteOrchestration = new P . CompleteOrchestrationAction
410- {
411- CarryoverEvents =
414+ var completeAction = ( OrchestrationCompleteOrchestratorAction ) action ;
415+ protoAction . CompleteOrchestration = new P . CompleteOrchestrationAction
416+ {
417+ CarryoverEvents =
412418 {
413419 // TODO
414420 } ,
415- Details = completeAction . Details ,
416- NewVersion = completeAction . NewVersion ,
417- OrchestrationStatus = completeAction . OrchestrationStatus . ToProtobuf ( ) ,
418- Result = completeAction . Result ,
419- } ;
421+ Details = completeAction . Details ,
422+ NewVersion = completeAction . NewVersion ,
423+ OrchestrationStatus = completeAction . OrchestrationStatus . ToProtobuf ( ) ,
424+ Result = completeAction . Result ,
425+ } ;
420426
421- if ( completeAction . OrchestrationStatus == OrchestrationStatus . Failed )
422- {
423- protoAction . CompleteOrchestration . FailureDetails = completeAction . FailureDetails . ToProtobuf ( ) ;
424- }
427+ if ( completeAction . OrchestrationStatus == OrchestrationStatus . Failed )
428+ {
429+ protoAction . CompleteOrchestration . FailureDetails = completeAction . FailureDetails . ToProtobuf ( ) ;
430+ }
425431
426- break ;
427- default :
428- throw new NotSupportedException ( $ "Unknown orchestrator action: { action . OrchestratorActionType } ") ;
429- }
432+ break ;
433+ default :
434+ throw new NotSupportedException ( $ "Unknown orchestrator action: { action . OrchestratorActionType } ") ;
435+ }
430436
431- response . Actions . Add ( protoAction ) ;
437+ response . Actions . Add ( protoAction ) ;
438+ }
432439 }
433440
434441 return response ;
0 commit comments