Exemple #1
0
        private void CreateWorkflowIfNotExists(Guid id)
        {
            if (WorkflowInit.Runtime.IsProcessExists(id))
            {
                return;
            }

            using (var sync = new WorkflowSync(WorkflowInit.Runtime, id))
            {
                WorkflowInit.Runtime.CreateInstance("SimpleWF", id);

                sync.StatrtWaitingFor(new List <ProcessStatus> {
                    ProcessStatus.Idled, ProcessStatus.Initialized
                });

                sync.Wait(new TimeSpan(0, 0, 10));
            }
        }
        private WorkflowSync GetWorkflowSync(Guid instanceId)
        {
            var sync = new WorkflowSync(WorkflowInitService.Runtime, instanceId);

            sync.WorkflowIdled += delegate(object sender, EventArgs e)
            {
                try
                {
                    var wf = WorkflowInitService.Runtime.GetWorkflow(instanceId);
                    //wf.TryUnload();
                }
                catch (InvalidOperationException)
                {
                }
            };

            return(sync);
        }
        public void SetWorkflowState(Guid instanceId, ServiceIdentity serviceIdentity, string stateName, string comment)
        {
            var workflowState = WorkflowStateService.GetWorkflowState(instanceId);


            if (workflowState.WorkflowStateName != stateName || workflowState.IsInitial)
            //Для черновиков устанавливаем всегда это временно
            {
                WorkflowSupportService.TryUpgradeWorkflow(Runtime, instanceId);

                WorkflowParcelService.AddParcel(instanceId,
                                                new WorkflowSetStateParcel()
                {
                    Comment               = comment,
                    InitiatorId           = serviceIdentity.Id,
                    Command               = WorkflowCommand.Unknown,
                    PreviousWorkflowState = workflowState
                });
                bool isIdled = true;
                using (var sync = new WorkflowSync(Runtime, instanceId))
                {
                    if (!WorkflowSupportService.CreateWorkflowIfNotExists(Runtime, instanceId, workflowState.Type))
                    //Это ожидание создания воркфлоу
                    {
                        sync.WaitHandle.WaitOne(600000);
                        isIdled = sync.WasIdled;
                    }
                }
                //Если воркфлоу не стало идленым - то его необходимо удалить полностью и создать заново
                if (!isIdled)
                {
                    using (var context = this.CreateContext())
                    {
                        context.DeleteWorkflowInPesistenceStore(instanceId);
                        context.SubmitChanges();
                    }
                    using (var sync = new WorkflowSync(Runtime, instanceId))
                    {
                        if (!WorkflowSupportService.CreateWorkflowIfNotExists(Runtime, instanceId, workflowState.Type))
                        //Это ожидание создания воркфлоу
                        {
                            sync.WaitHandle.WaitOne(600000);
                        }
                    }
                }

                var instance         = new StateMachineWorkflowInstance(Runtime, instanceId);
                var newWorkflowState = WorkflowStateService.GetWorkflowState(instanceId);
                if (newWorkflowState.WorkflowStateName != stateName)
                {
                    using (var sync = new WorkflowSync(Runtime, instanceId))
                    //Это ожидание завершения установки состояния воркфлоу
                    {
                        instance.SetState(stateName);
                        sync.WaitHandle.WaitOne(600000);
                    }
                }
                WorkflowState state =
                    WorkflowState.AllStates.First(
                        ws => ws.WorkflowStateName == stateName && ws.Type.Id == workflowState.Type.Id);
                if (!state.IsFinal && !state.IsInitial)
                {
                    WorkflowSupportService.RewriteWorkflow(instanceId, state);
                }
                //Для РД удаляем историю согласования
                if (workflowState.Type == WorkflowType.BillDemandWorkfow)
                {
                    if (state == WorkflowState.BillLimitManagerSighting)
                    {
                        BillDemandBuinessService.LimitExecutorResetSights(instanceId);
                    }

                    BillDemandBuinessService.LimitExecutorResetSights(instanceId);
                    BillDemandBuinessService.LimitManagerResetSights(instanceId);
                }
            }
        }
        public void UpgradeWorkflow(WorkflowRuntime runtime, Guid instanceId)
        {
            WorkflowInstance workflowInstance = runtime.GetWorkflow(instanceId);

            var definition = workflowInstance.GetWorkflowDefinition();

            if (!OldAssemblyNames.Contains(definition.GetType().Assembly.FullName))
            {
                return;
            }

            lock (GetLockForWorkflow(instanceId))
            {
                workflowInstance.Unload();

                var are = new AutoResetEvent(false);

                var parameters = new Dictionary <string, object>();

                //Получаем перзистанс и извлекаем состояние
                var persistance = runtime.GetService <NotTerminatingSqlWorkflowPersistenceService>();

                persistance.OnArgsAllowed +=
                    delegate(object sender, NotTerminatingSqlWorkflowPersistenceService.WorkflowSavedParametersArgs e)
                {
                    if (e.InstanceId == instanceId)
                    {
                        parameters = e.Parameters;
                        are.Set();
                    }
                };

                workflowInstance = runtime.GetWorkflow(instanceId);

                definition = workflowInstance.GetWorkflowDefinition();
                if (!OldAssemblyNames.Contains(definition.GetType().Assembly.FullName))
                {
                    //Если версия изменилась то дальнейшие манипуляции не нужны
                    return;
                }

                are.WaitOne(10000);

                workflowInstance.Unload();

                using (var context = this.CreateContext())
                {
                    context.DeleteWorkflowInPesistenceStore(instanceId);
                    context.SubmitChanges();
                }
                var workflowState = WorkflowStateService.GetWorkflowState(instanceId);

                parameters.Add(StateMachineWithSimpleContainer.DontWriteToWorkflowHistoryPersistenceKey, true);

                using (var sync = new WorkflowSync(runtime, instanceId))
                {
                    if (!CreateWorkflowIfNotExists(runtime, instanceId, workflowState.Type, parameters))
                    //Это ожидание создания воркфлоу
                    {
                        sync.WaitHandle.WaitOne(600000);
                    }
                }

                var wfinstance = new StateMachineWorkflowInstance(runtime, instanceId);
                using (var sync = new WorkflowSync(runtime, instanceId))
                //Это ожидание завершения установки состояния воркфлоу
                {
                    wfinstance.SetState(workflowState.WorkflowStateName);
                    sync.WaitHandle.WaitOne(600000);
                }

                var args = new SetWorkflowInternalParametersEventArgs(instanceId,
                                                                      new Dictionary <string, object>()
                {
                    {
                        StateMachineWithSimpleContainer.
                        DontWriteToWorkflowHistoryPersistenceKey
                        ,
                        false
                    }
                });
                SetInternalParameters(null, args);
            }
        }