From 4a3ac3d3aaccc6e3e79b90092cd195b8f02cf342 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sun, 3 Jan 2021 19:20:44 +0100 Subject: [PATCH] Add Tasks feature This features allows arbitrary code to add tasks to a registry that can be executed later on. See scheduling as an example. --- .../Models/WorkflowExecutionContext.cs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs index 82d906973..1b034c36d 100644 --- a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs +++ b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -71,6 +72,36 @@ namespace Elsa.Services.Models public bool HasBlockingActivities => WorkflowInstance.BlockingActivities.Any(); public object? WorkflowContext { get; set; } + /// + /// A collection of tasks to execute post-workflow burst execution. + /// + public IDictionary>> Tasks { get; set; } = new Dictionary>>(); + + public void RegisterTask(string groupName, Func task) + { + if (!Tasks.ContainsKey(groupName)) + { + var list = new List> { task }; + Tasks[groupName] = list; + } + else + { + Tasks[groupName].Add(task); + } + } + + public IEnumerable> GetRegisteredTasks(string groupName) => Tasks.ContainsKey(groupName) ? Tasks[groupName] : Enumerable.Empty>(); + + public async ValueTask ExecuteRegisteredTasksAsync(string groupName, CancellationToken cancellationToken = default) + { + var tasks = GetRegisteredTasks(groupName); + + foreach (var task in tasks) + await task(this, cancellationToken); + + Tasks.Remove(groupName); + } + public void SetVariable(string name, object? value) => WorkflowInstance.Variables.Set(name, value); public T? GetVariable(string name) => WorkflowInstance.Variables.Get(name); public object? GetVariable(string name) => WorkflowInstance.Variables.Get(name); @@ -100,9 +131,9 @@ namespace Elsa.Services.Models return activityInstance.Output; } - public T GetOutputFrom(string activityName) => (T)GetOutputFrom(activityName)!; + public T GetOutputFrom(string activityName) => (T) GetOutputFrom(activityName)!; public void SetWorkflowContext(object? value) => WorkflowContext = value; - public T GetWorkflowContext() => (T)WorkflowContext!; + public T GetWorkflowContext() => (T) WorkflowContext!; public async ValueTask> ActivateActivitiesAsync(CancellationToken cancellationToken = default) {