public void Run()
            {
                Guid id;

                Guid.TryParse(DataTransferObject.ParentInstanceID, out id);
                ID          = DataTransferObject.ResourceID;
                ParentID    = DataTransferObject.ParentID;
                WorkspaceID = DataTransferObject.WorkspaceID;

                // handle queuing
                WaitForSlot();

                // abort the execution - no space at the inn
                if (!ExecutableServiceRepository.Instance.DoesQueueHaveSpace())
                {
                    _instance.Abort();
                }
                else
                {
                    ExecutableServiceRepository.Instance.Add(this);
                    // here is space at the inn ;)
                    var wfappUtils = new WfApplicationUtils();

                    if (DataTransferObject.IsDebugMode())
                    {
                        ErrorResultTO invokeErrors;
                        wfappUtils.DispatchDebugState(DataTransferObject, StateType.Start, AllErrors.HasErrors(), AllErrors.MakeDisplayReady(), out invokeErrors, null, true);
                        AllErrors.MergeErrors(invokeErrors);
                    }

                    _previousNumberOfSteps           = DataTransferObject.NumberOfSteps;
                    DataTransferObject.NumberOfSteps = 0;
                    _instance.Run();
                }
            }
        // ReSharper disable InconsistentNaming
        public void DebugDispatcher_Write_WhenRemoteInvokeItemsPresentAfterEndMessage_ExpectRemoteItemsDispatchedBeforeEndMessage()
        // ReSharper restore InconsistentNaming
        {
            //------------Setup for test--------------------------
            var workspaceID = Guid.NewGuid();
            var writer = new Mock<IDebugWriter>();
            writer.Setup(s => s.Write(It.IsAny<IDebugState>())).Verifiable();
            var writerList = new List<IDebugState>();
            // .Callback<ISqlBulkCopy, DataTable>((sqlBulkCopy, dataTable) => returnedDataTable = dataTable);
            // ReSharper disable ConvertClosureToMethodGroup
            writer.Setup(w => w.Write(It.IsAny<IDebugState>())).Callback<IDebugState>(ds =>
            {
                writerList.Add(ds);
            });
            // ReSharper restore ConvertClosureToMethodGroup

            DebugDispatcher.Instance.Add(workspaceID, writer.Object);

            var origResourceID = Guid.NewGuid();
            var server = Guid.NewGuid();

            var state2 = new Mock<IDebugState>();
            state2.Setup(s => s.WorkspaceID).Returns(workspaceID);
            state2.Setup(s => s.OriginatingResourceID).Returns(origResourceID);
            state2.Setup(s => s.Server).Returns(server.ToString);
            state2.Setup(s => s.StateType).Returns(StateType.Message);

            var state1 = new Mock<IDebugState>();
            state1.Setup(s => s.WorkspaceID).Returns(workspaceID);
            state1.Setup(s => s.OriginatingResourceID).Returns(origResourceID);
            state1.Setup(s => s.Server).Returns(server.ToString);
            state1.Setup(s => s.StateType).Returns(StateType.Message);

            var utils = new WfApplicationUtils();
            var dataObject = new Mock<IDSFDataObject>();
            dataObject.Setup(d => d.RemoteDebugItems).Returns(new List<IDebugState> { state1.Object, state2.Object });
            dataObject.Setup(d => d.IsDebugMode()).Returns(true);
            dataObject.Setup(d => d.WorkspaceID).Returns(workspaceID);
            dataObject.Setup(d => d.Environment).Returns(new ExecutionEnvironment());

            //------------Execute Test---------------------------

            ErrorResultTO errors;
            utils.DispatchDebugState(dataObject.Object, StateType.End, false, string.Empty, out errors);

            //------------Assert Results-------------------------

            // Write happens asynchronously on a separate thread
            Thread.Sleep(50);
            writer.Verify(s => s.Write(It.IsAny<IDebugState>()), Times.Exactly(3));

            Assert.AreEqual(3, writerList.Count);
            // Now ensure ordering is correct ;)
            Assert.AreEqual(state1.Object, writerList[0]);
            Assert.AreEqual(state2.Object, writerList[1]);
            // ensure end state last to dispatch ;)
            Assert.AreEqual(StateType.End, writerList[2].StateType);

        }
            public void Run()
            {
                Guid id;
                Guid.TryParse(DataTransferObject.ParentInstanceID, out id);
                ID = DataTransferObject.ResourceID;
                ParentID = DataTransferObject.ParentID;
                WorkspaceID = DataTransferObject.WorkspaceID;

                // handle queuing 
                WaitForSlot();

                // abort the execution - no space at the inn
                if(!ExecutableServiceRepository.Instance.DoesQueueHaveSpace())
                {
                    _instance.Abort();
                }
                else
                {
                    ExecutableServiceRepository.Instance.Add(this);
                    // here is space at the inn ;)
                    var wfappUtils = new WfApplicationUtils();

                    if (DataTransferObject.IsDebugMode())
                    {
                        ErrorResultTO invokeErrors;
                        wfappUtils.DispatchDebugState(DataTransferObject, StateType.Start, AllErrors.HasErrors(), AllErrors.MakeDisplayReady(), out invokeErrors, null, true);
                        AllErrors.MergeErrors(invokeErrors);
                    }
                    
                    _previousNumberOfSteps = DataTransferObject.NumberOfSteps;
                    DataTransferObject.NumberOfSteps = 0;
                    _instance.Run();
                }
            }
        /// <summary>
        /// Invokes the workflow impl.
        /// </summary>
        /// <param name="workflowActivity">The workflow activity.</param>
        /// <param name="dataTransferObject">The data transfer object.</param>
        /// <param name="executionExtensions">The execution extensions.</param>
        /// <param name="instanceId">The instance id.</param>
        /// <param name="workspace">The workspace.</param>
        /// <param name="bookmarkName">Name of the bookmark.</param>
        /// <param name="isDebug">if set to <c>true</c> [is debug].</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        private IDSFDataObject InvokeWorkflowImpl(Activity workflowActivity, IDSFDataObject dataTransferObject, IList<object> executionExtensions, Guid instanceId, IWorkspace workspace, string bookmarkName, bool isDebug, out ErrorResultTO errors)
        {
            Tracker.TrackEvent(TrackerEventGroup.Workflows, TrackerEventName.Executed, string.Format("RES-{0}", dataTransferObject.ResourceID));

            if(AllErrors == null)
            {
                AllErrors = new ErrorResultTO();
            }
            IExecutionToken exeToken = new ExecutionToken { IsUserCanceled = false };

            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.StartedCallback);
            WorkflowApplication wfApp = InitEntryPoint(workflowActivity, dataTransferObject, executionExtensions, instanceId, workspace, bookmarkName);
            errors = new ErrorResultTO();

            if(wfApp != null)
            {
                // add termination token
                wfApp.Extensions.Add(exeToken);

                using(ManualResetEventSlim waitHandle = new ManualResetEventSlim(false))
                {
                    WorkflowApplicationRun run = new WorkflowApplicationRun(this, waitHandle, dataTransferObject, wfApp, workspace, executionExtensions, FetchParentInstanceId(dataTransferObject), isDebug, errors, exeToken);


                    if(instanceId == Guid.Empty)
                    {
                        Interlocked.Increment(ref Balance);
                        run.Run();
                        _runTime = DateTime.Now;
                        waitHandle.Wait();
                    }
                    else
                    {
                        Interlocked.Increment(ref Balance);

                        try
                        {
                            if(!string.IsNullOrEmpty(bookmarkName))
                            {
                                dataTransferObject.CurrentBookmarkName = bookmarkName;
                                run.Resume(dataTransferObject);
                            }
                            else
                            {
                                run.Run();
                                _runTime = DateTime.Now;
                            }

                            waitHandle.Wait();
                        }
                        catch(InstanceNotReadyException e1)
                        {
                            Interlocked.Decrement(ref Balance);
                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            errors.AddError(e1.Message);
                            AllErrors.AddError(e1.Message);
                            return null;
                        }
                        catch(InstancePersistenceException e2)
                        {
                            Interlocked.Decrement(ref Balance);
                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            errors.AddError(e2.Message);
                            AllErrors.AddError(e2.Message);
                            return run.DataTransferObject.Clone();
                        }
                        catch(Exception ex)
                        {
                            Interlocked.Decrement(ref Balance);

                            errors.AddError(ex.Message);
                            AllErrors.AddError(ex.Message);

                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            return run.DataTransferObject.Clone();
                        }
                    }

                    Interlocked.Decrement(ref Balance);
                    dataTransferObject = run.DataTransferObject.Clone();
                    var wfappUtils = new WfApplicationUtils();

                    ErrorResultTO invokeErrors;
                    if (dataTransferObject.IsDebugMode())
                    {
                        wfappUtils.DispatchDebugState(dataTransferObject, StateType.End, AllErrors.HasErrors(), AllErrors.MakeDisplayReady(), out invokeErrors, _runTime, false, true);
                    }
                    //AllErrors.MergeErrors(invokeErrors);
                    // avoid memory leak ;)
                    run.Dispose();
                }
            }
            else
            {
                errors.AddError("Internal System Error : Could not create workflow execution wrapper");
            }

            return dataTransferObject;
        }
        /// <summary>
        /// Invokes the workflow impl.
        /// </summary>
        /// <param name="workflowActivity">The workflow activity.</param>
        /// <param name="dataTransferObject">The data transfer object.</param>
        /// <param name="executionExtensions">The execution extensions.</param>
        /// <param name="instanceId">The instance id.</param>
        /// <param name="workspace">The workspace.</param>
        /// <param name="bookmarkName">Name of the bookmark.</param>
        /// <param name="isDebug">if set to <c>true</c> [is debug].</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        private IDSFDataObject InvokeWorkflowImpl(Activity workflowActivity, IDSFDataObject dataTransferObject, IList <object> executionExtensions, Guid instanceId, IWorkspace workspace, string bookmarkName, bool isDebug, out ErrorResultTO errors)
        {
            Tracker.TrackEvent(TrackerEventGroup.Workflows, TrackerEventName.Executed, string.Format("RES-{0}", dataTransferObject.ResourceID));

            if (AllErrors == null)
            {
                AllErrors = new ErrorResultTO();
            }
            IExecutionToken exeToken = new ExecutionToken {
                IsUserCanceled = false
            };

            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.StartedCallback);
            WorkflowApplication wfApp = InitEntryPoint(workflowActivity, dataTransferObject, executionExtensions, instanceId, workspace, bookmarkName);

            errors = new ErrorResultTO();

            if (wfApp != null)
            {
                // add termination token
                wfApp.Extensions.Add(exeToken);

                using (ManualResetEventSlim waitHandle = new ManualResetEventSlim(false))
                {
                    WorkflowApplicationRun run = new WorkflowApplicationRun(this, waitHandle, dataTransferObject, wfApp, workspace, executionExtensions, FetchParentInstanceId(dataTransferObject), isDebug, errors, exeToken);


                    if (instanceId == Guid.Empty)
                    {
                        Interlocked.Increment(ref Balance);
                        run.Run();
                        _runTime = DateTime.Now;
                        waitHandle.Wait();
                    }
                    else
                    {
                        Interlocked.Increment(ref Balance);

                        try
                        {
                            if (!string.IsNullOrEmpty(bookmarkName))
                            {
                                dataTransferObject.CurrentBookmarkName = bookmarkName;
                                run.Resume(dataTransferObject);
                            }
                            else
                            {
                                run.Run();
                                _runTime = DateTime.Now;
                            }

                            waitHandle.Wait();
                        }
                        catch (InstanceNotReadyException e1)
                        {
                            Interlocked.Decrement(ref Balance);
                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            errors.AddError(e1.Message);
                            AllErrors.AddError(e1.Message);
                            return(null);
                        }
                        catch (InstancePersistenceException e2)
                        {
                            Interlocked.Decrement(ref Balance);
                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            errors.AddError(e2.Message);
                            AllErrors.AddError(e2.Message);
                            return(run.DataTransferObject.Clone());
                        }
                        catch (Exception ex)
                        {
                            Interlocked.Decrement(ref Balance);

                            errors.AddError(ex.Message);
                            AllErrors.AddError(ex.Message);

                            ExecutionStatusCallbackDispatcher.Instance.Post(dataTransferObject.ExecutionCallbackID, ExecutionStatusCallbackMessageType.ErrorCallback);

                            return(run.DataTransferObject.Clone());
                        }
                    }

                    Interlocked.Decrement(ref Balance);
                    dataTransferObject = run.DataTransferObject.Clone();
                    var wfappUtils = new WfApplicationUtils();

                    if (dataTransferObject.IsDebugMode())
                    {
                        ErrorResultTO invokeErrors;
                        wfappUtils.DispatchDebugState(dataTransferObject, StateType.End, AllErrors.HasErrors(), AllErrors.MakeDisplayReady(), out invokeErrors, _runTime, false, true);
                    }
                    //AllErrors.MergeErrors(invokeErrors);
                    // avoid memory leak ;)
                    run.Dispose();
                }
            }
            else
            {
                errors.AddError("Internal System Error : Could not create workflow execution wrapper");
            }

            return(dataTransferObject);
        }
        /// <summary>
        /// Executes the specified errors.
        /// </summary>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override Guid Execute(out ErrorResultTO errors)
        {
            errors = new ErrorResultTO();
           // WorkflowApplicationFactory wfFactor = new WorkflowApplicationFactory();
            Guid result = GlobalConstants.NullDataListID;




            // Set Service Name
            DataObject.ServiceName = ServiceAction.ServiceName;
           
            // Set server ID, only if not set yet - original server;
            if(DataObject.ServerID == Guid.Empty)
                DataObject.ServerID = HostSecurityProvider.Instance.ServerID;

            // Set resource ID, only if not set yet - original resource;
            if(DataObject.ResourceID == Guid.Empty && ServiceAction != null && ServiceAction.Service != null)
                DataObject.ResourceID = ServiceAction.Service.ID;

            // Travis : Now set Data List
            DataObject.DataList = ServiceAction.DataListSpecification;
            // Set original instance ID, only if not set yet - original resource;
            if(DataObject.OriginalInstanceID == Guid.Empty)
                DataObject.OriginalInstanceID = DataObject.DataListID;
            Dev2Logger.Log.Info(String.Format("Started Execution for Service Name:{0} Resource Id:{1} Mode:{2}",DataObject.ServiceName,DataObject.ResourceID,DataObject.IsDebug?"Debug":"Execute"));
            //Set execution origin
            if(!string.IsNullOrWhiteSpace(DataObject.ParentServiceName))
            {
                DataObject.ExecutionOrigin = ExecutionOrigin.Workflow;
                DataObject.ExecutionOriginDescription = DataObject.ParentServiceName;
            }
            else if(DataObject.IsDebug)
            {
                DataObject.ExecutionOrigin = ExecutionOrigin.Debug;
            }
            else
            {
                DataObject.ExecutionOrigin = ExecutionOrigin.External;
            }

            try
            {
                // BUG 9304 - 2013.05.08 - TWR - Added CompileExpressions
                //_workflowHelper.CompileExpressions(theActivity,DataObject.ResourceID);

                //IDSFDataObject exeResult = wfFactor.InvokeWorkflow(activity.Value, DataObject,
                //                                                   new List<object> { EsbChannel, }, instanceId,
                //                                                   TheWorkspace, bookmark, out errors);
                var wfappUtils = new WfApplicationUtils();
                IExecutionToken exeToken = new ExecutionToken { IsUserCanceled = false };
                DataObject.ExecutionToken = exeToken;
                ErrorResultTO invokeErrors;
                if (DataObject.IsDebugMode())
                {
                    wfappUtils.DispatchDebugState(DataObject, StateType.Start, DataObject.Environment.HasErrors(), DataObject.Environment.FetchErrors(), out invokeErrors, null, true);
                }
                Eval(DataObject.ResourceID, DataObject);
                if (DataObject.IsDebugMode())
                {
                    wfappUtils.DispatchDebugState(DataObject, StateType.End, DataObject.Environment.HasErrors(), DataObject.Environment.FetchErrors(), out invokeErrors, DateTime.Now, false, true);
                }
                result = DataObject.DataListID;
            }
            catch(InvalidWorkflowException iwe)
            {
                Dev2Logger.Log.Error(iwe);
                var msg = iwe.Message;

                int start = msg.IndexOf("Flowchart ", StringComparison.Ordinal);

                // trap the no start node error so we can replace it with something nicer ;)
                errors.AddError(start > 0 ? GlobalConstants.NoStartNodeError : iwe.Message);
            }
            catch(Exception ex)
            {
                Dev2Logger.Log.Error(ex);
                errors.AddError(ex.Message);
            }
            finally
            {
                //ServiceAction.PushActivity(activity);
            }
            Dev2Logger.Log.Info(String.Format("Completed Execution for Service Name:{0} Resource Id: {1} Mode:{2}",DataObject.ServiceName,DataObject.ResourceID,DataObject.IsDebug?"Debug":"Execute"));
            return result;
        }
Exemplo n.º 7
0
        Guid ExecuteWf(ErrorResultTO to)
        {
            Guid result = new Guid();
            try
            {
                // BUG 9304 - 2013.05.08 - TWR - Added CompileExpressions
                //_workflowHelper.CompileExpressions(theActivity,DataObject.ResourceID);

                //IDSFDataObject exeResult = wfFactor.InvokeWorkflow(activity.Value, DataObject,
                //                                                   new List<object> { EsbChannel, }, instanceId,
                //                                                   TheWorkspace, bookmark, out errors);
                var wfappUtils = new WfApplicationUtils();
                IExecutionToken exeToken = new ExecutionToken { IsUserCanceled = false };
                DataObject.ExecutionToken = exeToken;
                ErrorResultTO invokeErrors;
                if(DataObject.IsDebugMode())
                {
                    wfappUtils.DispatchDebugState(DataObject, StateType.Start, DataObject.Environment.HasErrors(), DataObject.Environment.FetchErrors(), out invokeErrors, DateTime.Now, true, false, false);
                }
                Eval(DataObject.ResourceID, DataObject);
                if(DataObject.IsDebugMode())
                {
                    wfappUtils.DispatchDebugState(DataObject, StateType.End, DataObject.Environment.HasErrors(), DataObject.Environment.FetchErrors(), out invokeErrors, DataObject.StartTime, false, true);
                }
                result = DataObject.DataListID;
            }
            catch(InvalidWorkflowException iwe)
            {
                Dev2Logger.Log.Error(iwe);
                var msg = iwe.Message;

                int start = msg.IndexOf("Flowchart ", StringComparison.Ordinal);

                // trap the no start node error so we can replace it with something nicer ;)
                to.AddError(start > 0 ? GlobalConstants.NoStartNodeError : iwe.Message);
            }
            catch(Exception ex)
            {
                Dev2Logger.Log.Error(ex);
                to.AddError(ex.Message);
            }
            return result;
        }