Skip to content

Commit d39347d

Browse files
committed
Improve exceptions for runtime issues, harden state machine and add tests
1 parent bc8f033 commit d39347d

File tree

8 files changed

+573
-71
lines changed

8 files changed

+573
-71
lines changed

src/Common/Commands.Common/AzureLongRunningJob.cs

Lines changed: 228 additions & 64 deletions
Large diffs are not rendered by default.

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Reference Include="System.Xml" />
123123
</ItemGroup>
124124
<ItemGroup>
125+
<Compile Include="LongRunningJobCancelledException.cs" />
125126
<Compile Include="ShouldMethodInvoker.cs" />
126127
<Compile Include="AzureLongRunningJob.cs" />
127128
<Compile Include="Serialization\LegacyAzureAccount.cs" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.WindowsAzure.Commands.Common
18+
{
19+
/// <summary>
20+
/// Exception used to terminate runtime functions when a job is stopping
21+
/// </summary>
22+
[Serializable]
23+
public class LongRunningJobCancelledException :InvalidOperationException
24+
{
25+
public LongRunningJobCancelledException() : base()
26+
{
27+
}
28+
29+
public LongRunningJobCancelledException(string message) : base(message)
30+
{
31+
}
32+
33+
public LongRunningJobCancelledException(string message, Exception innerException) : base(message, innerException)
34+
{
35+
}
36+
}
37+
}

src/Common/Commands.Common/Properties/Resources.Designer.cs

Lines changed: 144 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common/Commands.Common/Properties/Resources.resx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,4 +1589,58 @@ The data is collected by Microsoft.
15891589
Use the Disable-AzureDataCollection cmdlet to turn the feature Off. The cmdlet can be found in the Azure module. To disable data collection: PS &gt; Disable-AzureDataCollection.
15901590
Use the Enable-AzureDataCollection cmdlet to turn the feature On. The cmdlet can be found in the Azure module. To enable data collection: PS &gt; Enable-AzureDataCollection.</value>
15911591
</data>
1592+
<data name="BaseShouldMethodFailureReason" xml:space="preserve">
1593+
<value>Execution failed because a background thread could not prompt the user.</value>
1594+
</data>
1595+
<data name="LROJobName" xml:space="preserve">
1596+
<value>Azure Long-Running Job</value>
1597+
</data>
1598+
<data name="LROTaskExceptionMessage" xml:space="preserve">
1599+
<value>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.</value>
1600+
<comment>0(string): exception message in background task</comment>
1601+
</data>
1602+
<data name="ShouldContinueFail" xml:space="preserve">
1603+
<value>Please execute the cmdlet again and include the 'Force' parameter, if available, to avoid unnecessary prompts.</value>
1604+
</data>
1605+
<data name="ShouldProcessFailConfirm" xml:space="preserve">
1606+
<value>Please execute the cmdlet again and omit the 'Confirm' parameter when using the 'AsJob' parameter.</value>
1607+
</data>
1608+
<data name="ShouldProcessFailImpact" xml:space="preserve">
1609+
<value>Please increase the user $ConfirmPreference setting, or include turn off confirmation using '-Confirm:$false' when using the 'AsJob' parameter and execute the cmdet again.</value>
1610+
</data>
1611+
<data name="ShouldProcessFailWhatIf" xml:space="preserve">
1612+
<value>Please execute the cmdlet again and omit the 'WhatIf' parameter when using the 'AsJob' parameter.</value>
1613+
</data>
1614+
<data name="TraceBeginLROJob" xml:space="preserve">
1615+
<value>[AzureLongRunningJob]: Starting cmdlet execution, setting for cmdlet confirmation required: '{0}'</value>
1616+
<comment>0(bool): whether cmdlet confirmation is required</comment>
1617+
</data>
1618+
<data name="TraceBlockLROThread" xml:space="preserve">
1619+
<value>[AzureLongRunningJob]: Blocking job for ShouldMethod '{0}'</value>
1620+
<comment>0(string): method type</comment>
1621+
</data>
1622+
<data name="TraceEndLROJob" xml:space="preserve">
1623+
<value>[AzureLongRunningJob]: Completing cmdlet execution in RunJob</value>
1624+
</data>
1625+
<data name="TraceHandleLROStateChange" xml:space="preserve">
1626+
<value>[AzureLongRunningJob]: State change from '{0}' to '{1}' because '{2}'</value>
1627+
<comment>0(string): last state, 1(string): new state, 2(string): state change reason</comment>
1628+
</data>
1629+
<data name="TraceHandlerCancelJob" xml:space="preserve">
1630+
<value>[AzureLongRunningJob]: Unblocking job due to stoppage or failure</value>
1631+
</data>
1632+
<data name="TraceHandlerUnblockJob" xml:space="preserve">
1633+
<value>[AzureLongRunningJob]: Unblocking job that was previously blocked.</value>
1634+
</data>
1635+
<data name="TraceLROJobException" xml:space="preserve">
1636+
<value>[AzureLongRunningJob]: Error in cmdlet execution</value>
1637+
</data>
1638+
<data name="TraceRemoveLROEventHandler" xml:space="preserve">
1639+
<value>[AzureLongRunningJob]: Removing state changed event handler, exception '{0}'</value>
1640+
<comment>0(string): exception message</comment>
1641+
</data>
1642+
<data name="TraceUnblockLROThread" xml:space="preserve">
1643+
<value>[AzureLongRunningJob]: ShouldMethod '{0}' unblocked.</value>
1644+
<comment>0(string): methodType</comment>
1645+
</data>
15921646
</root>

src/Common/Commands.Common/ShouldMethodInvoker.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1615
using System;
1716
using System.Management.Automation;
1817
using System.Threading;
1918

2019
namespace Microsoft.WindowsAzure.Commands.Common
2120
{
21+
/// <summary>
22+
/// Tracks the type of should method being executed
23+
/// </summary>
24+
internal enum ShouldMethodType
25+
{
26+
ShouldProcess= 0,
27+
ShouldContinue = 1,
28+
HasTransaction = 2
29+
}
30+
2231
/// <summary>
2332
/// Representation of a deferred powershell method execution for SHouldProcess or SHouldContinue which must execute on the cmdlet thread
2433
/// </summary>
@@ -33,5 +42,7 @@ internal class ShouldMethodInvoker
3342
public bool MethodResult { get; set; }
3443

3544
public Exception ThrownException { get; set; }
45+
46+
public ShouldMethodType MethodType { get; set; }
3647
}
3748
}

src/Common/Commands.Common/ShouldMethodStreamItem.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public void ExecuteShouldMethod(Cmdlet cmdlet)
6666

6767
throw;
6868
}
69-
finally
70-
{
71-
MethodInvoker?.Finished?.Set();
72-
}
7369
}
7470
}
7571
}

0 commit comments

Comments
 (0)