public async Task <bool> ResumeWorkflow(string workflowId) { if (LockProvider.AcquireLock(workflowId).Result) { bool requeue = false; try { var wf = await PersistenceStore.GetWorkflowInstance(workflowId); if (wf.Status == WorkflowStatus.Suspended) { wf.Status = WorkflowStatus.Runnable; await PersistenceStore.PersistWorkflow(wf); requeue = true; return(true); } return(false); } finally { await LockProvider.ReleaseLock(workflowId); if (requeue) { await QueueProvider.QueueForProcessing(workflowId); } } } return(false); }
public async Task <bool> SuspendWorkflow(string workflowId) { if (await LockProvider.AcquireLock(workflowId, new CancellationToken())) { 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); } } return(false); }
/// <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); } } }
/// <summary> /// Poll the persistence store for workflows ready to run. /// Poll the persistence store for stashed unpublished events /// </summary> private void PollRunnables(object target) { try { if (LockProvider.AcquireLock("poll runnables").Result) { try { Logger.LogInformation("Polling for runnable workflows"); IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); var runnables = persistenceStore.GetRunnableInstances().Result; foreach (var item in runnables) { Logger.LogDebug("Got runnable instance {0}", item); QueueProvider.QueueForProcessing(item); } } finally { LockProvider.ReleaseLock("poll runnables").Wait(); } } } catch (Exception ex) { Logger.LogError(ex.Message); } try { if (LockProvider.AcquireLock("unpublished events").Result) { try { Logger.LogInformation("Polling for unpublished events"); IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); var events = persistenceStore.GetUnpublishedEvents().Result.ToList(); foreach (var item in events) { Logger.LogDebug("Got unpublished event {0} {1}", item.EventName, item.EventKey); QueueProvider.QueueForPublishing(item).Wait(); persistenceStore.RemoveUnpublishedEvent(item.Id).Wait(); } } finally { LockProvider.ReleaseLock("unpublished events").Wait(); } } } catch (Exception ex) { Logger.LogError(ex.Message); } }
public async Task <bool> TerminateWorkflow(string workflowId) { if (LockProvider.AcquireLock(workflowId).Result) { try { var wf = await PersistenceStore.GetWorkflowInstance(workflowId); wf.Status = WorkflowStatus.Terminated; await PersistenceStore.PersistWorkflow(wf); return(true); } finally { await LockProvider.ReleaseLock(workflowId); } } return(false); }
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); } } }