File size: 9,463 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Threading;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This cmdlet resumes the jobs that are Job2. Errors are added for each Job that is not Job2.
/// </summary>
#if !CORECLR
[SuppressMessage("Microsoft.PowerShell", "PS1012:CallShouldProcessOnlyIfDeclaringSupport")]
[Cmdlet(VerbsLifecycle.Resume, "Job", SupportsShouldProcess = true, DefaultParameterSetName = JobCmdletBase.SessionIdParameterSet,
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=210611")]
#endif
[OutputType(typeof(Job))]
public class ResumeJobCommand : JobCmdletBase, IDisposable
{
#region Parameters
/// <summary>
/// Specifies the Jobs objects which need to be
/// suspended.
/// </summary>
[Parameter(Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = JobParameterSet)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public Job[] Job
{
get
{
return _jobs;
}
set
{
_jobs = value;
}
}
private Job[] _jobs;
/// <summary>
/// </summary>
public override string[] Command
{
get
{
return null;
}
}
/// <summary>
/// Specifies whether to delay returning from the cmdlet until all jobs reach a running state.
/// This could take significant time due to workflow throttling.
/// </summary>
[Parameter(ParameterSetName = ParameterAttribute.AllParameterSets)]
public SwitchParameter Wait { get; set; }
#endregion Parameters
#region Overrides
/// <summary>
/// Resume the Job.
/// </summary>
protected override void ProcessRecord()
{
// List of jobs to resume
List<Job> jobsToResume = null;
switch (ParameterSetName)
{
case NameParameterSet:
{
jobsToResume = FindJobsMatchingByName(true, false, true, false);
}
break;
case InstanceIdParameterSet:
{
jobsToResume = FindJobsMatchingByInstanceId(true, false, true, false);
}
break;
case SessionIdParameterSet:
{
jobsToResume = FindJobsMatchingBySessionId(true, false, true, false);
}
break;
case StateParameterSet:
{
jobsToResume = FindJobsMatchingByState(false);
}
break;
case FilterParameterSet:
{
jobsToResume = FindJobsMatchingByFilter(false);
}
break;
default:
{
jobsToResume = CopyJobsToList(_jobs, false, false);
}
break;
}
_allJobsToResume.AddRange(jobsToResume);
// Blue: 151804 When resuming a single suspended workflow job, Resume-job cmdlet doesn't wait for the job to be in running state
// Setting Wait to true so that this cmdlet will wait for the running job state.
if (_allJobsToResume.Count == 1)
Wait = true;
foreach (Job job in jobsToResume)
{
var job2 = job as Job2;
// If the job is not Job2, the resume operation is not supported.
if (job2 == null)
{
WriteError(new ErrorRecord(PSTraceSource.NewNotSupportedException(RemotingErrorIdStrings.JobResumeNotSupported, job.Id), "Job2OperationNotSupportedOnJob", ErrorCategory.InvalidType, (object)job));
continue;
}
string targetString = PSRemotingErrorInvariants.FormatResourceString(RemotingErrorIdStrings.RemovePSJobWhatIfTarget, job.Command, job.Id);
if (ShouldProcess(targetString, VerbsLifecycle.Resume))
{
_cleanUpActions.Add(job2, HandleResumeJobCompleted);
job2.ResumeJobCompleted += HandleResumeJobCompleted;
lock (_syncObject)
{
if (!_pendingJobs.Contains(job2.InstanceId))
{
_pendingJobs.Add(job2.InstanceId);
}
}
job2.ResumeJobAsync();
}
}
}
private bool _warnInvalidState = false;
private readonly HashSet<Guid> _pendingJobs = new HashSet<Guid>();
private readonly ManualResetEvent _waitForJobs = new ManualResetEvent(false);
private readonly Dictionary<Job2, EventHandler<AsyncCompletedEventArgs>> _cleanUpActions =
new Dictionary<Job2, EventHandler<AsyncCompletedEventArgs>>();
private readonly List<ErrorRecord> _errorsToWrite = new List<ErrorRecord>();
private readonly List<Job> _allJobsToResume = new List<Job>();
private readonly object _syncObject = new object();
private bool _needToCheckForWaitingJobs;
private void HandleResumeJobCompleted(object sender, AsyncCompletedEventArgs eventArgs)
{
Job job = sender as Job;
if (eventArgs.Error != null && eventArgs.Error is InvalidJobStateException)
{
_warnInvalidState = true;
}
var parentJob = job as ContainerParentJob;
if (parentJob != null && parentJob.ExecutionError.Count > 0)
{
foreach (
var e in
parentJob.ExecutionError.Where(static e => e.FullyQualifiedErrorId == "ContainerParentJobResumeAsyncError")
)
{
if (e.Exception is InvalidJobStateException)
{
// if any errors were invalid job state exceptions, warn the user.
// This is to support Get-Job | Resume-Job scenarios when many jobs
// are Completed, etc.
_warnInvalidState = true;
}
else
{
_errorsToWrite.Add(e);
}
}
parentJob.ExecutionError.Clear();
}
bool releaseWait = false;
lock (_syncObject)
{
if (_pendingJobs.Contains(job.InstanceId))
{
_pendingJobs.Remove(job.InstanceId);
}
if (_needToCheckForWaitingJobs && _pendingJobs.Count == 0)
releaseWait = true;
}
// end processing has been called
// set waithandle if this is the last one
if (releaseWait)
_waitForJobs.Set();
}
/// <summary>
/// End Processing.
/// </summary>
protected override void EndProcessing()
{
bool jobsPending = false;
lock (_syncObject)
{
_needToCheckForWaitingJobs = true;
if (_pendingJobs.Count > 0)
{
jobsPending = true;
}
}
if (Wait && jobsPending)
{
_waitForJobs.WaitOne();
}
if (_warnInvalidState)
{
WriteWarning(RemotingErrorIdStrings.ResumeJobInvalidJobState);
}
foreach (var e in _errorsToWrite)
{
WriteError(e);
}
foreach (var j in _allJobsToResume)
{
WriteObject(j);
}
base.EndProcessing();
}
/// <summary>
/// </summary>
protected override void StopProcessing()
{
_waitForJobs.Set();
}
#endregion Overrides
#region Dispose
/// <summary>
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// </summary>
/// <param name="disposing"></param>
protected void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
foreach (var pair in _cleanUpActions)
{
pair.Key.ResumeJobCompleted -= pair.Value;
}
_waitForJobs.Dispose();
}
#endregion Dispose
}
}
|