Exemplo n.º 1
0
        /// <summary>
        /// Disconnects all disconnectable jobs listed in the JobRepository.
        /// </summary>
        private void StopOrDisconnectAllJobs()
        {
            if (JobRepository.Jobs.Count == 0) { return; }
            List<RemoteRunspace> disconnectRunspaces = new List<RemoteRunspace>();

            using (ManualResetEvent jobsStopCompleted = new ManualResetEvent(false))
            {
                ThrottleManager throttleManager = new ThrottleManager();
                throttleManager.ThrottleComplete += delegate (object sender, EventArgs e)
                {
                    jobsStopCompleted.Set();
                };

                foreach (Job job in this.JobRepository.Jobs)
                {
                    // Only stop or disconnect PowerShell jobs.
                    if (job is PSRemotingJob == false)
                    {
                        continue;
                    }

                    if (!job.CanDisconnect)
                    {
                        // If the job cannot be disconnected then add it to
                        // the stop list.
                        throttleManager.AddOperation(new StopJobOperationHelper(job));
                    }
                    else if (job.JobStateInfo.State == JobState.Running)
                    {
                        // Otherwise add disconnectable runspaces to list so that
                        // they can be disconnected.
                        IEnumerable<RemoteRunspace> jobRunspaces = job.GetRunspaces();
                        if (jobRunspaces != null)
                        {
                            disconnectRunspaces.AddRange(jobRunspaces);
                        }
                    }
                } // foreach job

                // Stop jobs.
                throttleManager.EndSubmitOperations();
                jobsStopCompleted.WaitOne();
            } // using jobsStopCompleted

            // Disconnect all disconnectable job runspaces found.
            CloseOrDisconnectAllRemoteRunspaces(() =>
                {
                    return disconnectRunspaces;
                });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Closes or disconnects all the remote runspaces passed in by the getRunspace
        /// function.  If a remote runspace supports disconnect then it will be disconnected 
        /// rather than closed.
        /// </summary>
        private void CloseOrDisconnectAllRemoteRunspaces(Func<List<RemoteRunspace>> getRunspaces)
        {
            List<RemoteRunspace> runspaces = getRunspaces();
            if (runspaces.Count == 0) { return; }

            // whether the close of all remoterunspaces completed
            using (ManualResetEvent remoteRunspaceCloseCompleted = new ManualResetEvent(false))
            {
                ThrottleManager throttleManager = new ThrottleManager();
                throttleManager.ThrottleComplete += delegate (object sender, EventArgs e)
                {
                    remoteRunspaceCloseCompleted.Set();
                };

                foreach (RemoteRunspace remoteRunspace in runspaces)
                {
                    IThrottleOperation operation = new CloseOrDisconnectRunspaceOperationHelper(remoteRunspace);
                    throttleManager.AddOperation(operation);
                }
                throttleManager.EndSubmitOperations();

                remoteRunspaceCloseCompleted.WaitOne();
            }
        }
Exemplo n.º 3
0
 private void StopOrDisconnectAllJobs()
 {
     List<RemoteRunspace> disconnectRunspaces;
     if (this.JobRepository.Jobs.Count != 0)
     {
         disconnectRunspaces = new List<RemoteRunspace>();
         EventHandler<EventArgs> handler = null;
         using (ManualResetEvent jobsStopCompleted = new ManualResetEvent(false))
         {
             ThrottleManager manager = new ThrottleManager();
             if (handler == null)
             {
                 handler = (sender, e) => jobsStopCompleted.Set();
             }
             manager.ThrottleComplete += handler;
             foreach (Job job in this.JobRepository.Jobs)
             {
                 if (job is PSRemotingJob)
                 {
                     if (!job.CanDisconnect)
                     {
                         manager.AddOperation(new StopJobOperationHelper(job));
                     }
                     else if (job.JobStateInfo.State == JobState.Running)
                     {
                         IEnumerable<RemoteRunspace> runspaces = job.GetRunspaces();
                         if (runspaces != null)
                         {
                             disconnectRunspaces.AddRange(runspaces);
                         }
                     }
                 }
             }
             manager.EndSubmitOperations();
             jobsStopCompleted.WaitOne();
         }
         this.CloseOrDisconnectAllRemoteRunspaces(() => disconnectRunspaces);
     }
 }
Exemplo n.º 4
0
 private void CloseOrDisconnectAllRemoteRunspaces(Func<List<RemoteRunspace>> getRunspaces)
 {
     List<RemoteRunspace> list = getRunspaces();
     if (list.Count != 0)
     {
         EventHandler<EventArgs> handler = null;
         using (ManualResetEvent remoteRunspaceCloseCompleted = new ManualResetEvent(false))
         {
             ThrottleManager manager = new ThrottleManager();
             if (handler == null)
             {
                 handler = (sender, e) => remoteRunspaceCloseCompleted.Set();
             }
             manager.ThrottleComplete += handler;
             foreach (RemoteRunspace runspace in list)
             {
                 IThrottleOperation operation = new CloseOrDisconnectRunspaceOperationHelper(runspace);
                 manager.AddOperation(operation);
             }
             manager.EndSubmitOperations();
             remoteRunspaceCloseCompleted.WaitOne();
         }
     }
 }
Exemplo n.º 5
0
 private void SubmitAndWaitForConnect(List<IThrottleOperation> connectJobOperations)
 {
     using (ThrottleManager manager = new ThrottleManager())
     {
         EventHandler<EventArgs> handler2 = null;
         using (ManualResetEvent connectResult = new ManualResetEvent(false))
         {
             if (handler2 == null)
             {
                 handler2 = (sender, eventArgs) => connectResult.Set();
             }
             EventHandler<EventArgs> handler = handler2;
             manager.ThrottleComplete += handler;
             try
             {
                 manager.ThrottleLimit = 0;
                 manager.SubmitOperations(connectJobOperations);
                 manager.EndSubmitOperations();
                 connectResult.WaitOne();
             }
             finally
             {
                 manager.ThrottleComplete -= handler;
             }
         }
     }
 }