Esempio n. 1
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();
                }
            }
        }
Esempio n. 2
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;
             }
         }
     }
 }
Esempio n. 3
0
 protected PSRemotingJob()
 {
     this.moreData         = true;
     this.hideComputerName = true;
     this.throttleManager  = new ThrottleManager();
     this._syncObject      = new object();
 }
Esempio n. 4
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.StopProcessing();
         this.operationsComplete.WaitOne();
         this.operationsComplete.Close();
         if (!this.asjob)
         {
             if (this.job != null)
             {
                 this.job.Dispose();
             }
             this.throttleManager.ThrottleComplete -= new EventHandler <EventArgs>(this.HandleThrottleComplete);
             this.throttleManager.Dispose();
             this.throttleManager = null;
         }
         if (this.clearInvokeCommandOnRunspace)
         {
             this.ClearInvokeCommandOnRunspaces();
         }
         this.input.Dispose();
         lock (this.jobSyncObject)
         {
             if (this.disconnectComplete != null)
             {
                 this.disconnectComplete.Close();
                 this.disconnectComplete = null;
             }
         }
     }
 }
Esempio n. 5
0
        private void RetryFailedSessions()
        {
            EventHandler <EventArgs> eventHandler = null;

            using (ManualResetEvent manualResetEvent = new ManualResetEvent(false))
            {
                Collection <PSSession>    pSSessions         = new Collection <PSSession>();
                List <IThrottleOperation> throttleOperations = new List <IThrottleOperation>();
                this.retryThrottleManager.ThrottleLimit = this.ThrottleLimit;
                ThrottleManager throttleManager = this.retryThrottleManager;
                if (eventHandler == null)
                {
                    eventHandler = (object sender, EventArgs eventArgs) => manualResetEvent.Set();
                }
                throttleManager.ThrottleComplete += eventHandler;
                foreach (PSSession failedSession in this.failedSessions)
                {
                    throttleOperations.Add(new ConnectPSSessionCommand.ConnectRunspaceOperation(failedSession, this.stream, base.Host, new QueryRunspaces(), pSSessions));
                }
                this.retryThrottleManager.SubmitOperations(throttleOperations);
                this.retryThrottleManager.EndSubmitOperations();
                manualResetEvent.WaitOne();
                foreach (PSSession pSSession in pSSessions)
                {
                    base.RunspaceRepository.AddOrReplace(pSSession);
                }
            }
        }
Esempio n. 6
0
        internal PSWmiChildJob(Cmdlet cmds, string computerName, ThrottleManager throttleManager, int count) : base(null, null)
        {
            this.syncObject            = new object();
            this.statusMessage         = "test";
            base.UsesResultsCollection = true;
            this.computerName          = computerName;
            this.throttleManager       = throttleManager;
            this.wmiSinkArray          = new ArrayList();
            ManagementOperationObserver managementOperationObserver = new ManagementOperationObserver();

            this.wmiSinkArray.Add(managementOperationObserver);
            PSWmiChildJob pSWmiChildJob = this;

            pSWmiChildJob.sinkCompleted              = pSWmiChildJob.sinkCompleted + count;
            managementOperationObserver.ObjectReady += new ObjectReadyEventHandler(this.NewObject);
            managementOperationObserver.Completed   += new CompletedEventHandler(this.JobDone);
            this.helper = new WmiAsyncCmdletHelper(this, cmds, computerName, managementOperationObserver, count);
            this.helper.WmiOperationState += new EventHandler <WmiJobStateEventArgs>(this.HandleWMIState);
            this.helper.ShutdownComplete  += new EventHandler <EventArgs>(this.JobDoneForWin32Shutdown);
            base.SetJobState(JobState.NotStarted);
            IThrottleOperation throttleOperation = this.helper;

            throttleOperation.OperationComplete += new EventHandler <OperationStateEventArgs>(this.HandleOperationComplete);
            throttleManager.ThrottleComplete    += new EventHandler <EventArgs>(this.HandleThrottleComplete);
            throttleManager.AddOperation(throttleOperation);
        }
