Skip to content

Commit

Permalink
Merge pull request #85 from strem-app/feature/trigger-on-flow-start-o…
Browse files Browse the repository at this point in the history
…r-finish

Have added triggers for flow starting/ending
  • Loading branch information
grofit authored Apr 20, 2023
2 parents 7c2c3fa + 2d6a946 commit a39ffa9
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@using Strem.Flows.Services.Stores
@inherits Strem.Flows.Components.Triggers.CustomTriggerComponent<Strem.Flows.Default.Flows.Triggers.Utility.OnFlowFinishedTriggerData>

@inject IFlowStore FlowStore;

<div class="field">
<label class="label">Flow</label>
<div class="control">
<DataSelectInput @bind-Value="Data.FlowId" Data="FlowStore.Data" GetName="x => x.Name" GetValue="x => x.Id" MapValue="x => Guid.Parse(x.ToString())"></DataSelectInput>
</div>
</div>
<div class="field">
<div class="control">
<CheckBox @bind-Value="Data.OnlyOnSuccessfulExecution" Label="Only When Finished Successfully?"></CheckBox>
</div>
</div>

@code {
public override string Title => GetTitle();

public string GetTitle()
{
var targetedFlow = FlowStore.Get(Data.FlowId);
var successfulSuffix = Data.OnlyOnSuccessfulExecution ? "Successfully" : "";
return $"On <strong>{targetedFlow?.Name ?? "Flow"}</strong> Finished {successfulSuffix}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@using Strem.Flows.Services.Stores
@inherits Strem.Flows.Components.Triggers.CustomTriggerComponent<Strem.Flows.Default.Flows.Triggers.Utility.OnFlowStartedTriggerData>

@inject IFlowStore FlowStore;

<div class="field">
<label class="label">Flow</label>
<div class="control">
<DataSelectInput @bind-Value="Data.FlowId" Data="FlowStore.Data" GetName="x => x.Name" GetValue="x => x.Id" MapValue="x => Guid.Parse(x.ToString())"></DataSelectInput>
</div>
</div>

@code {
public override string Title => GetTitle();

public string GetTitle()
{
var targetedFlow = FlowStore.Get(Data.FlowId);
return $"On <strong>{targetedFlow?.Name ?? "Flow"}</strong> Starting";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using Strem.Core.Events.Bus;
using Strem.Core.Extensions;
using Strem.Core.State;
using Strem.Core.Variables;
using Strem.Flows.Data.Triggers;
using Strem.Flows.Events;
using Strem.Flows.Processors;
using Strem.Flows.Services.Stores;
using Strem.Flows.Types;

namespace Strem.Flows.Default.Flows.Triggers.Utility;

public class OnFlowFinishedTrigger : FlowTrigger<OnFlowFinishedTriggerData>
{
public override string Code => OnFlowFinishedTriggerData.TriggerCode;
public override string Version => OnFlowFinishedTriggerData.TriggerVersion;

public static VariableEntry FlowNameVariableEntry = new("flow.name");

public override string Name => "On Flow Finished";
public override string Category => "Utility";
public override string Description => "Triggers whenever the matching flow has finished executing";
public override VariableDescriptor[] VariableOutputs { get; } = new[] { FlowNameVariableEntry.ToDescriptor() };

public IFlowStore FlowStore { get; }

public OnFlowFinishedTrigger(ILogger<FlowTrigger<OnFlowFinishedTriggerData>> logger, IFlowStringProcessor flowStringProcessor, IAppState appState, IEventBus eventBus, IFlowStore flowStore) : base(logger, flowStringProcessor, appState, eventBus)
{
FlowStore = flowStore;
}

public override bool CanExecute() => true;

public override async Task<IObservable<IVariables>> Execute(OnFlowFinishedTriggerData data)
{
return EventBus.Receive<FlowFinishedEvent>()
.Where(x => x.FlowId == data.FlowId)
.Where(x => MatchesExecutionState(x, data))
.Select(x =>
{
var newVariables = new Core.Variables.Variables();
var flow = FlowStore.Get(x.FlowId);
newVariables.Set(FlowNameVariableEntry, flow!.Name);
return newVariables;
});
}

private bool MatchesExecutionState(FlowFinishedEvent eventData, OnFlowFinishedTriggerData flowData)
{
if (flowData.OnlyOnSuccessfulExecution)
{ return eventData.ExecutionResultType is ExecutionResultType.Success or ExecutionResultType.FailedButContinue; }

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using Strem.Flows.Data.Triggers;

namespace Strem.Flows.Default.Flows.Triggers.Utility;

public class OnFlowFinishedTriggerData : IFlowTriggerData
{
public static readonly string TriggerCode = "on-flow-finished";
public static readonly string TriggerVersion = "1.0.0";

public Guid Id { get; set; } = Guid.NewGuid();
public string Code => TriggerCode;
public string Version { get; set; } = TriggerVersion;

[Required]
public Guid FlowId { get; set; }

public bool OnlyOnSuccessfulExecution { get; set; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Reactive.Linq;
using Microsoft.Extensions.Logging;
using Strem.Core.Events.Bus;
using Strem.Core.Extensions;
using Strem.Core.State;
using Strem.Core.Variables;
using Strem.Flows.Data.Triggers;
using Strem.Flows.Events;
using Strem.Flows.Processors;
using Strem.Flows.Services.Stores;

namespace Strem.Flows.Default.Flows.Triggers.Utility;

public class OnFlowStartedTrigger : FlowTrigger<OnFlowStartedTriggerData>
{
public override string Code => OnFlowStartedTriggerData.TriggerCode;
public override string Version => OnFlowStartedTriggerData.TriggerVersion;

public static VariableEntry FlowNameVariableEntry = new("flow.name");

public override string Name => "On Flow Started";
public override string Category => "Utility";
public override string Description => "Triggers when the matching flow starts executing";
public override VariableDescriptor[] VariableOutputs { get; } = new[] { FlowNameVariableEntry.ToDescriptor() };

public IFlowStore FlowStore { get; }

public OnFlowStartedTrigger(ILogger<FlowTrigger<OnFlowStartedTriggerData>> logger, IFlowStringProcessor flowStringProcessor, IAppState appState, IEventBus eventBus, IFlowStore flowStore) : base(logger, flowStringProcessor, appState, eventBus)
{
FlowStore = flowStore;
}

public override bool CanExecute() => true;

public override async Task<IObservable<IVariables>> Execute(OnFlowStartedTriggerData data)
{
return EventBus.Receive<FlowStartedEvent>()
.Where(x => x.FlowId == data.FlowId)
.Select(x =>
{
var newVariables = new Core.Variables.Variables();
var flow = FlowStore.Get(x.FlowId);
newVariables.Set(FlowNameVariableEntry, flow!.Name);
return newVariables;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
using Strem.Flows.Data.Triggers;

namespace Strem.Flows.Default.Flows.Triggers.Utility;

public class OnFlowStartedTriggerData : IFlowTriggerData
{
public static readonly string TriggerCode = "on-flow-started";
public static readonly string TriggerVersion = "1.0.0";

public Guid Id { get; set; } = Guid.NewGuid();
public string Code => TriggerCode;
public string Version { get; set; } = TriggerVersion;

[Required]
public Guid FlowId { get; set; }
}
4 changes: 0 additions & 4 deletions src/Strem.Flows.Default/Strem.Flows.Default.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,4 @@
<ProjectReference Include="..\Strem.Infrastructure\Strem.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="Pages\Flows.razor" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/Strem.Flows/Events/FlowFinishedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Strem.Flows.Events.Base;
using Strem.Flows.Types;

namespace Strem.Flows.Events;

public record FlowFinishedEvent(Guid FlowId) : FlowEvent(FlowId);
public record FlowFinishedEvent(Guid FlowId, ExecutionResultType ExecutionResultType) : FlowEvent(FlowId);
2 changes: 1 addition & 1 deletion src/Strem.Flows/Executors/FlowExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void RemoveFlow(Guid flowId)

public void FinishFlow(Flow flow, ExecutionResultType executionResultType, IVariables flowVariables, FlowExecutionLog executionLog, IFlowTaskData? currentTaskData = null)
{
EventBus.PublishAsync(new FlowFinishedEvent(flow.Id));
EventBus.PublishAsync(new FlowFinishedEvent(flow.Id, executionResultType));
FlowExecutionLogger.CloseLogFor(executionLog, flowVariables, executionResultType, currentTaskData);
}

Expand Down

0 comments on commit a39ffa9

Please sign in to comment.