Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference to redis projects; remove need for json constructors in redis project; do not process saga actions on completed sagas #42

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Chronicle_" Version="3.*" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.9.2" />
<PackageReference Include="Chronicle_" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.7" />
<PackageReference Include="MongoDB.Driver" Version="2.11.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<FileVersion>2.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.7" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Chronicle_" Version="3.*" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Chronicle_" Version="3.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.7" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public async Task<IEnumerable<ISagaLogData>> ReadAsync(SagaId id, Type sagaType)
var sagaLogDatas = new List<RedisSagaLogData>();
var deserializedSagaLogDatas = new List<RedisSagaLogData>();
var cachedSagaLogDatas = await cache.GetStringAsync(LogId(id, sagaType));

if (!string.IsNullOrWhiteSpace(cachedSagaLogDatas))
{
sagaLogDatas = JsonConvert.DeserializeObject<List<RedisSagaLogData>>(cachedSagaLogDatas);
sagaLogDatas.ForEach(sld =>
{
{
var message = (sld.Message as JObject)?.ToObject(sld.MessageType);
deserializedSagaLogDatas.Add(new RedisSagaLogData(sld.Id, sld.Type, sld.CreatedAt, message, sld.MessageType));
deserializedSagaLogDatas.Add(new RedisSagaLogData { SagaId = sld.Id, Type = sld.Type, CreatedAt = sld.CreatedAt, Message = message, MessageType = sld.MessageType });
}
});
}
Expand All @@ -54,7 +54,7 @@ public async Task WriteAsync(ISagaLogData logData)
}
var sagaLogDatas = (await ReadAsync(logData.Id, logData.Type)).ToList();

var sagaLogData = new RedisSagaLogData(logData.Id, logData.Type, logData.CreatedAt, logData.Message, logData.Message.GetType());
var sagaLogData = new RedisSagaLogData { SagaId = logData.Id, Type = logData.Type, CreatedAt = logData.CreatedAt, Message = logData.Message, MessageType = logData.Message.GetType() };

