// This function executes the activity.
        // It is highly recommended to not to block the execution of this function while executing the activity.
        // All the information should be logged into the queue and the function should be returned.
        // In a separate thread or on a separate machine, read the data from the queue and execute the activity.
        // Once the activity action is completed the bookmark should be resumed.
        // Design the activity controller to hanlde multiple activities from one workflowflow.
        public override void StartResumablePSCommand(Guid jobInstanceId, Bookmark bookmark, PowerShell command, PowerShellStreams<PSObject, PSObject> streams, PSActivityEnvironment environment, PSActivity activityInstance)
        {
            ActivityActionData data = new ActivityActionData();
            data.jobInstanceId = jobInstanceId;
            data.bookmark = bookmark;
            data.command = command;
            data.streams = streams;
            data.environment = environment;

            // Add the request to the queue.
            ActivityActionsQueue.TryAdd(jobInstanceId, data);

            // Return the fucntion and allow the workfow do other work in parallel.
            // There should be a servicing thead which gets the data from the queue and perform the action.
            // To keep this sample simple, the worker thread calls the servicing function.
            ThreadPool.QueueUserWorkItem(ServiceRequests, jobInstanceId);
        }
Example #2
0
        /// <summary>
        /// RemoteActivityResumeRequired
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="duringResumeBookmark"></param>
        /// <returns></returns>
        internal bool RemoteActivityResumeRequired(PSActivity activity, bool duringResumeBookmark)
        {
            bool activityResumeRequired = false;
            if (activity is PSRemotingActivity)
            {
                lock (_syncObject)
                {
                    if (_remoteRunspaceIdCollection.ContainsKey(activity.Id))
                    {
                        // if all commands are completed remove the activity instance id's entry from  remoteRunspaceCollection
                        // otherwise restart the activity execution to reconnect to managed nodes.
                        Dictionary<int, Tuple<object, string>> commandRunspaceIdCollection;
                        _remoteRunspaceIdCollection.TryGetValue(activity.Id, out commandRunspaceIdCollection);
                        if (commandRunspaceIdCollection != null)
                        {
                            if (commandRunspaceIdCollection.Any(o => o.Value.Item1 is Guid))
                            {
                                activityResumeRequired = true;
                            }
                            else if (commandRunspaceIdCollection.Any(o => (o.Value.Item1 is string && o.Value.Item1.ToString().Equals("notstarted"))))
                            {
                                activityResumeRequired = true;
                            }
                        }

                        // This method is first time called from OnResumeBookmark, if no resume required it's entry
                        // needs to be removed fromt the collection as activity execution has finished.
                        // When an activity entry is removed, remoteActivityState will be persisted 
                        // as part of whole workflow application persistence at the end of activity completion
                        if (activityResumeRequired == false)
                            _remoteRunspaceIdCollection.Remove(activity.Id);
                    }
                    else if (duringResumeBookmark)
                    {
                        // Activity needs to be restarted/resumed if the process got crashed/terminated just after activity is bookmarked and 
                        // in this case there will be no entry for this activity id in _remoteRunspaceIdCollection
                        activityResumeRequired = true;
                    }
                }
            }

            _tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "RemoteActivityState.RemoteActivityResumeRequired returning activityResumeRequired: {0}", activityResumeRequired));
            return activityResumeRequired;
        }
		public virtual void StartResumablePSCommand(Guid jobInstanceId, Bookmark bookmark, System.Management.Automation.PowerShell command, PowerShellStreams<PSObject, PSObject> streams, PSActivityEnvironment environment, PSActivity activityInstance)
		{
			throw new NotImplementedException();
		}