Esempio n. 7
0
 public ConnectPSSessionCommand()
 {
     this.allSessions          = new Collection <PSSession>();
     this.throttleManager      = new ThrottleManager();
     this.operationsComplete   = new ManualResetEvent(true);
     this.queryRunspaces       = new QueryRunspaces();
     this.stream               = new ObjectStream();
     this.retryThrottleManager = new ThrottleManager();
     this.failedSessions       = new Collection <PSSession>();
 }
        /// <summary>
        /// This method is called periodically to refresh the thottle policy.
        /// </summary>
        /// <remarks>
        /// Currently, this method does no updates to the policy. However, in the future, we'll update the throttle policy dynamically. These will require
        /// making async calls to Table Storage.
        /// </remarks>
        /// <returns>a task</returns>
        public Task <IPolicyRepository> RefreshingThottlePolicy()
        {
            // get policy object from cache
            var policy = this.PolicyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());

            // here where we will update the policy

            // apply policy updates
            ThrottleManager.UpdatePolicy(policy, this.PolicyRepository);

            return(Task.FromResult(this.PolicyRepository));
        }
Esempio n. 9
0
 internal PSWmiJob(Cmdlet cmds, string[] computerName, int throttleLimit, string command, int count) : base(command, null)
 {
     this.throttleManager = new ThrottleManager();
     this.syncObject      = new object();
     base.PSJobTypeName   = "WmiJob";
     this.throttleManager.ThrottleLimit = throttleLimit;
     for (int i = 0; i < (int)computerName.Length; i++)
     {
         PSWmiChildJob pSWmiChildJob = new PSWmiChildJob(cmds, computerName[i], this.throttleManager, count);
         pSWmiChildJob.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleChildJobStateChanged);
         pSWmiChildJob.JobUnblocked += new EventHandler(this.HandleJobUnblocked);
         base.ChildJobs.Add(pSWmiChildJob);
     }
     this.CommonInit(throttleLimit);
 }
Esempio n. 10
0
 internal PSRemotingJob(string[] computerNames, List <IThrottleOperation> computerNameHelpers, string remoteCommand, int throttleLimit, string name) : base(remoteCommand, name)
 {
     this.moreData         = true;
     this.hideComputerName = true;
     this.throttleManager  = new ThrottleManager();
     this._syncObject      = new object();
     foreach (ExecutionCmdletHelperComputerName name2 in computerNameHelpers)
     {
         PSRemotingChildJob item = new PSRemotingChildJob(remoteCommand, name2, this.throttleManager);
         item.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleChildJobStateChanged);
         item.JobUnblocked += new EventHandler(this.HandleJobUnblocked);
         base.ChildJobs.Add(item);
     }
     this.CommonInit(throttleLimit, computerNameHelpers);
 }
Esempio n. 11
0
 internal PSRemotingJob(PSSession[] remoteRunspaceInfos, List <IThrottleOperation> runspaceHelpers, string remoteCommand, int throttleLimit, string name) : base(remoteCommand, name)
 {
     this.moreData         = true;
     this.hideComputerName = true;
     this.throttleManager  = new ThrottleManager();
     this._syncObject      = new object();
     for (int i = 0; i < remoteRunspaceInfos.Length; i++)
     {
         ExecutionCmdletHelperRunspace helper = (ExecutionCmdletHelperRunspace)runspaceHelpers[i];
         PSRemotingChildJob            item   = new PSRemotingChildJob(remoteCommand, helper, this.throttleManager);
         item.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleChildJobStateChanged);
         item.JobUnblocked += new EventHandler(this.HandleJobUnblocked);
         base.ChildJobs.Add(item);
     }
     this.CommonInit(throttleLimit, runspaceHelpers);
 }
Esempio n. 12
0
        /// <summary>
        /// After a decouple or part explosion, it's possible for this vessel to
        /// still have an assigned flight control parameter that is coming from
        /// a kOS core that is no longer on this vessel but is instead on the newly
        /// branched vessel we left behind.  If so, that parameter needs to be
        /// removed from this vessel.  The new kOSVesselModule will take care of
        /// making a new parameter on the new vessel, but this kOSVesselModule needs
        /// to detach it from this one.
        /// </summary>
        private void ResetPhysicallyDetachedParameters()
        {
            List <string> removeKeys = new List <string>();

            foreach (string key in flightControlParameters.Keys)
            {
                IFlightControlParameter p = flightControlParameters[key];
                if (p.GetShared() != null && p.GetShared().Vessel != null && Vessel != null &&
                    p.GetShared().Vessel.id != Vessel.id)
                {
                    removeKeys.Add(key);
                }
            }
            foreach (string key in removeKeys)
            {
                SafeHouse.Logger.SuperVerbose(string.Format(
                                                  "kOSVesselModule: re-defaulting parameter \"{0}\" because it's on a detached part of the vessel.", key));
                RemoveFlightControlParameter(key);
                IFlightControlParameter p = null;
                if (key.Equals("steering"))
                {
                    p = new SteeringManager(Vessel);
                }
                else if (key.Equals("throttle"))
                {
                    p = new ThrottleManager(Vessel);
                }
                else if (key.Equals("wheelsteering"))
                {
                    p = new WheelSteeringManager(Vessel);
                }
                else if (key.Equals("wheelthrottle"))
                {
                    p = new WheelThrottleManager(Vessel);
                }
                else if (key.Equals("flightcontrol"))
                {
                    p = new FlightControl(Vessel);
                }

                if (p != null)
                {
                    AddFlightControlParameter(key, p);
                }
            }
            foundWrongVesselAutopilot = false;
        }
