PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/HDInsight/Commands.HDInsight/Cmdlet/WaitAzureHDInsightJobCmdlet.cs

https://gitlab.com/jslee1/azure-powershell
C# | 248 lines | 200 code | 16 blank | 32 comment | 11 complexity | 1eef1e8aaaad938dbd069b3e3d99b46c MD5 | raw file
  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. using System;
  15. using System.Globalization;
  16. using System.Management.Automation;
  17. using System.Reflection;
  18. using System.Security.Cryptography.X509Certificates;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.Commands.BaseCommandInterfaces;
  22. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.Commands.CommandInterfaces;
  23. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.DataObjects;
  24. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.GetAzureHDInsightClusters;
  25. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.GetAzureHDInsightClusters.Extensions;
  26. using Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.ServiceLocation;
  27. using Microsoft.WindowsAzure.Management.HDInsight.Logging;
  28. namespace Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.PSCmdlets
  29. {
  30. /// <summary>
  31. /// Cmdlet that lists all the Jobs running on a HDInsight cluster.
  32. /// </summary>
  33. [Cmdlet(VerbsLifecycle.Wait, AzureHdInsightPowerShellConstants.AzureHDInsightJobs,
  34. DefaultParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByName)]
  35. [OutputType(typeof(AzureHDInsightJob))]
  36. public class WaitAzureHDInsightJobCmdlet : AzureHDInsightCmdlet, IWaitAzureHDInsightJobBase
  37. {
  38. private readonly IWaitAzureHDInsightJobCommand command;
  39. /// <summary>
  40. /// Initializes a new instance of the WaitAzureHDInsightJobCmdlet class.
  41. /// </summary>
  42. public WaitAzureHDInsightJobCmdlet()
  43. {
  44. this.command = ServiceLocator.Instance.Locate<IAzureHDInsightCommandFactory>().CreateWaitJobs();
  45. }
  46. /// <inheritdoc />
  47. [Parameter(Mandatory = false, HelpMessage = "The management certificate used to manage the Azure subscription.",
  48. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  49. [Alias(AzureHdInsightPowerShellConstants.AliasCert)]
  50. public X509Certificate2 Certificate
  51. {
  52. get { return this.command.Certificate; }
  53. set { this.command.Certificate = value; }
  54. }
  55. /// <inheritdoc />
  56. [Parameter(Mandatory = false, HelpMessage = "The HostedService to use when managing the HDInsight cluster.",
  57. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  58. [Alias(AzureHdInsightPowerShellConstants.AliasCloudServiceName)]
  59. public string HostedService
  60. {
  61. get { return this.command.HostedService; }
  62. set { this.command.HostedService = value; }
  63. }
  64. /// <inheritdoc />
  65. [Parameter(Mandatory = true, HelpMessage = "The name of the cluster the Job is running on", ValueFromPipelineByPropertyName = true,
  66. ValueFromPipeline = true, ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetWaitJobById)]
  67. public string Cluster
  68. {
  69. get { return this.command.Cluster; }
  70. set { this.command.Cluster = value; }
  71. }
  72. /// <inheritdoc />
  73. [Parameter(Mandatory = false, Position = 1, HelpMessage = "The credentials to connect to Azure HDInsight cluster.",
  74. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByName)]
  75. [Parameter(Mandatory = false, HelpMessage = "The credentials to connect to Azure HDInsight cluster.",
  76. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetWaitJobByJob)]
  77. [Parameter(Mandatory = false, HelpMessage = "The credentials to connect to Azure HDInsight cluster.",
  78. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetWaitJobById)]
  79. [Alias(AzureHdInsightPowerShellConstants.AliasCredentials)]
  80. public PSCredential Credential
  81. {
  82. get { return this.command.Credential; }
  83. set { this.command.Credential = value; }
  84. }
  85. /// <inheritdoc />
  86. [Parameter(Mandatory = false, HelpMessage = "The Endpoint to use when connecting to Azure.",
  87. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  88. public Uri Endpoint
  89. {
  90. get { return this.command.Endpoint; }
  91. set { this.command.Endpoint = value; }
  92. }
  93. /// <inheritdoc />
  94. [Parameter(Mandatory = false, HelpMessage = "Rule for SSL errors with HDInsight client.",
  95. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  96. public bool IgnoreSslErrors
  97. {
  98. get { return this.command.IgnoreSslErrors; }
  99. set { this.command.IgnoreSslErrors = value; }
  100. }
  101. /// <inheritdoc />
  102. [Parameter(Mandatory = true, HelpMessage = "The Jobs to wait for.", ValueFromPipeline = true,
  103. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetWaitJobByJob)]
  104. [Parameter(Mandatory = true, HelpMessage = "The Jobs to wait for.", ValueFromPipeline = true,
  105. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  106. public AzureHDInsightJob Job
  107. {
  108. get { return this.command.Job; }
  109. set
  110. {
  111. this.command.Job = value;
  112. if (value.IsNotNull())
  113. {
  114. this.command.JobId = this.command.Job.JobId;
  115. this.command.Cluster = this.command.Job.Cluster;
  116. }
  117. }
  118. }
  119. /// <inheritdoc />
  120. [Parameter(Mandatory = true, HelpMessage = "The Id of the Job to wait for.", ValueFromPipelineByPropertyName = true, ValueFromPipeline = true,
  121. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetWaitJobById)]
  122. public string JobId
  123. {
  124. get { return this.command.JobId; }
  125. set { this.command.JobId = value; }
  126. }
  127. /// <inheritdoc />
  128. [Parameter(Position = 1, Mandatory = true, HelpMessage = "The subscription id for the Azure subscription.",
  129. ParameterSetName = AzureHdInsightPowerShellConstants.ParameterSetJobHistoryByNameWithSpecificSubscriptionCredentials)]
  130. [Alias(AzureHdInsightPowerShellConstants.AliasSub)]
  131. public string Subscription
  132. {
  133. get { return this.command.Subscription; }
  134. set { this.command.Subscription = value; }
  135. }
  136. /// <inheritdoc />
  137. [Parameter(Mandatory = false, HelpMessage = "The number of seconds to wait for completion, before cancelling waiting.",
  138. ValueFromPipeline = true)]
  139. public double WaitTimeoutInSeconds
  140. {
  141. get { return this.command.WaitTimeoutInSeconds; }
  142. set { this.command.WaitTimeoutInSeconds = value; }
  143. }
  144. /// <inheritdoc />
  145. protected override void EndProcessing()
  146. {
  147. this.WriteWarning(string.Format(AzureHdInsightPowerShellConstants.AsmWarning, "Wait-AzureRmHDInsightJob"));
  148. try
  149. {
  150. this.command.Logger = this.Logger;
  151. this.command.CurrentSubscription = this.GetCurrentSubscription(this.Subscription, this.Certificate);
  152. Task task = this.command.EndProcessing();
  153. CancellationToken token = this.command.CancellationToken;
  154. while (!task.IsCompleted)
  155. {
  156. this.WriteDebugLog();
  157. task.Wait(1000, token);
  158. }
  159. if (task.IsFaulted)
  160. {
  161. throw new AggregateException(task.Exception);
  162. }
  163. foreach (AzureHDInsightJob output in this.command.Output)
  164. {
  165. this.WriteObject(output);
  166. }
  167. this.WriteDebugLog();
  168. }
  169. catch (AggregateException ex)
  170. {
  171. this.WriteObject(this.FormatException(ex));
  172. this.Logger.Log(Severity.Error, Verbosity.Normal, this.FormatException(ex));
  173. throw ex.InnerException;
  174. }
  175. this.WriteDebugLog();
  176. }
  177. /// <inheritdoc />
  178. protected override void ProcessRecord()
  179. {
  180. DateTime start = DateTime.Now;
  181. string msg = string.Format(CultureInfo.CurrentCulture, "Waiting for jobDetails Started : {0}", start.ToString(CultureInfo.CurrentCulture));
  182. this.Logger.Log(Severity.Informational, Verbosity.Detailed, msg);
  183. try
  184. {
  185. this.command.Logger = this.Logger;
  186. this.command.CurrentSubscription = this.GetCurrentSubscription(this.Subscription, this.Certificate);
  187. Task task = this.command.ProcessRecord();
  188. CancellationToken token = this.command.CancellationToken;
  189. while (!task.IsCompleted)
  190. {
  191. this.WriteDebugLog();
  192. if (this.command.JobDetailsStatus.IsNotNull())
  193. {
  194. msg = string.Format(CultureInfo.CurrentCulture, "Waiting for jobDetails : {0}", this.JobId);
  195. var record = new ProgressRecord(
  196. 0, msg, this.command.JobDetailsStatus.StatusCode.ToString() + " : " + this.command.JobDetailsStatus.PercentComplete);
  197. this.WriteProgress(record);
  198. }
  199. task.Wait(1000, token);
  200. }
  201. this.WriteDebugLog();
  202. }
  203. catch (Exception ex)
  204. {
  205. Type type = ex.GetType();
  206. this.Logger.Log(Severity.Error, Verbosity.Normal, this.FormatException(ex));
  207. this.WriteDebugLog();
  208. if (type == typeof(AggregateException) || type == typeof(TargetInvocationException) || type == typeof(TaskCanceledException))
  209. {
  210. ex.Rethrow();
  211. }
  212. else
  213. {
  214. throw;
  215. }
  216. }
  217. msg = string.Format(CultureInfo.CurrentCulture, "Waiting for jobDetails Stopped : {0}", DateTime.Now.ToString(CultureInfo.CurrentCulture));
  218. this.Logger.Log(Severity.Informational, Verbosity.Detailed, msg);
  219. msg = string.Format(
  220. CultureInfo.CurrentCulture,
  221. "Waiting for jobDetails Executed for {0} minutes",
  222. (DateTime.Now - start).TotalMinutes.ToString(CultureInfo.CurrentCulture));
  223. this.Logger.Log(Severity.Informational, Verbosity.Detailed, msg);
  224. this.WriteDebugLog();
  225. }
  226. /// <inheritdoc />
  227. protected override void StopProcessing()
  228. {
  229. this.command.Cancel();
  230. }
  231. }
  232. }