Esempio n. 1
0
        /// <summary>
        /// End for obtaining a runspace for the specified connection info
        /// </summary>
        /// <param name="asyncResult">async result to end on</param>
        /// <returns>remote runspace to invoke commands on</returns>
        public override Runspace EndGetRunspace(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            LocalRunspaceAsyncResult result = asyncResult as LocalRunspaceAsyncResult;

            if (result == null)
            {
                throw new ArgumentException(Resources.InvalidAsyncResultSpecified, "asyncResult");
            }

            // this will throw an exeption when a runspace is not successfully
            // available
            result.EndInvoke();

            Debug.Assert(result.Runspace != null, "EndInvoke() should throw an exception if runspace is null");

            _tracer.WriteMessage("LocalRunspaceProvider: Request serviced and runspace returned");
            Runspace runspace = result.Runspace;

            Debug.Assert(runspace.RunspaceStateInfo.State == RunspaceState.Opened, "Only opened runspace should be returned");

            return(runspace);
        }
        private void ServiceRequests(object state)
        {
            LocalRunspaceAsyncResult localRunspaceAsyncResult = null;
            bool     flag;
            Runspace runspace;

            lock (this._runspaceCache.TimerServicingSyncObject)
            {
                Runspace runspace1 = this.AssignRunspaceIfPossible();
                while (runspace1 != null && this._requests.TryDequeue(out localRunspaceAsyncResult))
                {
                    localRunspaceAsyncResult.Runspace = runspace1;
                    flag = true;
                    this.AddToPendingCallbacks(localRunspaceAsyncResult);
                    if (this._runspaceCache.Cache.Count < this._maxRunspaces)
                    {
                        runspace = this.AssignRunspaceIfPossible();
                    }
                    else
                    {
                        runspace = null;
                    }
                    runspace1 = runspace;
                }
                if (!flag && runspace1 != null)
                {
                    this.ReleaseRunspace(runspace1);
                }
            }
            Interlocked.CompareExchange(ref this._isServicing, 0, 1);
        }
        private void ServiceCallbacks(object state)
        {
            LocalRunspaceAsyncResult localRunspaceAsyncResult = null;

            while (this._callbacks.TryDequeue(out localRunspaceAsyncResult))
            {
                localRunspaceAsyncResult.SetAsCompleted(null);
            }
            Interlocked.CompareExchange(ref this._isServicingCallbacks, 0, 1);
        }
Esempio n. 4
0
        private void AddToPendingCallbacks(LocalRunspaceAsyncResult asyncResult)
        {
            _callbacks.Enqueue(asyncResult);

            if (Interlocked.CompareExchange(ref _isServicingCallbacks, Servicing, NotServicing) != NotServicing)
            {
                return;
            }

            TraceThreadPoolInfo("Callback thread");
            ThreadPool.QueueUserWorkItem(ServiceCallbacks);
        }
 private void AddToPendingCallbacks(LocalRunspaceAsyncResult asyncResult)
 {
     this._callbacks.Enqueue(asyncResult);
     if (Interlocked.CompareExchange(ref this._isServicingCallbacks, 1, 0) == 0)
     {
         this.TraceThreadPoolInfo("Callback thread");
         ThreadPool.QueueUserWorkItem(new WaitCallback(this.ServiceCallbacks));
         return;
     }
     else
     {
         return;
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Begin for obtaining a runspace for the specified ConnectionInfo
        /// </summary>
        /// <param name="connectionInfo">connection info to be used for remote connections</param>
        /// <param name="retryCount">number of times to retry</param>
        /// <param name="callback">optional user defined callback</param>
        /// <param name="state">optional user specified state</param>
        /// <param name="retryInterval">time in milliseconds before the next retry has to be attempted</param>
        /// <returns>async result</returns>
        public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
        {
            if (connectionInfo != null)
            {
                throw new InvalidOperationException();
            }

            LocalRunspaceAsyncResult asyncResult = new LocalRunspaceAsyncResult(state, callback, Guid.Empty);

            // Get the source language mode from the activity arguments if available and pass to runspace fetching.
            PSLanguageMode?      sourceLanguageMode = null;
            RunCommandsArguments args = state as RunCommandsArguments;

            if (args != null)
            {
                PSWorkflowRuntime wfRuntime = args.WorkflowHost as PSWorkflowRuntime;
                if (wfRuntime != null)
                {
                    PSWorkflowJob wfJob = wfRuntime.JobManager.GetJob(args.PSActivityContext.JobInstanceId);
                    if (wfJob != null)
                    {
                        sourceLanguageMode = wfJob.SourceLanguageMode;
                    }
                }
            }

            Runspace runspace = AssignRunspaceIfPossible(sourceLanguageMode);

            if (runspace != null)
            {
                asyncResult.Runspace = runspace;
                asyncResult.CompletedSynchronously = true;
                asyncResult.SetAsCompleted(null);
            }
            else
            {
                // queue the request
                _requests.Enqueue(asyncResult);
                CheckAndStartRequestServicingThread();
            }

            return(asyncResult);
        }
 public override Runspace EndGetRunspace(IAsyncResult asyncResult)
 {
     if (asyncResult != null)
     {
         LocalRunspaceAsyncResult localRunspaceAsyncResult = asyncResult as LocalRunspaceAsyncResult;
         if (localRunspaceAsyncResult != null)
         {
             localRunspaceAsyncResult.EndInvoke();
             this._tracer.WriteMessage("LocalRunspaceProvider: Request serviced and runspace returned");
             Runspace runspace = localRunspaceAsyncResult.Runspace;
             return(runspace);
         }
         else
         {
             throw new ArgumentException(Resources.InvalidAsyncResultSpecified, "asyncResult");
         }
     }
     else
     {
         throw new ArgumentNullException("asyncResult");
     }
 }
 public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
 {
     if (connectionInfo == null)
     {
         LocalRunspaceAsyncResult localRunspaceAsyncResult = new LocalRunspaceAsyncResult(state, callback, Guid.Empty);
         Runspace runspace = this.AssignRunspaceIfPossible();
         if (runspace == null)
         {
             this._requests.Enqueue(localRunspaceAsyncResult);
             this.CheckAndStartRequestServicingThread();
         }
         else
         {
             localRunspaceAsyncResult.Runspace = runspace;
             localRunspaceAsyncResult.CompletedSynchronously = true;
             localRunspaceAsyncResult.SetAsCompleted(null);
         }
         return(localRunspaceAsyncResult);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Esempio n. 9
0
		private void AddToPendingCallbacks(LocalRunspaceAsyncResult asyncResult)
		{
			this._callbacks.Enqueue(asyncResult);
			if (Interlocked.CompareExchange(ref this._isServicingCallbacks, 1, 0) == 0)
			{
				this.TraceThreadPoolInfo("Callback thread");
				ThreadPool.QueueUserWorkItem(new WaitCallback(this.ServiceCallbacks));
				return;
			}
			else
			{
				return;
			}
		}
Esempio n. 10
0
		public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
		{
			if (connectionInfo == null)
			{
				LocalRunspaceAsyncResult localRunspaceAsyncResult = new LocalRunspaceAsyncResult(state, callback, Guid.Empty);
				Runspace runspace = this.AssignRunspaceIfPossible();
				if (runspace == null)
				{
					this._requests.Enqueue(localRunspaceAsyncResult);
					this.CheckAndStartRequestServicingThread();
				}
				else
				{
					localRunspaceAsyncResult.Runspace = runspace;
					localRunspaceAsyncResult.CompletedSynchronously = true;
					localRunspaceAsyncResult.SetAsCompleted(null);
				}
				return localRunspaceAsyncResult;
			}
			else
			{
				throw new InvalidOperationException();
			}
		}
Esempio n. 11
0
        /// <summary>
        /// Begin for obtaining a runspace for the specified ConnectionInfo
        /// </summary>
        /// <param name="connectionInfo">connection info to be used for remote connections</param>
        /// <param name="retryCount">number of times to retry</param>
        /// <param name="callback">optional user defined callback</param>
        /// <param name="state">optional user specified state</param>
        /// <param name="retryInterval">time in milliseconds before the next retry has to be attempted</param>
        /// <returns>async result</returns>
        public override IAsyncResult BeginGetRunspace(WSManConnectionInfo connectionInfo, uint retryCount, uint retryInterval, AsyncCallback callback, object state)
        {
            if (connectionInfo != null)
            {
                throw new InvalidOperationException();
            }

            LocalRunspaceAsyncResult asyncResult = new LocalRunspaceAsyncResult(state, callback, Guid.Empty);

            // Get the source language mode from the activity arguments if available and pass to runspace fetching.
            PSLanguageMode? sourceLanguageMode = null;
            RunCommandsArguments args = state as RunCommandsArguments;
            if (args != null)
            {
                PSWorkflowRuntime wfRuntime = args.WorkflowHost as PSWorkflowRuntime;
                if (wfRuntime != null)
                {
                    PSWorkflowJob wfJob = wfRuntime.JobManager.GetJob(args.PSActivityContext.JobInstanceId);
                    if (wfJob != null)
                    {
                        sourceLanguageMode = wfJob.SourceLanguageMode;
                    }
                }
            }

            Runspace runspace = AssignRunspaceIfPossible(sourceLanguageMode);
            if (runspace != null)
            {
                asyncResult.Runspace = runspace;
                asyncResult.CompletedSynchronously = true;
                asyncResult.SetAsCompleted(null);
            }
            else
            {
                // queue the request
                _requests.Enqueue(asyncResult);
                CheckAndStartRequestServicingThread();
            }

            return asyncResult;
        }
Esempio n. 12
0
        private void AddToPendingCallbacks(LocalRunspaceAsyncResult asyncResult)
        {
            _callbacks.Enqueue(asyncResult);

            if (Interlocked.CompareExchange(ref _isServicingCallbacks, Servicing, NotServicing) != NotServicing) return;

            TraceThreadPoolInfo("Callback thread");
            ThreadPool.QueueUserWorkItem(ServiceCallbacks);
        }