| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Activities; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Alterations.Core.Contexts; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides contextual information about an alteration. |
| | | 11 | | /// </summary> |
| | | 12 | | public class AlterationContext |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="AlterationContext"/> class. |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public AlterationContext( |
| | 0 | 18 | | IAlteration alteration, |
| | 0 | 19 | | WorkflowExecutionContext workflowExecutionContext, |
| | 0 | 20 | | AlterationLog log, |
| | 0 | 21 | | CancellationToken cancellationToken) |
| | | 22 | | { |
| | 0 | 23 | | Alteration = alteration; |
| | 0 | 24 | | WorkflowExecutionContext = workflowExecutionContext; |
| | 0 | 25 | | AlterationLog = log; |
| | 0 | 26 | | CancellationToken = cancellationToken; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The alteration being handled. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public IAlteration Alteration { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// A workflow execution context of the workflow instance being altered. This offers maximum flexibility for alterin |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public WorkflowExecutionContext WorkflowExecutionContext { get; set; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The workflow of the workflow instance being altered. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public Workflow Workflow => WorkflowExecutionContext.Workflow; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The cancellation token. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public CancellationToken CancellationToken { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The service provider. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public IServiceProvider ServiceProvider => WorkflowExecutionContext.ServiceProvider; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The alteration log. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public AlterationLog AlterationLog { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// A flag indicating whether the alteration has succeeded. |
| | | 61 | | /// </summary> |
| | 0 | 62 | | public bool HasSucceeded { get; private set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// A flag indicating whether the alteration has failed. |
| | | 66 | | /// </summary> |
| | 0 | 67 | | public bool HasFailed { get; private set; } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// An optional action to be executed when the alteration is committed. Set this to perform permanent side effects s |
| | | 71 | | /// </summary> |
| | 0 | 72 | | public Func<Task>? CommitAction { get; set; } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Logs a message. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="eventName">The event name to log.</param> |
| | | 78 | | /// <param name="message">The message to log.</param> |
| | | 79 | | /// <param name="logLevel">The log level.</param> |
| | | 80 | | public void Log(string eventName, string message, LogLevel logLevel = LogLevel.Information) |
| | | 81 | | { |
| | 0 | 82 | | AlterationLog.Add(message, logLevel, eventName); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Marks the alteration as succeeded. |
| | | 87 | | /// </summary> |
| | | 88 | | public void Succeed() |
| | | 89 | | { |
| | 0 | 90 | | Succeed($"{Alteration.GetType().Name} succeeded"); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Marks the alteration as succeeded. |
| | | 95 | | /// </summary> |
| | | 96 | | public void Succeed(Func<Task> commitAction) |
| | | 97 | | { |
| | 0 | 98 | | Succeed(); |
| | 0 | 99 | | CommitAction = commitAction; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Marks the alteration as succeeded. |
| | | 104 | | /// </summary> |
| | | 105 | | public void Succeed(Action commitAction) |
| | | 106 | | { |
| | 0 | 107 | | Succeed(); |
| | 0 | 108 | | CommitAction = () => |
| | 0 | 109 | | { |
| | 0 | 110 | | commitAction(); |
| | 0 | 111 | | return Task.CompletedTask; |
| | 0 | 112 | | }; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Marks the alteration as succeeded. |
| | | 117 | | /// </summary> |
| | | 118 | | public void Succeed(string message) |
| | | 119 | | { |
| | 0 | 120 | | HasSucceeded = true; |
| | 0 | 121 | | Log($"Alteration {Alteration.GetType().Name} succeeded", message); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Marks the alteration as succeeded. |
| | | 126 | | /// </summary> |
| | | 127 | | public void Succeed(string message, Func<Task> commitAction) |
| | | 128 | | { |
| | 0 | 129 | | Succeed(message); |
| | 0 | 130 | | CommitAction = commitAction; |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Marks the alteration as succeeded. |
| | | 135 | | /// </summary> |
| | | 136 | | public void Succeed(string message, Action commitAction) |
| | | 137 | | { |
| | 0 | 138 | | Succeed(message); |
| | 0 | 139 | | CommitAction = () => |
| | 0 | 140 | | { |
| | 0 | 141 | | commitAction(); |
| | 0 | 142 | | return Task.CompletedTask; |
| | 0 | 143 | | }; |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Marks the alteration as failed. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="message">An optional message.</param> |
| | | 150 | | public void Fail(string? message = null) |
| | | 151 | | { |
| | 0 | 152 | | HasFailed = true; |
| | 0 | 153 | | Log($"Alteration {Alteration.GetType().Name} failed", message ?? $"{Alteration.GetType().Name} failed", LogLevel |
| | 0 | 154 | | } |
| | | 155 | | } |