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 ;
@@ -196,14 +248,15 @@ public static U CopyCmdlet<U>(U cmdlet) where U : AzurePSCmdlet
196
248
field . SafeCopyValue ( source : cmdlet , target : returnValue ) ;
197
249
}
198
250
251
+ cmdlet . SafeCopyParameterSet ( returnValue ) ;
199
252
return returnValue as U ;
200
253
}
201
254
202
255
/// <summary>
203
256
/// Run a cmdlet execution as a Job - this is intended to run in a background thread
204
257
/// </summary>
205
258
/// <param name="state">The Job record that will track the progress of this job </param>
206
- public virtual void RunJob ( object state )
259
+ public override void RunJob ( object state )
207
260
{
208
261
if ( TryStart ( ) )
209
262
{
@@ -239,7 +292,7 @@ public virtual void RunJob(object state)
239
292
/// Return cmdlet runtime that will report results to this job
240
293
/// </summary>
241
294
/// <returns>An IcommandRuntime that reports results to this job</returns>
242
- public virtual ICommandRuntime GetJobRuntime ( )
295
+ public override ICommandRuntime GetJobRuntime ( )
243
296
{
244
297
return this as ICommandRuntime ;
245
298
}
@@ -307,7 +360,7 @@ internal string GetShouldMethodFailureReaon(T cmdlet, ShouldMethodStreamItem met
307
360
/// <summary>
308
361
/// Mark the task as started
309
362
/// </summary>
310
- public bool TryStart ( )
363
+ public override bool TryStart ( )
311
364
{
312
365
bool result = false ;
313
366
lock ( _lockObject )
@@ -326,7 +379,7 @@ public bool TryStart()
326
379
/// <summary>
327
380
/// Mark the task as blocked
328
381
/// </summary>
329
- public bool TryBlock ( )
382
+ public override bool TryBlock ( )
330
383
{
331
384
bool result = false ;
332
385
lock ( _lockObject )
@@ -345,7 +398,7 @@ public bool TryBlock()
345
398
/// <summary>
346
399
/// Mark the job as Failed
347
400
/// </summary>
348
- public void Fail ( )
401
+ public override void Fail ( )
349
402
{
350
403
lock ( _lockObject )
351
404
{
@@ -357,7 +410,7 @@ public void Fail()
357
410
/// <summary>
358
411
/// Mark the job as successfully complete
359
412
/// </summary>
360
- public void Complete ( )
413
+ public override void Complete ( )
361
414
{
362
415
lock ( _lockObject )
363
416
{
@@ -378,7 +431,7 @@ public void Complete()
378
431
/// <summary>
379
432
/// Mark the job as cancelled
380
433
/// </summary>
381
- public void Cancel ( )
434
+ public override void Cancel ( )
382
435
{
383
436
lock ( _lockObject )
384
437
{
@@ -787,8 +840,7 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
787
840
try
788
841
{
789
842
stateChangedEventHandler ( null , new JobStateEventArgs ( this . JobStateInfo ) ) ;
790
-
791
- if ( ! gotResultEvent . IsSet && this . TryBlock ( ) )
843
+ if ( ! gotResultEvent . IsSet )
792
844
{
793
845
WriteDebug ( string . Format ( Resources . TraceBlockLROThread , methodType ) ) ;
794
846
ShouldMethodInvoker methodInvoker = new ShouldMethodInvoker
@@ -800,16 +852,18 @@ private bool InvokeShouldMethodAndWaitForResults(Func<Cmdlet, bool> shouldMethod
800
852
} ;
801
853
802
854
BlockedActions . Enqueue ( new ShouldMethodStreamItem ( methodInvoker ) ) ;
803
-
804
- gotResultEvent . Wait ( ) ;
805
- WriteDebug ( string . Format ( Resources . TraceUnblockLROThread , shouldMethod ) ) ;
806
- TryStart ( ) ;
807
- lock ( resultsLock )
855
+ if ( this . TryBlock ( ) )
808
856
{
809
- 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 )
810
861
{
811
- closureSafeExceptionThrownOnCmdletThread = methodInvoker . ThrownException ;
812
- 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
+ }
813
867
}
814
868
}
815
869
}
0 commit comments