26
26
27
27
namespace Microsoft . Azure . Commands . Common
28
28
{
29
- public class AzureLongRunningJob < T > : Job , ICommandRuntime where T : AzurePSCmdlet
29
+ /// <summary>
30
+ /// Abstract class for uniform processing of jobs
31
+ /// </summary>
32
+ public abstract class AzureLongRunningJob : Job
33
+ {
34
+ protected AzureLongRunningJob ( string command , string name ) : base ( command , name )
35
+ {
36
+ }
37
+
38
+ /// <summary>
39
+ /// Run a cmdlet execution as a Job - this is intended to run in a background thread
40
+ /// </summary>
41
+ /// <param name="state">The Job record that will track the progress of this job </param>
42
+ public abstract void RunJob ( object state ) ;
43
+
44
+ /// <summary>
45
+ /// Return cmdlet runtime that will report results to this job
46
+ /// </summary>
47
+ /// <returns>An IcommandRuntime that reports results to this job</returns>
48
+ public abstract ICommandRuntime GetJobRuntime ( ) ;
49
+
50
+ /// <summary>
51
+ /// Mark the job as started
52
+ /// </summary>
53
+ public abstract bool TryStart ( ) ;
54
+
55
+ /// <summary>
56
+ /// Mark the job as Blocked
57
+ /// </summary>
58
+ public abstract bool TryBlock ( ) ;
59
+
60
+
61
+ /// <summary>
62
+ /// Complete the job (will mark job as Completed or Failed, depending on the execution details)
63
+ /// </summary>
64
+ public abstract void Complete ( ) ;
65
+
66
+ /// <summary>
67
+ /// Mark the Job as Failed
68
+ /// </summary>
69
+ public abstract void Fail ( ) ;
70
+
71
+ /// <summary>
72
+ /// Stop the job
73
+ /// </summary>
74
+ public abstract void Cancel ( ) ;
75
+ }
76
+
77
+ /// <summary>
78
+ /// Cmdlet-specific Implementation class for long running jobs
79
+ /// </summary>
80
+ /// <typeparam name="T">The type of the cmdlet being executed</typeparam>
81
+ public class AzureLongRunningJob < T > : AzureLongRunningJob , ICommandRuntime where T : AzurePSCmdlet
30
82
{
31
83
string _status = "Running" ;
32
84
T _cmdlet ;
@@ -204,7 +256,7 @@ public static U CopyCmdlet<U>(U cmdlet) where U : AzurePSCmdlet
204
256
/// Run a cmdlet execution as a Job - this is intended to run in a background thread
205
257
/// </summary>
206
258
/// <param name="state">The Job record that will track the progress of this job </param>
207
- public virtual void RunJob ( object state )
259
+ public override void RunJob ( object state )
208
260
{
209
261
if ( TryStart ( ) )
210
262
{
@@ -240,7 +292,7 @@ public virtual void RunJob(object state)
240
292
/// Return cmdlet runtime that will report results to this job
241
293
/// </summary>
242
294
/// <returns>An IcommandRuntime that reports results to this job</returns>
243
- public virtual ICommandRuntime GetJobRuntime ( )
295
+ public override ICommandRuntime GetJobRuntime ( )
244
296
{
245
297
return this as ICommandRuntime ;
246
298
}
@@ -308,7 +360,7 @@ internal string GetShouldMethodFailureReaon(T cmdlet, ShouldMethodStreamItem met
308
360
/// <summary>
309
361
/// Mark the task as started
310
362
/// </summary>
311
- public bool TryStart ( )
363
+ public override bool TryStart ( )
312
364
{
313
365
bool result = false ;
314
366
lock ( _lockObject )
@@ -327,7 +379,7 @@ public bool TryStart()
327
379
/// <summary>
328
380
/// Mark the task as blocked
329
381
/// </summary>
330
- public bool TryBlock ( )
382
+ public override bool TryBlock ( )
331
383
{
332
384
bool result = false ;
333
385
lock ( _lockObject )
@@ -346,7 +398,7 @@ public bool TryBlock()
346
398
/// <summary>
347
399
/// Mark the job as Failed
348
400
/// </summary>
349
- public void Fail ( )
401
+ public override void Fail ( )
350
402
{
351
403
lock ( _lockObject )
352
404
{
@@ -358,7 +410,7 @@ public void Fail()
358
410
/// <summary>
359
411
/// Mark the job as successfully complete
360
412
/// </summary>
361
- public void Complete ( )
413
+ public override void Complete ( )
362
414
{
363
415
lock ( _lockObject )
364
416
{
@@ -379,7 +431,7 @@ public void Complete()
379
431
/// <summary>
380
432
/// Mark the job as cancelled
381
433
/// </summary>
382
- public void Cancel ( )
434
+ public override void Cancel ( )
383
435
{
384
436
lock ( _lockObject )
385
437
{
@@ -788,8 +840,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
788
840
try
789
841
{
790
842
stateChangedEventHandler ( null , new JobStateEventArgs ( this . JobStateInfo ) ) ;
791
-
792
- if ( ! gotResultEvent . IsSet && this . TryBlock ( ) )
843
+ if ( ! gotResultEvent . IsSet )
793
844
{
794
845
WriteDebug ( string . Format ( Resources . TraceBlockLROThread , methodType ) ) ;
795
846
ShouldMethodInvoker methodInvoker = new ShouldMethodInvoker
@@ -801,16 +852,18 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
801
852
} ;
802
853
803
854
BlockedActions . Enqueue ( new ShouldMethodStreamItem ( methodInvoker ) ) ;
804
-
805
- gotResultEvent . Wait ( ) ;
806
- WriteDebug ( string . Format ( Resources . TraceUnblockLROThread , shouldMethod ) ) ;
807
- TryStart ( ) ;
808
- lock ( resultsLock )
855
+ if ( this . TryBlock ( ) )
809
856
{
810
- if ( closureSafeExceptionThrownOnCmdletThread == null ) // stateChangedEventHandler didn't set the results? = ok to clobber results?
857
+ gotResultEvent . Wait ( ) ;
858
+ WriteDebug ( string . Format ( Resources . TraceUnblockLROThread , shouldMethod ) ) ;
859
+ TryStart ( ) ;
860
+ lock ( resultsLock )
811
861
{
812
- closureSafeExceptionThrownOnCmdletThread = methodInvoker . ThrownException ;
813
- methodResult = methodInvoker . MethodResult ;
862
+ if ( closureSafeExceptionThrownOnCmdletThread == null ) // stateChangedEventHandler didn't set the results? = ok to clobber results?
863
+ {
864
+ closureSafeExceptionThrownOnCmdletThread = methodInvoker . ThrownException ;
865
+ methodResult = methodInvoker . MethodResult ;
866
+ }
814
867
}
815
868
}
816
869
}
0 commit comments