|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
| 15 | +using Microsoft.Azure.Commands.Common; |
15 | 16 | using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
|
16 |
| -using Microsoft.WindowsAzure.Commands.Common; |
17 | 17 | using System;
|
18 | 18 | using System.Collections.Generic;
|
19 | 19 | using System.Collections.ObjectModel;
|
20 | 20 | using System.IO;
|
21 |
| -using System.Linq; |
22 | 21 | using System.Management.Automation;
|
23 |
| -using System.Management.Automation.Runspaces; |
24 | 22 | using System.Reflection;
|
25 | 23 | using System.Text;
|
| 24 | +using System.Threading; |
26 | 25 |
|
27 | 26 | namespace Microsoft.WindowsAzure.Commands.Utilities.Common
|
28 | 27 | {
|
29 | 28 | public static class CmdletExtensions
|
30 | 29 | {
|
| 30 | + /// <summary> |
| 31 | + /// Execute this cmdlet in the background and return a job that tracks the results |
| 32 | + /// </summary> |
| 33 | + /// <typeparam name="T">The cmdlet type</typeparam> |
| 34 | + /// <param name="cmdlet">The cmdlet to execute</param> |
| 35 | + /// <param name="jobName">The name of the job</param> |
| 36 | + /// <returns>The job tracking cmdlet execution</returns> |
| 37 | + public static Job ExecuteAsJob<T>(this T cmdlet, string jobName) where T : AzurePSCmdlet |
| 38 | + { |
| 39 | + if (cmdlet == null) |
| 40 | + { |
| 41 | + throw new ArgumentNullException(nameof(cmdlet)); |
| 42 | + } |
| 43 | + |
| 44 | + return ExecuteAsJob(cmdlet, jobName, cmd => cmd.ExecuteCmdlet()); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Execute this cmdlet in the background and return a job that tracks the results |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="T">The cmdlet type</typeparam> |
| 51 | + /// <param name="cmdlet">The cmdlet to execute</param> |
| 52 | + /// <param name="jobName">The name of the job</param> |
| 53 | + /// <param name="executor">The method to execute in the background job</param> |
| 54 | + /// <returns>The job tracking cmdlet execution</returns> |
| 55 | + public static Job ExecuteAsJob<T>(this T cmdlet, string jobName, Action<T> executor) where T : AzurePSCmdlet |
| 56 | + { |
| 57 | + if (cmdlet == null) |
| 58 | + { |
| 59 | + throw new ArgumentNullException(nameof(cmdlet)); |
| 60 | + } |
| 61 | + |
| 62 | + if (executor == null) |
| 63 | + { |
| 64 | + throw new ArgumentNullException(nameof(executor)); |
| 65 | + } |
| 66 | + |
| 67 | + var job = AzureLongRunningJob<T>.Create(cmdlet, cmdlet?.MyInvocation?.MyCommand?.Name, jobName, executor); |
| 68 | + cmdlet.SafeAddToJobRepository(job); |
| 69 | + ThreadPool.QueueUserWorkItem(job.RunJob, job); |
| 70 | + return job; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Determine if AsJob is present |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="T">The cmdlet type</typeparam> |
| 77 | + /// <param name="cmdlet">The cmdlet</param> |
| 78 | + /// <returns>True if the cmdlet shoudl run as a Job, otherwise false</returns> |
| 79 | + public static bool AsJobPresent<T>(this T cmdlet) where T : AzurePSCmdlet |
| 80 | + { |
| 81 | + if (cmdlet == null) |
| 82 | + { |
| 83 | + throw new ArgumentNullException(nameof(cmdlet)); |
| 84 | + } |
| 85 | + |
| 86 | + return (cmdlet.MyInvocation?.BoundParameters != null |
| 87 | + && cmdlet.MyInvocation.BoundParameters.ContainsKey("AsJob")); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Execute the given cmdlet synchronously os as a job, based on input parameters |
| 92 | + /// </summary> |
| 93 | + /// <typeparam name="T"></typeparam> |
| 94 | + /// <param name="cmdlet"></param> |
| 95 | + public static void ExecuteSynchronouslyOrAsJob<T>(this T cmdlet) where T: AzurePSCmdlet |
| 96 | + { |
| 97 | + if (cmdlet == null) |
| 98 | + { |
| 99 | + throw new ArgumentNullException(nameof(cmdlet)); |
| 100 | + } |
| 101 | + |
| 102 | + cmdlet.ExecuteSynchronouslyOrAsJob(c => c.ExecuteCmdlet()); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Decide whether to execute this cmdlet as a job or synchronously, based on input parameters |
| 107 | + /// </summary> |
| 108 | + /// <typeparam name="T">The cmdlet type</typeparam> |
| 109 | + /// <param name="cmdlet">The cmdlet to execute</param> |
| 110 | + /// <param name="executor">The cmdlet method to execute</param> |
| 111 | + public static void ExecuteSynchronouslyOrAsJob<T>(this T cmdlet, Action<T> executor) where T : AzurePSCmdlet |
| 112 | + { |
| 113 | + if (cmdlet == null) |
| 114 | + { |
| 115 | + throw new ArgumentNullException(nameof(cmdlet)); |
| 116 | + } |
| 117 | + |
| 118 | + if (executor == null) |
| 119 | + { |
| 120 | + throw new ArgumentNullException(nameof(executor)); |
| 121 | + } |
| 122 | + |
| 123 | + if (cmdlet.AsJobPresent()) |
| 124 | + { |
| 125 | + cmdlet.WriteObject(cmdlet.ExecuteAsJob(cmdlet.ImplementationBackgroundJobDescription, executor)); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + executor(cmdlet); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Safely Attempt to copy a property value from source to target |
| 135 | + /// </summary> |
| 136 | + /// <typeparam name="T">The type fo the source and target objects</typeparam> |
| 137 | + /// <param name="property">The property to copy</param> |
| 138 | + /// <param name="source">The source object to copy from</param> |
| 139 | + /// <param name="target">The target object to copy to</param> |
| 140 | + public static void SafeCopyValue<T>(this PropertyInfo property, T source, T target) |
| 141 | + { |
| 142 | + if (property == null) |
| 143 | + { |
| 144 | + throw new ArgumentNullException(nameof(property)); |
| 145 | + } |
| 146 | + |
| 147 | + try |
| 148 | + { |
| 149 | + property.SetValue(target, property.GetValue(source)); |
| 150 | + } |
| 151 | + catch |
| 152 | + { |
| 153 | + // ignore get and set errors |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Safely Attempt to copy a field value from source to target |
| 159 | + /// </summary> |
| 160 | + /// <typeparam name="T">The type of the source and target objects</typeparam> |
| 161 | + /// <param name="field">The field to copy</param> |
| 162 | + /// <param name="source">The source object to copy from</param> |
| 163 | + /// <param name="target">The target object to copy to</param> |
| 164 | + public static void SafeCopyValue<T>(this FieldInfo field, T source, T target) |
| 165 | + { |
| 166 | + if (field == null) |
| 167 | + { |
| 168 | + throw new ArgumentNullException(nameof(field)); |
| 169 | + } |
| 170 | + |
| 171 | + try |
| 172 | + { |
| 173 | + field.SetValue(target, field.GetValue(source)); |
| 174 | + } |
| 175 | + catch |
| 176 | + { |
| 177 | + // ignore get and set errors |
| 178 | + } |
| 179 | + } |
| 180 | + |
31 | 181 | public static string AsAbsoluteLocation(this string realtivePath)
|
32 | 182 | {
|
33 | 183 | return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, realtivePath));
|
@@ -231,5 +381,19 @@ public static void DisableDataCollection(this AzurePSCmdlet cmdlt)
|
231 | 381 | }
|
232 | 382 |
|
233 | 383 | #endregion
|
| 384 | + |
| 385 | + |
| 386 | + static void SafeAddToJobRepository(this AzurePSCmdlet cmdlet, Job job) |
| 387 | + { |
| 388 | + try |
| 389 | + { |
| 390 | + cmdlet.JobRepository.Add(job); |
| 391 | + } |
| 392 | + catch |
| 393 | + { |
| 394 | + // ignore errors in adding the job to the repository |
| 395 | + } |
| 396 | + } |
| 397 | + |
234 | 398 | }
|
235 | 399 | }
|
0 commit comments