Beispiel #1
0
        public async Task <bool> SuspendWorkflow(string workflowId)
        {
            if (!await _lockProvider.AcquireLock(workflowId, new CancellationToken()))
            {
                return(false);
            }

            try
            {
                var wf = await _persistenceStore.GetWorkflowInstance(workflowId);

                if (wf.Status == WorkflowStatus.Runnable)
                {
                    wf.Status = WorkflowStatus.Suspended;
                    await _persistenceStore.PersistWorkflow(wf);

                    return(true);
                }

                return(false);
            }
            finally
            {
                await _lockProvider.ReleaseLock(workflowId);
            }
        }
Beispiel #2
0
        public async Task <bool> SuspendWorkflow(string workflowId)
        {
            if (!await _lockProvider.AcquireLock(workflowId, new CancellationToken()))
            {
                return(false);
            }

            try
            {
                var wf = await _persistenceStore.GetWorkflowInstance(workflowId);

                if (wf.Status == WorkflowStatus.Runnable)
                {
                    wf.Status = WorkflowStatus.Suspended;
                    await _persistenceStore.PersistWorkflow(wf);

                    await _eventHub.PublishNotification(new WorkflowSuspended()
                    {
                        EventTimeUtc         = DateTime.UtcNow,
                        Reference            = wf.Reference,
                        WorkflowInstanceId   = wf.Id,
                        WorkflowDefinitionId = wf.WorkflowDefinitionId,
                        Version = wf.Version
                    });

                    return(true);
                }

                return(false);
            }
            finally
            {
                await _lockProvider.ReleaseLock(workflowId);
            }
        }
        public async Task <ActionResult <WorkflowInstance> > Get(string id)
        {
            var result = await _persistenceProvider.GetWorkflowInstance(id);

            if (result == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <WorkflowInstance>(result)));
        }
Beispiel #4
0
        //[Authorize(Policy = Policies.Viewer)]
        public async Task <ActionResult <WorkflowInstance> > Get(string id)
        {
            var logTrace = new LogTrace();

            AiLogger.LogInfo(logTrace, "Workflow Get: " + id);

            var result = await _persistenceProvider.GetWorkflowInstance(id);

            if (result == null)
            {
                return(NotFound());
            }

            return(Ok(_mapper.Map <WorkflowInstance>(result)));
        }
        private async Task <bool> SeedSubscription(Event evt, EventSubscription sub, List <string> toQueue, CancellationToken cancellationToken)
        {
            foreach (var eventId in await _persistenceStore.GetEvents(sub.EventName, sub.EventKey, sub.SubscribeAsOf))
            {
                if (eventId == evt.Id)
                {
                    continue;
                }

                var siblingEvent = await _persistenceStore.GetEvent(eventId);

                if ((!siblingEvent.IsProcessed) && (siblingEvent.EventTime < evt.EventTime))
                {
                    await QueueProvider.QueueWork(eventId, QueueType.Event);

                    return(false);
                }

                if (!siblingEvent.IsProcessed)
                {
                    toQueue.Add(siblingEvent.Id);
                }
            }

            if (!await _lockProvider.AcquireLock(sub.WorkflowId, cancellationToken))
            {
                Logger.LogInformation("Workflow locked {0}", sub.WorkflowId);
                return(false);
            }

            try
            {
                var workflow = await _persistenceStore.GetWorkflowInstance(sub.WorkflowId);

                var pointers = workflow.ExecutionPointers.Where(p => p.EventName == sub.EventName && p.EventKey == sub.EventKey && !p.EventPublished && p.EndTime == null);
                foreach (var p in pointers)
                {
                    p.EventData      = evt.EventData;
                    p.EventPublished = true;
                    p.Active         = true;
                }
                workflow.NextExecution = 0;
                await _persistenceStore.PersistWorkflow(workflow);

                await _persistenceStore.TerminateSubscription(sub.Id);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.Message);
                return(false);
            }
            finally
            {
                await _lockProvider.ReleaseLock(sub.WorkflowId);

                await QueueProvider.QueueWork(sub.WorkflowId, QueueType.Workflow);
            }
        }
Beispiel #6
0
        public async Task <IActionResult> Get(string id)
        {
            var result = await _workflowStore.GetWorkflowInstance(id);

            _logger.LogInformation("Workflow Instance: {result}", result);
            return(Ok("Workflow Instance: {result}" + result));
        }
