Exemplo n.º 1
0
            private void AggregateJobResults(PSDataCollection <PSStreamObject> resultsCollection)
            {
                lock (_myLock)
                {
                    // try not to remove results from a job, unless it seems safe ...
                    if (_disposed || _stoppedMonitoringAllJobs || _aggregatedResults.IsAddingCompleted || _cancellationTokenSource.IsCancellationRequested)
                    {
                        return;
                    }
                }

                // ... and after removing the results via ReadAll, we have to make sure that we don't drop them ...
                foreach (var result in resultsCollection.ReadAll())
                {
                    bool successfullyAggregatedResult = false;
                    try
                    {
                        lock (_myLock)
                        {
                            // try not to remove results from a job, unless it seems safe ...
                            if (!(_disposed || _stoppedMonitoringAllJobs || _aggregatedResults.IsAddingCompleted || _cancellationTokenSource.IsCancellationRequested))
                            {
                                _aggregatedResults.Add(result, _cancellationTokenSource.Token);
                                successfullyAggregatedResult = true;
                            }
                        }
                    }
                    catch (Exception) // BlockingCollection.Add can throw undocumented exceptions - we cannot just catch InvalidOperationException
                    {
                    }

                    // ... so if _aggregatedResults is not accepting new results, we will store them in the throttling job
                    if (!successfullyAggregatedResult)
                    {
                        this.StopMonitoringJob(_throttlingJob);
                        try
                        {
                            _throttlingJob.Results.Add(result);
                        }
                        catch (InvalidOperationException)
                        {
                            Dbg.Assert(false, "ThrottlingJob.Results was already closed when trying to preserve results aggregated by ForwardingHelper");
                        }
                    }
                }
            }
Exemplo n.º 2
0
 private static EventHandler <DataAddedEventArgs> GetStreamForwarder <T>(Action <T> forwardingAction, bool swallowInvalidOperationExceptions = false)
 {
     return(delegate(object sender, DataAddedEventArgs eventArgs) {
         PSDataCollection <T> datas = (PSDataCollection <T>)sender;
         foreach (T local in datas.ReadAll())
         {
             try
             {
                 forwardingAction(local);
             }
             catch (InvalidOperationException)
             {
                 if (!swallowInvalidOperationExceptions)
                 {
                     throw;
                 }
             }
         }
     });
 }
Exemplo n.º 3
0
 private void AggregateJobResults(PSDataCollection <PSStreamObject> resultsCollection)
 {
     lock (this._myLock)
     {
         if ((this._disposed || this._stoppedMonitoringAllJobs) || (this._aggregatedResults.IsAddingCompleted || this._cancellationTokenSource.IsCancellationRequested))
         {
             return;
         }
     }
     foreach (PSStreamObject obj2 in resultsCollection.ReadAll())
     {
         bool flag = false;
         try
         {
             lock (this._myLock)
             {
                 if ((!this._disposed && !this._stoppedMonitoringAllJobs) && (!this._aggregatedResults.IsAddingCompleted && !this._cancellationTokenSource.IsCancellationRequested))
                 {
                     this._aggregatedResults.Add(obj2, this._cancellationTokenSource.Token);
                     flag = true;
                 }
             }
         }
         catch (Exception)
         {
         }
         if (!flag)
         {
             this.StopMonitoringJob(this._throttlingJob);
             try
             {
                 this._throttlingJob.Results.Add(obj2);
             }
             catch (InvalidOperationException)
             {
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Default constructor for creating ServerPowerShellDrivers
        /// </summary>
        /// <param name="powershell">decoded powershell object</param>
        /// <param name="extraPowerShell">extra pipeline to be run after <paramref name="powershell"/> completes</param>
        /// <param name="noInput">whether there is input for this powershell</param>
        /// <param name="clientPowerShellId">the client powershell id</param>
        /// <param name="clientRunspacePoolId">the client runspacepool id</param>
        /// <param name="runspacePoolDriver">runspace pool driver
        /// which is creating this powershell driver</param>
        /// <param name="apartmentState">apartment state for this powershell</param>
        /// <param name="hostInfo">host info using which the host for
        /// this powershell will be constructed</param>
        /// <param name="streamOptions">serialization options for the streams in this powershell</param>
        /// <param name="addToHistory">
        /// true if the command is to be added to history list of the runspace. false, otherwise.
        /// </param>
        /// <param name="rsToUse">
        /// If not null, this Runspace will be used to invoke Powershell.
        /// If null, the RunspacePool pointed by <paramref name="runspacePoolDriver"/> will be used.
        /// </param>
        /// <param name="output">
        /// If not null, this is used as another source of output sent to the client.
        /// </param>
        internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId,
                                        Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver,
                                        ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions,
                                        bool addToHistory, Runspace rsToUse, PSDataCollection <PSObject> output)
#endif
        {
            InstanceId          = clientPowerShellId;
            RunspacePoolId      = clientRunspacePoolId;
            RemoteStreamOptions = streamOptions;
#if !CORECLR // No ApartmentState In CoreCLR
            this.apartmentState = apartmentState;
#endif
            LocalPowerShell        = powershell;
            _extraPowerShell       = extraPowerShell;
            _localPowerShellOutput = new PSDataCollection <PSObject>();
            _noInput         = noInput;
            _addToHistory    = addToHistory;
            _psDriverInvoker = runspacePoolDriver;

            DataStructureHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, RemoteStreamOptions, LocalPowerShell);
            _remoteHost          = DataStructureHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost);

            if (!noInput)
            {
                InputCollection = new PSDataCollection <object>();
                InputCollection.ReleaseOnEnumeration = true;
                InputCollection.IdleEvent           += new EventHandler <EventArgs>(HandleIdleEvent);
            }

            RegisterPipelineOutputEventHandlers(_localPowerShellOutput);

            if (LocalPowerShell != null)
            {
                RegisterPowerShellEventHandlers(LocalPowerShell);
                _datasent[0] = false;
            }

            if (extraPowerShell != null)
            {
                RegisterPowerShellEventHandlers(extraPowerShell);
                _datasent[1] = false;
            }

            RegisterDataStructureHandlerEventHandlers(DataStructureHandler);

            // set the runspace pool and invoke this powershell
            if (rsToUse != null)
            {
                LocalPowerShell.Runspace = rsToUse;
                if (extraPowerShell != null)
                {
                    extraPowerShell.Runspace = rsToUse;
                }
            }
            else
            {
                LocalPowerShell.RunspacePool = runspacePoolDriver.RunspacePool;
                if (extraPowerShell != null)
                {
                    extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool;
                }
            }

            if (output != null)
            {
                output.DataAdded += (sender, args) =>
                {
                    if (_localPowerShellOutput.IsOpen)
                    {
                        var items = output.ReadAll();
                        foreach (var item in items)
                        {
                            _localPowerShellOutput.Add(item);
                        }
                    }
                };
            }
        }