Esempio n. 13
0
        internal PSRemotingChildJob(string remoteCommand, ExecutionCmdletHelper helper, ThrottleManager throttleManager) : base(remoteCommand)
        {
            this.hideComputerName      = true;
            this.SyncObject            = new object();
            base.UsesResultsCollection = true;
            this.helper          = helper;
            this.remoteRunspace  = helper.Pipeline.Runspace;
            this.remotePipeline  = helper.Pipeline as RemotePipeline;
            this.throttleManager = throttleManager;
            RemoteRunspace remoteRunspace = this.remoteRunspace as RemoteRunspace;

            if ((remoteRunspace != null) && (remoteRunspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen))
            {
                remoteRunspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
            }
            this.AggregateResultsFromHelper(helper);
            this.RegisterThrottleComplete(throttleManager);
        }
Esempio n. 14
0
 internal PSRemotingJob(List <IThrottleOperation> helpers, int throttleLimit, string name, bool aggregateResults) : base(string.Empty, name)
 {
     this.moreData         = true;
     this.hideComputerName = true;
     this.throttleManager  = new ThrottleManager();
     this._syncObject      = new object();
     foreach (ExecutionCmdletHelper helper in helpers)
     {
         PSRemotingChildJob item = new PSRemotingChildJob(helper, this.throttleManager, aggregateResults);
         item.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleChildJobStateChanged);
         item.JobUnblocked += new EventHandler(this.HandleJobUnblocked);
         base.ChildJobs.Add(item);
     }
     base.CloseAllStreams();
     base.SetJobState(JobState.Disconnected);
     this.throttleManager.ThrottleLimit = throttleLimit;
     this.throttleManager.SubmitOperations(helpers);
     this.throttleManager.EndSubmitOperations();
 }
Esempio n. 15
0
 internal PSRemotingChildJob(
     string remoteCommand,
     ExecutionCmdletHelper helper,
     ThrottleManager throttleManager)
     : base(remoteCommand)
 {
     using (PSRemotingChildJob.tracer.TraceConstructor((object)this))
     {
         this.helper          = helper;
         this.remoteRunspace  = helper.Pipeline.Runspace;
         this.remotePipeline  = helper.Pipeline as RemotePipeline;
         this.throttleManager = throttleManager;
         if (this.remoteRunspace is RemoteRunspace remoteRunspace && remoteRunspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen)
         {
             remoteRunspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
         }
         this.AggregateResultsFromHelper(helper);
         this.RegisterThrottleComplete(throttleManager);
     }
 }
 internal PSInvokeExpressionSyncJob(List <IThrottleOperation> operations, ThrottleManager throttleManager)
 {
     base.UsesResultsCollection = true;
     base.Results.AddRef();
     this.throttleManager = throttleManager;
     base.RegisterThrottleComplete(this.throttleManager);
     foreach (IThrottleOperation operation in operations)
     {
         ExecutionCmdletHelper item     = operation as ExecutionCmdletHelper;
         RemoteRunspace        runspace = item.Pipeline.Runspace as RemoteRunspace;
         if ((runspace != null) && (runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen))
         {
             runspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(this.HandleURIDirectionReported);
             runspace.StateChanged           += new EventHandler <RunspaceStateEventArgs>(this.HandleRunspaceStateChanged);
         }
         this.helpers.Add(item);
         base.AggregateResultsFromHelper(item);
         RemotePipeline pipeline = item.Pipeline as RemotePipeline;
         this.powershells.Add(pipeline.PowerShell.InstanceId, pipeline.PowerShell);
     }
 }
Esempio n. 17
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);
            }
        }
