public async Task <string> StartWorkflow <TData>(string workflowId, int?version, TData data = null)
            where TData : class
        {
            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new WorkflowNotRegisteredException(workflowId, version);
            }

            var wf = new WorkflowInstance
            {
                WorkflowDefinitionId = workflowId,
                Version       = def.Version,
                Data          = data,
                Description   = def.Description,
                NextExecution = 0,
                CreateTime    = DateTime.Now.ToUniversalTime(),
                Status        = WorkflowStatus.Runnable
            };

            if ((def.DataType != null) && (data == null))
            {
                wf.Data = TypeExtensions.GetConstructor(def.DataType, new Type[] { }).Invoke(null);
            }

            wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));

            string id = await _persistenceStore.CreateNewWorkflow(wf);

            await _queueProvider.QueueWork(id, QueueType.Workflow);

            return(id);
        }
Esempio n. 2
0
        public async Task <string> StartWorkflow <TData>(string workflowId, int version, TData data)
        {
            if (_shutdown)
            {
                throw new Exception("Host is not running");
            }

            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new Exception(String.Format("Workflow {0} version {1} is not registered", workflowId, version));
            }

            var wf = new WorkflowInstance();

            wf.WorkflowDefinitionId = workflowId;
            wf.Version       = version;
            wf.Data          = data;
            wf.Description   = def.Description;
            wf.NextExecution = 0;
            wf.Status        = WorkflowStatus.Runnable;
            wf.ExecutionPointers.Add(new ExecutionPointer()
            {
                StepId = def.InitialStep, Active = true
            });
            string id = await _persistenceStore.CreateNewWorkflow(wf);

            await _queueProvider.QueueForProcessing(id);

            return(id);
        }
Esempio n. 3
0
        public async Task <string> StartWorkflow <TData>(string workflowId, int?version, TData data = null, string reference = null)
            where TData : class, new()
        {
            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new WorkflowNotRegisteredException(workflowId, version);
            }

            var wf = new WorkflowInstance
            {
                WorkflowDefinitionId = workflowId,
                Version       = def.Version,
                Data          = data,
                Description   = def.Description,
                NextExecution = 0,
                CreateTime    = _dateTimeProvider.UtcNow,
                Status        = WorkflowStatus.Runnable,
                Reference     = reference
            };

            if ((def.DataType != null) && (data == null))
            {
                if (typeof(TData) == def.DataType)
                {
                    wf.Data = new TData();
                }
                else
                {
                    wf.Data = def.DataType.GetConstructor(new Type[0]).Invoke(new object[0]);
                }
            }

            wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));

            using (var scope = _serviceProvider.CreateScope())
            {
                var middlewareRunner = scope.ServiceProvider.GetRequiredService <IWorkflowMiddlewareRunner>();
                await middlewareRunner.RunPreMiddleware(wf, def);
            }

            string id = await _persistenceStore.CreateNewWorkflow(wf);

            await _queueProvider.QueueWork(id, QueueType.Workflow);

            await _queueProvider.QueueWork(id, QueueType.Index);

            await _eventHub.PublishNotification(new WorkflowStarted
            {
                EventTimeUtc         = _dateTimeProvider.UtcNow,
                Reference            = reference,
                WorkflowInstanceId   = id,
                WorkflowDefinitionId = def.Id,
                Version = def.Version
            });

            return(id);
        }
Esempio n. 4
0
        public async Task <string> StartWorkflow <TData>(string workflowId, int?version, TData data = null, string reference = null)
            where TData : class, new()
        {
            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new WorkflowNotRegisteredException(workflowId, version);
            }

            var wf = new WorkflowInstance
            {
                WorkflowDefinitionId = workflowId,
                Version       = def.Version,
                Data          = data,
                Description   = def.Description,
                NextExecution = 0,
                CreateTime    = DateTime.Now.ToUniversalTime(),
                Status        = WorkflowStatus.Runnable,
                Reference     = reference
            };

            if ((def.DataType != null) && (data == null))
            {
                wf.Data = new TData();
            }

            wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));

            string id = await _persistenceStore.CreateNewWorkflow(wf);

            await _queueProvider.QueueWork(id, QueueType.Workflow);

            await _eventHub.PublishNotification(new WorkflowStarted()
            {
                EventTimeUtc         = DateTime.UtcNow,
                Reference            = reference,
                WorkflowInstanceId   = id,
                WorkflowDefinitionId = def.Id,
                Version = def.Version
            });

            return(id);
        }
Esempio n. 5
0
        public async Task <string> StartWorkflow <TData>(string workflowId, int?version, TData data = null)
            where TData : class
        {
            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new WorkflowNotRegisteredException(workflowId, version);
            }

            var wf = new WorkflowInstance
            {
                WorkflowDefinitionId = workflowId,
                Version       = def.Version,
                Data          = data,
                Description   = def.Description,
                NextExecution = 0,
                CreateTime    = DateTime.Now.ToUniversalTime(),
                Status        = WorkflowStatus.Runnable
            };

            if ((def.DataType != null) && (data == null))
            {
                wf.Data = TypeExtensions.GetConstructor(def.DataType, new Type[] { }).Invoke(null);
            }

            wf.ExecutionPointers.Add(new ExecutionPointer
            {
                Id       = Guid.NewGuid().ToString(),
                StepId   = 0,
                Active   = true,
                StepName = Enumerable.First <WorkflowStep>(def.Steps, x => x.Id == 0).Name
            });

            string id = await _persistenceStore.CreateNewWorkflow(wf);

            await _queueProvider.QueueWork(id, QueueType.Workflow);

            return(id);
        }
        public async Task <WorkflowInstance> RunWorkflowSync <TData>(string workflowId, int version, TData data, string reference, CancellationToken token, bool persistSate = true)
            where TData : new()
        {
            var def = _registry.GetDefinition(workflowId, version);

            if (def == null)
            {
                throw new WorkflowNotRegisteredException(workflowId, version);
            }

            var wf = new WorkflowInstance
            {
                WorkflowDefinitionId = workflowId,
                Version       = def.Version,
                Data          = data,
                Description   = def.Description,
                NextExecution = 0,
                CreateTime    = _dateTimeProvider.UtcNow,
                Status        = WorkflowStatus.Suspended,
                Reference     = reference
            };

            if ((def.DataType != null) && (data == null))
            {
                if (typeof(TData) == def.DataType)
                {
                    wf.Data = new TData();
                }
                else
                {
                    wf.Data = def.DataType.GetConstructor(new Type[0]).Invoke(new object[0]);
                }
            }

            wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));

            var id = Guid.NewGuid().ToString();

            if (persistSate)
            {
                id = await _persistenceStore.CreateNewWorkflow(wf, token);
            }
            else
            {
                wf.Id = id;
            }

            wf.Status = WorkflowStatus.Runnable;

            if (!await _lockService.AcquireLock(id, CancellationToken.None))
            {
                throw new InvalidOperationException();
            }

            try
            {
                while ((wf.Status == WorkflowStatus.Runnable) && !token.IsCancellationRequested)
                {
                    await _executor.Execute(wf, token);

                    if (persistSate)
                    {
                        await _persistenceStore.PersistWorkflow(wf, token);
                    }
                }
            }
            finally
            {
                await _lockService.ReleaseLock(id);
            }

            if (persistSate)
            {
                await _queueService.QueueWork(id, QueueType.Index);
            }

            return(wf);
        }