-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#7 continue to implement the Saga for review category status
- Loading branch information
1 parent
3014360
commit 32e1e4b
Showing
7 changed files
with
192 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/Cik.Magazine.CategoryService/Sagas/CategoryProcessManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using Akka; | ||
using Akka.Actor; | ||
using Akka.Event; | ||
using Akka.Persistence; | ||
using Akka.Persistence.Fsm; | ||
using Cik.Magazine.CategoryService.Domain; | ||
using Cik.Magazine.Shared; | ||
using Cik.Magazine.Shared.Messages.Category; | ||
using Status = Cik.Magazine.Shared.Messages.Category.Status; | ||
|
||
namespace Cik.Magazine.CategoryService.Sagas | ||
{ | ||
public class CategoryProcessManager : PersistentFSM<Status, CategoryState, Event> | ||
{ | ||
private readonly Guid _id; | ||
private readonly IActorRef _commander; | ||
private readonly CategoryState _state = new CategoryState(); | ||
private readonly ILoggingAdapter _log; | ||
private long LastSnapshottedVersion { get; set; } | ||
|
||
public CategoryProcessManager(Guid id, IActorRef commander) | ||
{ | ||
_id = id; | ||
_commander = commander; | ||
_log = Context.GetLogger(); | ||
|
||
StartWith(Status.Reviewing, _state); | ||
When(Status.Reviewing, (e, state) => | ||
{ | ||
if (e.FsmEvent is CategoryCreated) | ||
{ | ||
var oldEvent = (CategoryCreated)e.FsmEvent; | ||
return GoTo(Status.Published) | ||
.Applying(new CategoryStatusUpdated(oldEvent.AggregateId, Status.Published)); | ||
} | ||
|
||
return state; | ||
}); | ||
When(Status.Published, (e, state) => | ||
{ | ||
// TODO: do the actions like send email to notify or something else | ||
return state; | ||
}); | ||
} | ||
|
||
public override string PersistenceId => $"category-process-manager-{_id}"; | ||
|
||
protected override bool ReceiveCommand(object message) | ||
{ | ||
return message.Match() | ||
.With<IEvent>(@event => | ||
{ | ||
Persist(@event, e => { }); | ||
}).WasHandled; | ||
} | ||
|
||
protected override bool ReceiveRecover(object message) | ||
{ | ||
return message.Match() | ||
.With<CategoryCreated>(@event => | ||
{ | ||
_state.Apply(@event); | ||
}) | ||
.With<RecoveryCompleted>(() => | ||
{ | ||
_log.Debug("[PM] Recovered state to version {0}", LastSequenceNr); | ||
}) | ||
.With<SnapshotOffer>(offer => | ||
{ | ||
LastSnapshottedVersion = offer.Metadata.SequenceNr; | ||
}).WasHandled; | ||
} | ||
|
||
protected override CategoryState ApplyEvent(Event e, CategoryState data) | ||
{ | ||
if (e is CategoryStatusUpdated) | ||
{ | ||
return data; | ||
} | ||
return data; | ||
} | ||
|
||
protected override void OnRecoveryCompleted() | ||
{ | ||
|
||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Cik.Magazine.CategoryService/Sagas/ExampleProcessManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using Akka.Actor; | ||
using Status = Cik.Magazine.Shared.Messages.Category.Status; | ||
|
||
namespace Cik.Magazine.CategoryService.Sagas | ||
{ | ||
public class Data | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public Status Status { get; set; } | ||
} | ||
|
||
public class ExampleProcessManager : FSM<Status, Data> | ||
{ | ||
public ExampleProcessManager() | ||
{ | ||
var initData = new Data | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "Sample", | ||
Status = Status.Reviewing | ||
}; | ||
|
||
StartWith(Status.Reviewing, initData); | ||
|
||
When(Status.Reviewing, @event => | ||
{ | ||
Console.WriteLine("Run Reviewing"); | ||
if (@event.StateData.Status == Status.Reviewing) | ||
{ | ||
@event.StateData.Status = Status.Published; | ||
return GoTo(Status.Published) | ||
.Using(initData); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Already Reviewed."); | ||
} | ||
return null; | ||
}, TimeSpan.FromSeconds(5)); | ||
|
||
When(Status.Published, @event => | ||
{ | ||
Console.WriteLine("Run Published"); | ||
if (@event.StateData.Status == Status.Published) | ||
{ | ||
@event.StateData.Status = Status.Reviewing; | ||
return GoTo(Status.Reviewing) | ||
.Using(initData); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Change back to Review."); | ||
} | ||
return null; | ||
}, TimeSpan.FromSeconds(5)); | ||
|
||
OnTransition((a, b) => | ||
{ | ||
|
||
}); | ||
|
||
Initialize(); | ||
|
||
} | ||
|
||
protected override bool Receive(object message) | ||
{ | ||
return base.Receive(message); | ||
} | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
src/Cik.Magazine.CategoryService/Sagas/ReviewCategorySaga.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters