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); _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); } } }