Beispiel #7
0
        /// <summary>
        /// Worker thread body
        /// </summary>
        private void RunWorkflows()
        {
            IWorkflowExecutor    workflowExecutor = _serviceProvider.GetService <IWorkflowExecutor>();
            IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>();

            while (!_shutdown)
            {
                try
                {
                    var workflowId = QueueProvider.DequeueForProcessing().Result;
                    if (workflowId != null)
                    {
                        try
                        {
                            if (LockProvider.AcquireLock(workflowId).Result)
                            {
                                WorkflowInstance workflow = null;
                                try
                                {
                                    workflow = persistenceStore.GetWorkflowInstance(workflowId).Result;
                                    if (workflow.Status == WorkflowStatus.Runnable)
                                    {
                                        workflowExecutor.Execute(workflow, persistenceStore, Options);
                                    }
                                }
                                finally
                                {
                                    LockProvider.ReleaseLock(workflowId).Wait();
                                    if (workflow != null)
                                    {
                                        if ((workflow.Status == WorkflowStatus.Runnable) && workflow.NextExecution.HasValue && workflow.NextExecution.Value < DateTime.Now.ToUniversalTime().Ticks)
                                        {
                                            QueueProvider.QueueForProcessing(workflowId);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Logger.LogInformation("Workflow locked {0}", workflowId);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex.Message);
                        }
                    }
                    else
                    {
                        Thread.Sleep(Options.IdleTime); //no work
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.Message);
                }
            }
        }
Beispiel #8
0
        //[Authorize(Policy = Policies.Controller)]
        public async Task <ActionResult <WorkflowInstance> > StartWorkflow(string id)
        {
            string contentReq = null;

            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                contentReq = await reader.ReadToEndAsync();
            }

            var logTrace = new LogTrace();

            AiLogger.LogInfo(logTrace, "Workflow Start: " + contentReq);

            object workflowData = contentReq;
            string workflowId   = null;

            if (id.Equals(CompressJobWorkflow.WorkflowId))
            {
                workflowId   = CompressJobWorkflow.WorkflowId;
                workflowData = NewtonJsonConvert.DeserializeObject <CompressJobData>(contentReq);
            }
            else if (id.Equals(WfFileInWorkflow.WorkflowId))
            {
                workflowId   = WfFileInWorkflow.WorkflowId;
                workflowData = NewtonJsonConvert.DeserializeObject <WfFileInData>(contentReq);
            }
            else if (id.Equals(Test01UserWorkflow.WorkflowId))
            {
                var wfEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <Pdf4meWorkflowEvent>(contentReq);
                workflowData = new Pdf4meWorkflowData()
                {
                    WorkflowEvent = wfEvent
                };

                workflowId = Test01UserWorkflow.WorkflowId;
                //workflowData = NewtonJsonConvert.DeserializeObject<WfFileInData>(contentReq);
            }
            else
            {
                var wfEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <Pdf4meWorkflowEvent>(contentReq);
                workflowData = new Pdf4meWorkflowData()
                {
                    WorkflowEvent = wfEvent
                };

                workflowId = id;
            }

            var instanceId = await _workflowController.StartWorkflow(workflowId, workflowData);

            var result = await _persistenceProvider.GetWorkflowInstance(instanceId);

            return(Created(instanceId, _mapper.Map <WorkflowInstance>(result)));
        }
Beispiel #9
0
        protected override async Task ProcessItem(string itemId, CancellationToken cancellationToken)
        {
            if (!await _lockProvider.AcquireLock(itemId, cancellationToken))
            {
                Logger.LogInformation("Workflow locked {0}", itemId);
                return;
            }

            WorkflowInstance       workflow = null;
            WorkflowExecutorResult result   = null;

            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                workflow = await _persistenceStore.GetWorkflowInstance(itemId, cancellationToken);

                if (workflow.Status == WorkflowStatus.Runnable)
                {
                    try
                    {
                        result = await _executor.Execute(workflow, cancellationToken);
                    }
                    finally
                    {
                        await _persistenceStore.PersistWorkflow(workflow, cancellationToken);

                        await QueueProvider.QueueWork(itemId, QueueType.Index);

                        _greylist.Remove($"wf:{itemId}");
                    }
                }
            }
            finally
            {
                await _lockProvider.ReleaseLock(itemId);

                if ((workflow != null) && (result != null))
                {
                    foreach (var sub in result.Subscriptions)
                    {
                        await SubscribeEvent(sub, _persistenceStore, cancellationToken);
                    }

                    await _persistenceStore.PersistErrors(result.Errors, cancellationToken);

                    var readAheadTicks = _datetimeProvider.UtcNow.Add(Options.PollInterval).Ticks;

                    if ((workflow.Status == WorkflowStatus.Runnable) && workflow.NextExecution.HasValue && workflow.NextExecution.Value < readAheadTicks)
                    {
                        new Task(() => FutureQueue(workflow, cancellationToken)).Start();
                    }
                }
            }
        }
        public async void AddWorkflowData(string workflowId, string key, object data)
        {
            var wf = await _persistenceStore.GetWorkflowInstance(workflowId);

            var dict = (wf.Data as IDictionary <string, object>);

            if (dict == null)
            {
                throw new NullReferenceException("unable to load workflow data as dictionary");
            }
            if (dict.ContainsKey(key))
            {
                dict[key] = data;
            }
            else
            {
                dict.Add(key, data);
            }


            await _persistenceStore.PersistWorkflow(wf);
        }
        private async Task <bool> SeedSubscription(Event evt, EventSubscription sub, CancellationToken cancellationToken)
        {
            if (await _lockProvider.AcquireLock(sub.WorkflowId, cancellationToken))
            {
                try
                {
                    var workflow = await _persistenceStore.GetWorkflowInstance(sub.WorkflowId);

                    var pointers = workflow.ExecutionPointers.Where(p => p.EventName == sub.EventName && p.EventKey == sub.EventKey && !p.EventPublished && p.EndTime == null);
                    foreach (var p in pointers)
                    {
                        p.EventData      = evt.EventData;
                        p.EventPublished = true;
                        p.Active         = true;
                    }
                    workflow.NextExecution = 0;
                    await _persistenceStore.PersistWorkflow(workflow);

                    await _persistenceStore.TerminateSubscription(sub.Id);

                    return(true);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.Message, ex);
                    return(false);
                }
                finally
                {
                    await _lockProvider.ReleaseLock(sub.WorkflowId);

                    await QueueProvider.QueueWork(sub.WorkflowId, QueueType.Workflow);
                }
            }
            else
            {
                Logger.LogInformation("Workflow locked {0}", sub.WorkflowId);
                return(false);
            }
        }
