Beispiel #1
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);
                                    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);
                }
            }
        }