Skip to content

Commit bc8f033

Browse files
committed
Adding Debug trace information to jobs
1 parent 3750fc8 commit bc8f033

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Common/Commands.Common/AzureLongRunningJob.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,16 @@ public virtual void RunJob(object state)
200200
try
201201
{
202202
Start();
203+
WriteDebug(string.Format("[AzureLongRunningJob]: Starting cmdlet execution, must confirm: {0}", _shouldConfirm));
203204
_execute(_cmdlet);
205+
WriteDebug("[AzureLongRunningJob]: Completing cmdlet execution successfully");
204206
Complete();
205207
}
206208
catch (Exception ex)
207209
{
208210
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);
209211
Error.Add(new ErrorRecord(ex, message, ErrorCategory.InvalidOperation, this));
212+
WriteDebug("[AzureLongRunningJob]: Error in cmdlet execution");
210213
Fail();
211214
}
212215
}
@@ -576,15 +579,14 @@ public void WriteWarning(string text)
576579
/// </summary>
577580
/// <param name="state"></param>
578581
/// <returns></returns>
579-
internal bool IsTerminalState(JobState state)
582+
internal bool IsFailedOrCancelled(JobState state)
580583
{
581-
return (state == JobState.Completed || state == JobState.Failed || state == JobState.Stopped);
584+
return (state == JobState.Failed || state == JobState.Stopped);
582585
}
583586

584587
/// <summary>
585588
/// Queue actions that must occur on the cmdlet thread, and block the current thread until they are completed
586589
/// </summary>
587-
/// <typeparam name="V">The output type of the called cmdlet action</typeparam>
588590
/// <param name="shouldMethod">The action to invoke</param>
589591
/// <param name="exceptionThrownOnCmdletThread">Any exception that results</param>
590592
/// <returns>The result of executing the action on the cmdlet thread</returns>
@@ -599,19 +601,23 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
599601
EventHandler<JobStateEventArgs> stateChangedEventHandler =
600602
delegate (object sender, JobStateEventArgs eventArgs)
601603
{
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)
603606
{
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");
604615
lock (resultsLock)
605616
{
606617
closureSafeExceptionThrownOnCmdletThread = new OperationCanceledException();
607618
}
608619
gotResultEvent.Set();
609620
}
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-
}
615621
};
616622
this.StateChanged += stateChangedEventHandler;
617623
Interlocked.MemoryBarrier();
@@ -622,7 +628,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
622628
if (!gotResultEvent.IsSet)
623629
{
624630
this.Block();
625-
631+
WriteDebug("[AzureLongRunningJob]: Blocking job for ShouldMethod");
626632
ShouldMethodInvoker methodInvoker = new ShouldMethodInvoker
627633
{
628634
ShouldMethod = shouldMethod,
@@ -632,6 +638,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
632638

633639
BlockedActions.Enqueue(new ShouldMethodStreamItem(methodInvoker));
634640
gotResultEvent.Wait();
641+
WriteDebug("[AzureLongRunningJob]: ShouldMethod unblocked");
635642
this.Start();
636643

637644
lock (resultsLock)
@@ -646,6 +653,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
646653
}
647654
finally
648655
{
656+
WriteDebug(string.Format("[AzureLongRunningJob]: Removing state changed event handler, exception {0}", closureSafeExceptionThrownOnCmdletThread?.Message));
649657
this.StateChanged -= stateChangedEventHandler;
650658
}
651659
}

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Action/StopAzureVMCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public override void ExecuteCmdlet()
7474
call(this.VirtualMachineClient.DeallocateWithHttpMessagesAsync);
7575
}
7676
}
77+
else
78+
{
79+
WriteDebugWithTimestamp("[Stop-AureRmVMJob]: ShouldMethod returned false");
80+
}
7781
});
7882
}
7983
}

0 commit comments

Comments
 (0)