Esempio n. 18
0
        public void UpdateRateLimits()
        {
            //init policy repo
            var policyRepository = new PolicyCacheRepository();

            //get policy object from cache
            var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());

            //update client rate limits
            policy.ClientRules["api-client-key-1"] =
                new RateLimits {
                PerMinute = 50, PerHour = 500
            };

            //add new client rate limits
            policy.ClientRules.Add("api-client-key-3",
                                   new RateLimits {
                PerMinute = 60, PerHour = 600
            });

            //apply policy updates
            ThrottleManager.UpdatePolicy(policy, policyRepository);
        }
Esempio n. 19
0
 protected void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.throttleManager.Dispose();
         this.operationsComplete.WaitOne();
         this.operationsComplete.Close();
         this.throttleManager.ThrottleComplete -= new EventHandler <EventArgs>(this.HandleThrottleComplete);
         this.throttleManager = null;
         foreach (RemoteRunspace runspace in this.toDispose)
         {
             runspace.Dispose();
         }
         foreach (List <IThrottleOperation> list in this.allOperations)
         {
             foreach (OpenRunspaceOperation operation in list)
             {
                 operation.Dispose();
             }
         }
         this.stream.Dispose();
     }
 }
Esempio n. 20
0
 internal PSInvokeExpressionSyncJob(
     List <IThrottleOperation> operations,
     ThrottleManager throttleManager)
 {
     this.Results.AddRef();
     this.throttleManager = throttleManager;
     this.RegisterThrottleComplete(this.throttleManager);
     foreach (IThrottleOperation operation in operations)
     {
         ExecutionCmdletHelper helper = operation as ExecutionCmdletHelper;
         if (helper.Pipeline.Runspace is RemoteRunspace runspace && runspace.RunspaceStateInfo.State == RunspaceState.BeforeOpen)
         {
             runspace.URIRedirectionReported += new EventHandler <RemoteDataEventArgs <Uri> >(((PSRemotingChildJob)this).HandleURIDirectionReported);
             runspace.StateChanged           += new EventHandler <RunspaceStateEventArgs>(this.HandleRunspaceStateChanged);
         }
         this.helpers.Add(helper);
         this.AggregateResultsFromHelper(helper);
         RemotePipeline pipeline = helper.Pipeline as RemotePipeline;
         this.powershells.Add(pipeline.PowerShell.InstanceId, pipeline.PowerShell);
     }
     throttleManager.SubmitOperations(operations);
     throttleManager.EndSubmitOperations();
 }
Esempio n. 21
0
        protected virtual void DoCleanupOnFinished()
        {
            bool flag = false;

            if (!this.cleanupDone)
            {
                lock (this.SyncObject)
                {
                    if (!this.cleanupDone)
                    {
                        this.cleanupDone = true;
                        flag             = true;
                    }
                }
            }
            if (flag)
            {
                this.StopAggregateResultsFromHelper(this.helper);
                this.helper.OperationComplete -= new EventHandler <OperationStateEventArgs>(this.HandleOperationComplete);
                this.UnregisterThrottleComplete(this.throttleManager);
                this.throttleManager = null;
            }
        }
Esempio n. 22
0
        internal PSRemotingChildJob(ExecutionCmdletHelper helper, ThrottleManager throttleManager, bool aggregateResults = false)
        {
            this.hideComputerName      = true;
            this.SyncObject            = new object();
            base.UsesResultsCollection = true;
            this.helper          = helper;
            this.remotePipeline  = helper.Pipeline as RemotePipeline;
            this.remoteRunspace  = helper.Pipeline.Runspace;
            this.throttleManager = throttleManager;
            if (aggregateResults)
            {
                this.AggregateResultsFromHelper(helper);
            }
            else
            {
                this.remotePipeline.StateChanged     += new EventHandler <PipelineStateEventArgs>(this.HandlePipelineStateChanged);
                this.remotePipeline.Output.DataReady += new EventHandler(this.HandleOutputReady);
                this.remotePipeline.Error.DataReady  += new EventHandler(this.HandleErrorReady);
            }
            IThrottleOperation operation = helper;

            operation.OperationComplete += new EventHandler <OperationStateEventArgs>(this.HandleOperationComplete);
            base.SetJobState(JobState.Disconnected, null);
        }
Esempio n. 23
0
 protected void UnregisterThrottleComplete(ThrottleManager throttleManager)
 {
     throttleManager.ThrottleComplete -= new EventHandler <EventArgs>(this.HandleThrottleComplete);
 }