Skip to content

Commit d4c4ca7

Browse files
authored
Merge pull request #442 from serverlessworkflow/fix-add-suspended-workflow-status-phase
Added a new `suspended` workflow instance status phase
2 parents 86a437a + 748f987 commit d4c4ca7

File tree

9 files changed

+31
-12
lines changed

9 files changed

+31
-12
lines changed

src/api/Synapse.Api.Application/Commands/WorkflowInstances/CancelWorkflowInstanceCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public virtual async Task<IOperationResult> HandleAsync(CancelWorkflowInstanceCo
5151
var workflowInstanceReference = new ResourceReference<WorkflowInstance>(command.Name, command.Namespace);
5252
var original = await resources.GetAsync<WorkflowInstance>(command.Name, command.Namespace, cancellationToken).ConfigureAwait(false)
5353
?? throw new ProblemDetailsException(new(ProblemTypes.NotFound, ProblemTitles.NotFound, (int)HttpStatusCode.NotFound, ProblemDescriptions.ResourceNotFound.Format(workflowInstanceReference.ToString())));
54-
if (!string.IsNullOrWhiteSpace(original.Status?.Phase) && original.Status?.Phase != WorkflowInstanceStatusPhase.Pending && original.Status?.Phase != WorkflowInstanceStatusPhase.Running && original.Status?.Phase != WorkflowInstanceStatusPhase.Waiting)
54+
if (!string.IsNullOrWhiteSpace(original.Status?.Phase) && original.Status?.Phase != WorkflowInstanceStatusPhase.Pending && original.Status?.Phase != WorkflowInstanceStatusPhase.Running && original.Status?.Phase != WorkflowInstanceStatusPhase.Suspended && original.Status?.Phase != WorkflowInstanceStatusPhase.Waiting)
5555
throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
5656
var updated = original.Clone()!;
5757
updated.Status ??= new();

src/api/Synapse.Api.Application/Commands/WorkflowInstances/ResumeWorkflowInstanceCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public virtual async Task<IOperationResult> HandleAsync(ResumeWorkflowInstanceCo
5151
var workflowInstanceReference = new ResourceReference<WorkflowInstance>(command.Name, command.Namespace);
5252
var original = await resources.GetAsync<WorkflowInstance>(command.Name, command.Namespace, cancellationToken).ConfigureAwait(false)
5353
?? throw new ProblemDetailsException(new(ProblemTypes.NotFound, ProblemTitles.NotFound, (int)HttpStatusCode.NotFound, ProblemDescriptions.ResourceNotFound.Format(workflowInstanceReference.ToString())));
54-
if (!string.IsNullOrWhiteSpace(original.Status?.Phase) && original.Status?.Phase != WorkflowInstanceStatusPhase.Waiting) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
54+
if (!string.IsNullOrWhiteSpace(original.Status?.Phase) && original.Status?.Phase != WorkflowInstanceStatusPhase.Suspended) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
5555
var updated = original.Clone()!;
5656
updated.Status ??= new();
5757
updated.Status.Phase = WorkflowInstanceStatusPhase.Running;

src/api/Synapse.Api.Application/Commands/WorkflowInstances/SuspendWorkflowInstanceCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public virtual async Task<IOperationResult> HandleAsync(SuspendWorkflowInstanceC
5454
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Running) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
5555
var updated = original.Clone()!;
5656
updated.Status ??= new();
57-
updated.Status.Phase = WorkflowInstanceStatusPhase.Waiting;
57+
updated.Status.Phase = WorkflowInstanceStatusPhase.Suspended;
5858
var jsonPatch = JsonPatchUtility.CreateJsonPatchFromDiff(original, updated);
5959
await resources.PatchStatusAsync<WorkflowInstance>(new(PatchType.JsonPatch, jsonPatch), command.Name, command.Namespace, cancellationToken: cancellationToken).ConfigureAwait(false);
6060
return this.Ok();

src/core/Synapse.Core/WorkflowInstanceStatusPhase.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public static class WorkflowInstanceStatusPhase
2828
/// </summary>
2929
public const string Running = "running";
3030
/// <summary>
31-
/// Indicates that the workflow's execution is waiting for user or event input
31+
/// Indicates that the workflow's execution has been suspended
32+
/// </summary>
33+
public const string Suspended = "suspended";
34+
/// <summary>
35+
/// Indicates that the workflow's execution is waiting for event(s)
3236
/// </summary>
3337
public const string Waiting = "waiting";
3438
/// <summary>
@@ -44,4 +48,19 @@ public static class WorkflowInstanceStatusPhase
4448
/// </summary>
4549
public const string Faulted = "faulted";
4650

51+
/// <summary>
52+
/// Gets a new <see cref="IEnumerable{T}"/> containing all workflow instance status phases
53+
/// </summary>
54+
/// <returns>A new <see cref="IEnumerable{T}"/> containing all workflow instance status phases</returns>
55+
public static IEnumerable<string> AsEnumerable()
56+
{
57+
yield return Pending;
58+
yield return Running;
59+
yield return Suspended;
60+
yield return Waiting;
61+
yield return Completed;
62+
yield return Cancelled;
63+
yield return Faulted;
64+
}
65+
4766
}

src/operator/Synapse.Operator/Services/WorkflowInstanceHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected virtual async Task<ServiceAccount> GetServiceAccountAsync(Cancellation
170170
/// <returns>A boolean indicating whether or not the handler should resume the execution of the handled workflow instance</returns>
171171
protected virtual bool ShouldResumeExecution(string? statusPhase)
172172
{
173-
if (statusPhase == WorkflowInstanceStatusPhase.Waiting)
173+
if (statusPhase == WorkflowInstanceStatusPhase.Suspended)
174174
{
175175
this._suspended = true;
176176
return false;

src/runner/Synapse.Runner/Services/RunnerApplication.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected virtual async Task OnHandleStatusPhaseChangedAsync(string? phase, Canc
124124
{
125125
switch (phase)
126126
{
127-
case WorkflowInstanceStatusPhase.Waiting:
127+
case WorkflowInstanceStatusPhase.Suspended:
128128
await this.OnSuspendAsync(cancellationToken).ConfigureAwait(false);
129129
break;
130130
case WorkflowInstanceStatusPhase.Cancelled:

src/runner/Synapse.Runner/Services/WorkflowExecutionContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,11 @@ public virtual async Task<TaskInstance> SkipAsync(TaskInstance task, object? res
698698
/// <inheritdoc/>
699699
public virtual async Task SuspendAsync(CancellationToken cancellationToken = default)
700700
{
701-
if (this.Instance.Status?.Phase == WorkflowInstanceStatusPhase.Waiting) return;
701+
if (this.Instance.Status?.Phase == WorkflowInstanceStatusPhase.Suspended) return;
702702
using var @lock = await this.Lock.LockAsync(cancellationToken).ConfigureAwait(false);
703703
var originalInstance = this.Instance.Clone();
704704
this.Instance.Status ??= new();
705-
this.Instance.Status.Phase = WorkflowInstanceStatusPhase.Waiting;
705+
this.Instance.Status.Phase = WorkflowInstanceStatusPhase.Suspended;
706706
var run = this.Instance.Status.Runs?.LastOrDefault();
707707
if (run != null) run.EndedAt = DateTimeOffset.Now;
708708
var jsonPatch = JsonPatchUtility.CreateJsonPatchFromDiff(originalInstance, this.Instance);

src/runner/Synapse.Runner/Services/WorkflowExecutor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public virtual async Task ExecuteAsync(CancellationToken cancellationToken = def
108108
await this.StartAsync(this.CancellationTokenSource.Token).ConfigureAwait(false);
109109
break;
110110
case WorkflowInstanceStatusPhase.Running:
111-
case WorkflowInstanceStatusPhase.Waiting:
111+
case WorkflowInstanceStatusPhase.Suspended:
112112
await this.ResumeAsync(this.CancellationTokenSource.Token).ConfigureAwait(false);
113113
break;
114114
default:

tests/Synapse.UnitTests/Services/MockWorkflowInstanceApiClient.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task SuspendAsync(string name, string @namespace, CancellationToken
3737
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Running) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
3838
var updated = original.Clone()!;
3939
updated.Status ??= new();
40-
updated.Status.Phase = WorkflowInstanceStatusPhase.Waiting;
40+
updated.Status.Phase = WorkflowInstanceStatusPhase.Suspended;
4141
var jsonPatch = JsonPatchUtility.CreateJsonPatchFromDiff(original, updated);
4242
await this.Resources.PatchStatusAsync<WorkflowInstance>(new(PatchType.JsonPatch, jsonPatch), name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false);
4343
}
@@ -49,7 +49,7 @@ public async Task ResumeAsync(string name, string @namespace, CancellationToken
4949
var workflowInstanceReference = new ResourceReference<WorkflowInstance>(name, @namespace);
5050
var original = await this.Resources.GetAsync<WorkflowInstance>(name, @namespace, cancellationToken).ConfigureAwait(false)
5151
?? throw new ProblemDetailsException(new(ProblemTypes.NotFound, ProblemTitles.NotFound, (int)HttpStatusCode.NotFound, ProblemDescriptions.ResourceNotFound.Format(workflowInstanceReference.ToString())));
52-
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Waiting) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
52+
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Suspended) throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
5353
var updated = original.Clone()!;
5454
updated.Status ??= new();
5555
updated.Status.Phase = WorkflowInstanceStatusPhase.Running;
@@ -64,7 +64,7 @@ public async Task CancelAsync(string name, string @namespace, CancellationToken
6464
var workflowInstanceReference = new ResourceReference<WorkflowInstance>(name, @namespace);
6565
var original = await this.Resources.GetAsync<WorkflowInstance>(name, @namespace, cancellationToken).ConfigureAwait(false)
6666
?? throw new ProblemDetailsException(new(ProblemTypes.NotFound, ProblemTitles.NotFound, (int)HttpStatusCode.NotFound, ProblemDescriptions.ResourceNotFound.Format(workflowInstanceReference.ToString())));
67-
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Pending && original.Status?.Phase != WorkflowInstanceStatusPhase.Running && original.Status?.Phase != WorkflowInstanceStatusPhase.Waiting)
67+
if (original.Status?.Phase != WorkflowInstanceStatusPhase.Pending && original.Status?.Phase != WorkflowInstanceStatusPhase.Running && original.Status?.Phase != WorkflowInstanceStatusPhase.Suspended)
6868
throw new ProblemDetailsException(new(ProblemTypes.AdmissionFailed, ProblemTitles.AdmissionFailed, (int)HttpStatusCode.BadRequest, $"The workflow instance '{workflowInstanceReference}' is in an expected phase '{original.Status?.Phase}'"));
6969
var updated = original.Clone()!;
7070
updated.Status ??= new();

0 commit comments

Comments
 (0)