@@ -200,13 +200,16 @@ public virtual void RunJob(object state)
200
200
try
201
201
{
202
202
Start ( ) ;
203
+ WriteDebug ( string . Format ( "[AzureLongRunningJob]: Starting cmdlet execution, must confirm: {0}" , _shouldConfirm ) ) ;
203
204
_execute ( _cmdlet ) ;
205
+ WriteDebug ( "[AzureLongRunningJob]: Completing cmdlet execution successfully" ) ;
204
206
Complete ( ) ;
205
207
}
206
208
catch ( Exception ex )
207
209
{
208
210
string message = string . Format ( "The cmdlet failed in background execution. The returned error was '{0}'. Please execute the cmdlet again. You may need to execute this cmdlet synchronously, by omitting the '-AsJob' parameter" , ex . Message ) ;
209
211
Error . Add ( new ErrorRecord ( ex , message , ErrorCategory . InvalidOperation , this ) ) ;
212
+ WriteDebug ( "[AzureLongRunningJob]: Error in cmdlet execution" ) ;
210
213
Fail ( ) ;
211
214
}
212
215
}
@@ -576,15 +579,14 @@ public void WriteWarning(string text)
576
579
/// </summary>
577
580
/// <param name="state"></param>
578
581
/// <returns></returns>
579
- internal bool IsTerminalState ( JobState state )
582
+ internal bool IsFailedOrCancelled ( JobState state )
580
583
{
581
- return ( state == JobState . Completed || state == JobState . Failed || state == JobState . Stopped ) ;
584
+ return ( state == JobState . Failed || state == JobState . Stopped ) ;
582
585
}
583
586
584
587
/// <summary>
585
588
/// Queue actions that must occur on the cmdlet thread, and block the current thread until they are completed
586
589
/// </summary>
587
- /// <typeparam name="V">The output type of the called cmdlet action</typeparam>
588
590
/// <param name="shouldMethod">The action to invoke</param>
589
591
/// <param name="exceptionThrownOnCmdletThread">Any exception that results</param>
590
592
/// <returns>The result of executing the action on the cmdlet thread</returns>
@@ -599,19 +601,23 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
599
601
EventHandler < JobStateEventArgs > stateChangedEventHandler =
600
602
delegate ( object sender , JobStateEventArgs eventArgs )
601
603
{
602
- if ( IsTerminalState ( eventArgs . JobStateInfo . State ) || eventArgs . JobStateInfo . State == JobState . Stopping )
604
+ WriteDebug ( string . Format ( "[AzureLongRunningJob]: State change from {0} to {1} because {2}" , eventArgs ? . PreviousJobStateInfo ? . State , eventArgs ? . JobStateInfo ? . State , eventArgs ? . PreviousJobStateInfo ? . Reason ) ) ;
605
+ if ( eventArgs ? . JobStateInfo ? . State != JobState . Blocked && eventArgs ? . PreviousJobStateInfo ? . State == JobState . Blocked )
603
606
{
607
+ WriteDebug ( "[AzureLongRunningJob]: Unblocking job that was previously blocked" ) ;
608
+ // if receive-job is called, unblock by executing delayed powershell actions
609
+ UnblockJob ( ) ;
610
+ }
611
+
612
+ if ( IsFailedOrCancelled ( eventArgs . JobStateInfo . State ) || eventArgs . JobStateInfo . State == JobState . Stopping )
613
+ {
614
+ WriteDebug ( "[AzureLongRunningJob]: Unblocking job due to stoppage or failure" ) ;
604
615
lock ( resultsLock )
605
616
{
606
617
closureSafeExceptionThrownOnCmdletThread = new OperationCanceledException ( ) ;
607
618
}
608
619
gotResultEvent . Set ( ) ;
609
620
}
610
- else if ( eventArgs ? . JobStateInfo ? . State == JobState . Running && eventArgs ? . PreviousJobStateInfo ? . State == JobState . Blocked )
611
- {
612
- // if receive-job is called, unblock by executing delayed powershell actions
613
- UnblockJob ( ) ;
614
- }
615
621
} ;
616
622
this . StateChanged += stateChangedEventHandler ;
617
623
Interlocked . MemoryBarrier ( ) ;
@@ -622,7 +628,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
622
628
if ( ! gotResultEvent . IsSet )
623
629
{
624
630
this . Block ( ) ;
625
-
631
+ WriteDebug ( "[AzureLongRunningJob]: Blocking job for ShouldMethod" ) ;
626
632
ShouldMethodInvoker methodInvoker = new ShouldMethodInvoker
627
633
{
628
634
ShouldMethod = shouldMethod ,
@@ -632,6 +638,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
632
638
633
639
BlockedActions . Enqueue ( new ShouldMethodStreamItem ( methodInvoker ) ) ;
634
640
gotResultEvent . Wait ( ) ;
641
+ WriteDebug ( "[AzureLongRunningJob]: ShouldMethod unblocked" ) ;
635
642
this . Start ( ) ;
636
643
637
644
lock ( resultsLock )
@@ -646,6 +653,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
646
653
}
647
654
finally
648
655
{
656
+ WriteDebug ( string . Format ( "[AzureLongRunningJob]: Removing state changed event handler, exception {0}" , closureSafeExceptionThrownOnCmdletThread ? . Message ) ) ;
649
657
this . StateChanged -= stateChangedEventHandler ;
650
658
}
651
659
}
0 commit comments