create Deadline activity
This commit is contained in:
@@ -4,10 +4,10 @@ using Elsa.Workflows;
|
|||||||
using Elsa.Workflows.Attributes;
|
using Elsa.Workflows.Attributes;
|
||||||
using Elsa.Workflows.Models;
|
using Elsa.Workflows.Models;
|
||||||
|
|
||||||
namespace YourNamespace.Activities;
|
namespace Elsa.Workflows.Activities.Caspian;
|
||||||
|
|
||||||
[Activity(
|
[Activity(
|
||||||
Category = "Workflow",
|
Category = "Caspian",
|
||||||
DisplayName = "Append To InquiryResult",
|
DisplayName = "Append To InquiryResult",
|
||||||
Description = "Appends an item to WorkflowStash.InquiryResult (supports JsonElement after persistence)."
|
Description = "Appends an item to WorkflowStash.InquiryResult (supports JsonElement after persistence)."
|
||||||
)]
|
)]
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using Elsa.Extensions;
|
||||||
|
using Elsa.Workflows;
|
||||||
|
using Elsa.Workflows.Activities.Flowchart.Attributes;
|
||||||
|
using Elsa.Workflows.Attributes;
|
||||||
|
using Elsa.Workflows.Models;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Elsa.Workflows.Activities.Caspian;
|
||||||
|
|
||||||
|
[Activity(
|
||||||
|
Category = "Caspian",
|
||||||
|
DisplayName = "Deadline",
|
||||||
|
Description = "Checks whether a configured deadline has expired."
|
||||||
|
)]
|
||||||
|
[FlowNode("Valid", "Expired")]
|
||||||
|
public class Deadline : Activity
|
||||||
|
{
|
||||||
|
[Input(
|
||||||
|
Description = "Variable name used to store the start time of the deadline.",
|
||||||
|
DefaultValue = "DeadlineStartedAt"
|
||||||
|
)]
|
||||||
|
public Input<string> VariableName { get; set; } = new("DeadlineStartedAt");
|
||||||
|
|
||||||
|
[Input(
|
||||||
|
Description = "The deadline duration (example: 48:00:00).",
|
||||||
|
DefaultValue = "48:00:00"
|
||||||
|
)]
|
||||||
|
public Input<TimeSpan> Timeout { get; set; } = new(TimeSpan.FromHours(48));
|
||||||
|
|
||||||
|
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
||||||
|
{
|
||||||
|
var variableName = context.Get(VariableName);
|
||||||
|
var timeout = context.Get(Timeout);
|
||||||
|
|
||||||
|
var startedAtObject = context.GetVariable<object>(variableName);
|
||||||
|
|
||||||
|
if (startedAtObject == null)
|
||||||
|
{
|
||||||
|
context.SetVariable(variableName, DateTimeOffset.UtcNow);
|
||||||
|
await context.ScheduleOutcomesAsync("Valid");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var startedTime = ParseDateTime(startedAtObject);
|
||||||
|
var elapsed = DateTimeOffset.UtcNow - startedTime;
|
||||||
|
|
||||||
|
if (elapsed > timeout)
|
||||||
|
await context.ScheduleOutcomesAsync("Expired");
|
||||||
|
else
|
||||||
|
await context.ScheduleOutcomesAsync("Valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
private DateTimeOffset ParseDateTime(object value)
|
||||||
|
{
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
DateTimeOffset dto => dto,
|
||||||
|
DateTime dt => new DateTimeOffset(dt),
|
||||||
|
JsonElement e when e.ValueKind == JsonValueKind.String => DateTimeOffset.Parse(e.GetString()!),
|
||||||
|
string s => DateTimeOffset.Parse(s),
|
||||||
|
_ => throw new InvalidCastException($"Cannot convert {value.GetType()} to DateTimeOffset")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user