Beispiel #12
0
        private void RunPublications()
        {
            IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>();

            while (!_shutdown)
            {
                try
                {
                    var pub = QueueProvider.DequeueForPublishing().Result;
                    if (pub != null)
                    {
                        try
                        {
                            if (LockProvider.AcquireLock(pub.WorkflowId).Result)
                            {
                                try
                                {
                                    var workflow = persistenceStore.GetWorkflowInstance(pub.WorkflowId).Result;
                                    var pointers = workflow.ExecutionPointers.Where(p => p.EventName == pub.EventName && p.EventKey == p.EventKey && !p.EventPublished);
                                    foreach (var p in pointers)
                                    {
                                        p.EventData      = pub.EventData;
                                        p.EventPublished = true;
                                        p.Active         = true;
                                    }
                                    workflow.NextExecution = 0;
                                    persistenceStore.PersistWorkflow(workflow);
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex.Message);
                                    persistenceStore.CreateUnpublishedEvent(pub); //retry later
                                }
                                finally
                                {
                                    LockProvider.ReleaseLock(pub.WorkflowId).Wait();
                                    QueueProvider.QueueForProcessing(pub.WorkflowId);
                                }
                            }
                            else
                            {
                                Logger.LogInformation("Workflow locked {0}", pub.WorkflowId);
                                persistenceStore.CreateUnpublishedEvent(pub); //retry later
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex.Message);
                        }
                    }
                    else
                    {
                        Thread.Sleep(Options.IdleTime); //no work
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.Message);
                }
            }
        }
Beispiel #13
0
        protected WorkflowStatus GetStatus(string workflowId)
        {
            var instance = PersistenceProvider.GetWorkflowInstance(workflowId).Result;

            return(instance.Status);
        }
        public async Task <IActionResult> Get(string id)
        {
            var result = await _workflowStore.GetWorkflowInstance(id);

            return(Json(result));
        }
Beispiel #15
0
        /// <summary>
        /// Worker thread body
        /// </summary>
        private async void RunWorkflows()
        {
            while (!_shutdown)
            {
                try
                {
                    var workflowId = await _queueProvider.DequeueWork(QueueType.Workflow);

                    if (workflowId != null)
                    {
                        try
                        {
                            if (await _lockProvider.AcquireLock(workflowId))
                            {
                                WorkflowInstance       workflow = null;
                                WorkflowExecutorResult result   = null;
                                try
                                {
                                    workflow = await _persistenceStore.GetWorkflowInstance(workflowId);

                                    if (workflow.Status == WorkflowStatus.Runnable)
                                    {
                                        try
                                        {
                                            result = _executor.Execute(workflow, _options);
                                        }
                                        finally
                                        {
                                            await _persistenceStore.PersistWorkflow(workflow);
                                        }
                                    }
                                }
                                finally
                                {
                                    await _lockProvider.ReleaseLock(workflowId);

                                    if ((workflow != null) && (result != null))
                                    {
                                        foreach (var sub in result.Subscriptions)
                                        {
                                            await SubscribeEvent(sub);
                                        }

                                        await _persistenceStore.PersistErrors(result.Errors);

                                        if ((workflow.Status == WorkflowStatus.Runnable) && workflow.NextExecution.HasValue && workflow.NextExecution.Value < DateTime.Now.ToUniversalTime().Ticks)
                                        {
                                            await _queueProvider.QueueWork(workflowId, QueueType.Workflow);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _logger.LogInformation("Workflow locked {0}", workflowId);
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex.Message);
                        }
                    }
                    else
                    {
                        await Task.Delay(_options.IdleTime);  //no work
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                }
            }
        }
Beispiel #16
0
 public async Task <TDataWorkflow> GetFlowInstance <TDataWorkflow>(string instanceId) where TDataWorkflow : class, IDataWorkflow, new()
 {
     return(await _workflowStore.GetWorkflowInstance(instanceId) as TDataWorkflow);
 }