sagaLogDatas.Add(sagaLogData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@ namespace Chronicle.Integrations.Redis.Persistence
{
internal sealed class RedisSagaLogData : ISagaLogData
{
public SagaId Id { get; }
public Type Type { get; }
public long CreatedAt { get; }
public object Message { get; }
public Type MessageType { get; }
public string SagaId { get; set; }
[JsonIgnore]
public SagaId Id => SagaId;
public Type Type { get; set; }
public long CreatedAt { get; set; }
public object Message { get; set; }
public Type MessageType { get; set; }

[JsonConstructor]
public RedisSagaLogData(SagaId id, Type type, long createdAt, object message, Type messageType)
{
Id = id;
Type = type;
CreatedAt = createdAt;
Message = message;
MessageType = messageType;
}

public static ISagaLogData Create(SagaId sagaId, Type sagaType, object message)
=> new RedisSagaLogData(sagaId, sagaType, DateTimeOffset.Now.ToUnixTimeMilliseconds(), message, message.GetType());
}
}
19 changes: 7 additions & 12 deletions src/Chronicle.Integrations.Redis/src/Persistence/RedisSagaState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ namespace Chronicle.Integrations.Redis.Persistence
{
internal sealed class RedisSagaState : ISagaState
{
public SagaId Id { get; }
public Type Type { get; }
public SagaStates State { get; private set; }
public object Data { get; private set; }
public Type DataType { get; }

[JsonConstructor]
public RedisSagaState(SagaId id, Type type, SagaStates state, object data = null, Type dataType = null)
=> (Id, Type, State, Data, DataType) = (id, type, state, data, dataType);

public static ISagaState Create(SagaId sagaId, Type sagaType, SagaStates state, object data = null, Type dataType = null)
=> new RedisSagaState(sagaId, sagaType, state, data, dataType);
public string SagaId { get; set; }
[JsonIgnore]
public SagaId Id => SagaId;
public Type Type { get; set; }
public SagaStates State { get; set; }
public object Data { get; set; }
public Type DataType { get; set; }

public void Update(SagaStates state, object data = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class RedisSagaStateRepository : ISagaStateRepository

public RedisSagaStateRepository(IDistributedCache cache)
=> _cache = cache;

public async Task<ISagaState> ReadAsync(SagaId sagaId, Type sagaType)
{
if (string.IsNullOrWhiteSpace(sagaId))
Expand All @@ -26,7 +26,7 @@ public async Task<ISagaState> ReadAsync(SagaId sagaId, Type sagaType)

RedisSagaState state = null;
var cachedSagaState = await _cache.GetStringAsync(StateId(sagaId, sagaType));

if (!string.IsNullOrWhiteSpace(cachedSagaState))
{
state = JsonConvert.DeserializeObject<RedisSagaState>(cachedSagaState);
Expand All @@ -42,8 +42,7 @@ public async Task WriteAsync(ISagaState state)
throw new ChronicleException($"{nameof(state)} was null.");
}

var sagaState = new RedisSagaState(state.Id, state.Type, state.State, state.Data, state.Data.GetType());

var sagaState = new RedisSagaState { SagaId = state.Id, Type = state.Type, State = state.State, Data = state.Data, DataType = state.Data.GetType() };

var serializedSagaState = JsonConvert.SerializeObject(sagaState);
await _cache.SetStringAsync(StateId(state.Id, state.Type), serializedSagaState);
Expand Down
13 changes: 8 additions & 5 deletions src/Chronicle.Tests/Chronicle.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="NSubstitute" Version="4.0.0" />
<PackageReference Include="Shouldly" Version="3.0.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

Expand Down
19 changes: 19 additions & 0 deletions src/Chronicle.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Chronicle.Tests", "Chronicl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chronicle.Integrations.MongoDB", "Chronicle.Integrations.MongoDB\src\Chronicle.Integrations.MongoDB.csproj", "{DFB344D1-3121-43F9-9E66-20BC6B0DD9CC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Chronicle.Integrations.Redis", "Chronicle.Integrations.Redis", "{3B661443-315C-4A0F-A67E-62A792AF35D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chronicle.Integrations.Redis", "Chronicle.Integrations.Redis\src\Chronicle.Integrations.Redis.csproj", "{755F7A4D-7FC3-444E-86D3-528787961B2F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,11 +73,26 @@ Global
{DFB344D1-3121-43F9-9E66-20BC6B0DD9CC}.Release|x64.Build.0 = Release|Any CPU
{DFB344D1-3121-43F9-9E66-20BC6B0DD9CC}.Release|x86.ActiveCfg = Release|Any CPU
{DFB344D1-3121-43F9-9E66-20BC6B0DD9CC}.Release|x86.Build.0 = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|x64.ActiveCfg = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|x64.Build.0 = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|x86.ActiveCfg = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Debug|x86.Build.0 = Debug|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|Any CPU.Build.0 = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|x64.ActiveCfg = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|x64.Build.0 = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|x86.ActiveCfg = Release|Any CPU
{755F7A4D-7FC3-444E-86D3-528787961B2F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F6DDA96B-847E-4F6D-86EC-126800155219}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{755F7A4D-7FC3-444E-86D3-528787961B2F} = {3B661443-315C-4A0F-A67E-62A792AF35D3}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Chronicle/Chronicle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Scrutor" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.7" />
<PackageReference Include="Scrutor" Version="3.2.2" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Chronicle/Managers/SagaCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private async Task ProcessAsync<TMessage>(TMessage message, ISagaAction<TMessage
{
var (isInitialized, state) = await _initializer.TryInitializeAsync(saga, id, message);

if (!isInitialized)
if (!isInitialized || state.State is SagaStates.Completed)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, this is useful to avoid redundant processing. Some sagas may require interaction from a user outside of the normal flow - like clicking on a link in an e-mail, which they could do multiple times. I couldn't think of a case where one would want to process a message for a saga after it has already been marked Completed.

{
return;
}
Expand Down