Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Trax.Effect/Services/ServiceTrain/ServiceTrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ public override async Task<TOut> Run(TIn input, CancellationToken cancellationTo
await this.FinishServiceTrain(result);
await EffectRunner.SaveChanges(CancellationToken);

// Ensure output is available as serialized JSON for lifecycle hooks,
// even when SaveTrainParameters() is not configured. Runs AFTER
// SaveChanges() so it is NOT persisted to the database.
if (Metadata.Output is null)
{
var outputObject = Metadata.GetOutputObject();
if (outputObject is not null)
{
try
{
Metadata.Output = System.Text.Json.JsonSerializer.Serialize(
(object)outputObject,
Configuration
.TraxEffectConfiguration
.TraxEffectConfiguration
.StaticSystemJsonSerializerOptions
);
}
catch (Exception ex)
{
Logger?.LogDebug(
ex,
"Failed to serialize output for lifecycle hooks in train ({TrainName}).",
TrainName
);
}
}
}

await LifecycleHookRunner.OnCompleted(Metadata, CancellationToken);

try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using FluentAssertions;
using LanguageExt;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -21,6 +22,7 @@ public override ServiceProvider ConfigureServices(IServiceCollection services) =
.AddScopedTraxRoute<IThrowingOnCancelledHookTrain, ThrowingOnCancelledHookTrain>()
.AddScopedTraxRoute<IPartialOverrideTrain, PartialOverrideTrain>()
.AddScopedTraxRoute<INoOverrideTrain, NoOverrideTrain>()
.AddScopedTraxRoute<IOutputRecordingTrain, OutputRecordingTrain>()
.BuildServiceProvider();

#region OnStarted
Expand Down Expand Up @@ -241,6 +243,51 @@ public async Task Run_CancellationTokenPassedToHooks()

#endregion

#region OutputSerialization

[Test]
public async Task Run_WithoutSaveTrainParameters_OnCompletedHasSerializedOutput()
{
var train = (OutputRecordingTrain)
Scope.ServiceProvider.GetRequiredService<IOutputRecordingTrain>();

await train.Run("test-input");

train.CompletedCalled.Should().BeTrue();
train.CapturedOutput.Should().NotBeNull();
}

[Test]
public async Task Run_WithoutSaveTrainParameters_OutputContainsCorrectData()
{
var train = (OutputRecordingTrain)
Scope.ServiceProvider.GetRequiredService<IOutputRecordingTrain>();

await train.Run("test-input");

var deserialized = JsonSerializer.Deserialize<TestOutputDto>(train.CapturedOutput!);
deserialized.Should().NotBeNull();
deserialized!.Value.Should().Be("processed:test-input");
deserialized.Count.Should().Be(42);
}

[Test]
public async Task Run_WithoutSaveTrainParameters_GetOutputObjectStillAvailable()
{
var train = (OutputRecordingTrain)
Scope.ServiceProvider.GetRequiredService<IOutputRecordingTrain>();

await train.Run("test-input");

object? outputObj = train.CapturedOutputObject;
outputObj.Should().NotBeNull();
var outputDto = outputObj as TestOutputDto;
outputDto.Should().NotBeNull();
outputDto!.Value.Should().Be("processed:test-input");
}

#endregion

#region Test Trains

private interface IRecordingTrain : IServiceTrain<Unit, Unit> { }
Expand Down Expand Up @@ -438,5 +485,30 @@ protected override async Task<Either<Exception, Unit>> RunInternal(Unit input) =
Activate(input).Resolve();
}

private record TestOutputDto(
[property: System.Text.Json.Serialization.JsonPropertyName("value")] string Value,
[property: System.Text.Json.Serialization.JsonPropertyName("count")] int Count
);

private interface IOutputRecordingTrain : IServiceTrain<string, TestOutputDto> { }

private class OutputRecordingTrain : ServiceTrain<string, TestOutputDto>, IOutputRecordingTrain
{
public bool CompletedCalled { get; private set; }
public string? CapturedOutput { get; private set; }
public dynamic? CapturedOutputObject { get; private set; }

protected override async Task<Either<Exception, TestOutputDto>> RunInternal(string input) =>
new TestOutputDto($"processed:{input}", 42);

protected override Task OnCompleted(Metadata metadata, CancellationToken ct)
{
CompletedCalled = true;
CapturedOutput = metadata.Output;
CapturedOutputObject = metadata.GetOutputObject();
return Task.CompletedTask;
}
}

#endregion
}
Loading