These are chat archives for petabridge/akka-bootcamp

22nd
Feb 2017
Nick Chamberlain
@heynickc
Feb 22 2017 16:48
@FOstyn I did some digging and besides being able to pass in different configurations on the startup of the Actor System, configuration is immutable
@webafriq_twitter I'll take a look, is this Akka Bootcamp code or your own?
@webafriq_twitter just asking so maybe I can reproduce more quickly
babafemi
@webafriq_twitter
Feb 22 2017 16:52
Yeah mine.
@heynickc . Mine
Nick Chamberlain
@heynickc
Feb 22 2017 16:53
ok, just noticed @mwardm's response and I think we're on the right track with that, what do you think?
babafemi
@webafriq_twitter
Feb 22 2017 16:53
@heynickc . Though, @mwardm , gave me something that helped me be better at it. Solved it. I think now i am doing even more complex behaviour with your support.
Recently i have been battling with persistence. I was trying crud with akka.persistence, but i was only able to solve create and read, but i have not been able to get update and delete.Need some heads up. Thanks in advance.
Nick Chamberlain
@heynickc
Feb 22 2017 16:57
is this related to your previous question?
involving SaveSnapshot?
babafemi
@webafriq_twitter
Feb 22 2017 16:57
@heynickc , solve that, thanks to your support.
Nick Chamberlain
@heynickc
Feb 22 2017 16:58
glad to hear it!
babafemi
@webafriq_twitter
Feb 22 2017 16:58
This is a different issue. I have a role based system where the admin creates roles and permissions.
As user logs in, as in normal oop, the roles are saved in session, now because i am deployingt in the cloud and i dont want to use redis, i have chosen to save inmemory.
so my issue is, after saving inmemory, i have issues with updating the saved role, permissions.
@heynickc , thats my issue.
Nick Chamberlain
@heynickc
Feb 22 2017 17:01
can you be more specific about your issues?
is it an issue with using Akka.Persistence to update an Actor, in memory or not?
babafemi
@webafriq_twitter
Feb 22 2017 17:02
when using imemory persistence with Sqlserver for my jurnal, i can successfully read, and add, but i am not able to delete and update.
the issue is not updating the object inmemory, but reflecting the updated object in the journal in sqlserver.
Nick Chamberlain
@heynickc
Feb 22 2017 17:04
ok, can you show me code you're trying to use to do so?
babafemi
@webafriq_twitter
Feb 22 2017 17:05
public class PersistAgentStore : ReceivePersistentActor
{
private List<CreateSuperAgentMessage> _superAgentMessageStore = new List<CreateSuperAgentMessage>();
private int _msgsSinceLastSnapshot = 0;
public override string PersistenceId { get; } = "SuperAgentStoreActor";
public PersistAgentStore()
{
// recover
Recover<CreateSuperAgentMessage>(str => _superAgentMessageStore.Add(str)); // from the journal
Recover<SnapshotOffer>(offer =>
{
var superAgents = offer.Snapshot as List<CreateSuperAgentMessage>;
if (superAgents != null) // null check
_superAgentMessageStore = superAgents;
});
        //All SuperAgents Commands
        Command<CreateSuperAgentMessage>(str => Persist(str, s =>
        {
            _superAgentMessageStore.Add(str);
            if (++_msgsSinceLastSnapshot % 100 == 0)
            {
                SaveSnapshot(_superAgentMessageStore);
            }
        }));

        Command<SaveSnapshotSuccess>(success =>
        {
            // soft-delete the journal up until the sequence # at
            // which the snapshot was taken
            DeleteMessages(success.Metadata.SequenceNr);
        });

        Command<EditSuperAgentMessage>(str => Persist(str, s =>
        {
            var superagents = _superAgentMessageStore.Where(x => x.Id == str.Id).Select(x => x).FirstOrDefault();
            superagents = new CreateSuperAgentMessage(str.Id, str.Name, str.OfficeAddress1, str.OfficeAddress2, str.PhoneNumber1, str.PhoneNumber2,
                                                      str.ContactPersonPhoneNumber, str.WebsiteAddress, str.OfficialEmail, str.CACRegistrationDocument,
                                                      str.CbnCertificationDocument, 0, str.Result);
            _superAgentMessageStore.Add(superagents); //add msg to in-memory event store after persisting
            SaveSnapshot(_superAgentMessageStore);
        }));

        Command<GetSuperAgentsBankId>(model =>
       {
           List<CreateSuperAgentMessage> agentsByBankId = _superAgentMessageStore.Where(x => x.Id == model.Id).Select(x => x).ToList();
           Sender.Tell(agentsByBankId, Sender);
       });
    }
}
Thats it.
Nick Chamberlain
@heynickc
Feb 22 2017 17:05
did you happen to look at my response to you explaining what SaveSnapshot does?
because I believe you are trying to use that method to persist the update?
babafemi
@webafriq_twitter
Feb 22 2017 17:06
I did, but i didnt understand it. But due to the delay in respons, i just went back and kept trying but still couldnt figure it out.
There is not real example on google to help.
Nick Chamberlain
@heynickc
Feb 22 2017 17:07
in essence, SaveSnapshot is infrastructure code - it's there to speed up Recovery
I like to think of snapshotting as event consolidation
babafemi
@webafriq_twitter
Feb 22 2017 17:07
ok. Got that.
Nick Chamberlain
@heynickc
Feb 22 2017 17:08
so that won't necessarily create a new event for updating the entity, right?
babafemi
@webafriq_twitter
Feb 22 2017 17:08
Yeah.
It there means in this code:
        Command<EditSuperAgentMessage>(str => Persist(str, s =>
        {
            var superagents = _superAgentMessageStore.Where(x => x.Id == str.Id).Select(x => x).FirstOrDefault();
            superagents = new CreateSuperAgentMessage(str.Id, str.Name, str.OfficeAddress1, str.OfficeAddress2, str.PhoneNumber1, str.PhoneNumber2,
                                                      str.ContactPersonPhoneNumber, str.WebsiteAddress, str.OfficialEmail, str.CACRegistrationDocument,
                                                      str.CbnCertificationDocument, 0, str.Result);
            _superAgentMessageStore.Add(superagents); //add msg to in-memory event store after persisting
            SaveSnapshot(_superAgentMessageStore);
        }));
if i take savesnapshot out, it will only update the inmemory object, then how will it then reflect in the journal?
so both locatins are in sync (inmemory and sqlserver), like model and database in sync with migrations.
Nick Chamberlain
@heynickc
Feb 22 2017 17:12
yes if you look at the sample code here http://getakka.net/docs/persistence/persistent-actors
babafemi
@webafriq_twitter
Feb 22 2017 17:14
opening it.
Nick Chamberlain
@heynickc
Feb 22 2017 17:14
you'll notice that the in-memory ExampleState Update(Evt evt) method always appends to its internal List<string> _events
_events.Add(evt.Data)
I don't have Akka.Persistence.Sql open in front of me right now, but event better might be this example https://github.com/akkadotnet/akka.net/blob/dev/src/examples/PersistenceExample/ExamplePersistentActor.cs
babafemi
@webafriq_twitter
Feb 22 2017 17:19
From the links, are you saying there is no difference betrween update and add?
Nick Chamberlain
@heynickc
Feb 22 2017 17:19
yep
it is "append-only"
like a bank account
instead of deleting your bank account balance and replacing it with a new balance, we're simply appending a new transaction
babafemi
@webafriq_twitter
Feb 22 2017 17:21
so how then does my scenario work, the role managent i mentioned earlier works?
or how does a shopping cart scenario work?
Nick Chamberlain
@heynickc
Feb 22 2017 17:24
so conceptually, you might send the actor a command that says create new admin sending a admin created event to the event store => event #1
babafemi
@webafriq_twitter
Feb 22 2017 17:24
ok, go on.
Nick Chamberlain
@heynickc
Feb 22 2017 17:25
then you have a give admin super role command, sending a admin given super role to the event store => event #2
then you have a take away admin super role command, sending a admin taken role away event to the event store => event #3
if you wanted to know what the state of the admin right now is, you replay them in sequence: event #1, event #2, event #3
in functional programming this would be a "left fold"
babafemi
@webafriq_twitter
Feb 22 2017 17:26
Question:
What if i make a mistake in the admins name, and i do an update what happens? is a new admin creatd with the new name? If yes, what then happens to the previous admin created.
Nick Chamberlain
@heynickc
Feb 22 2017 17:28
event name changed to Bobby => event #4 oops... event name changed to Bob => event #5
1,2,3,4,5 results in the current name of the admin being Bob
no new admin if the identifier for that admin stays the same
and Recovery should handle that for you
making any more sense?
babafemi
@webafriq_twitter
Feb 22 2017 17:31
my data is exhausted, can we have a private chat once i reload?
Yeah it is. I think this part is my most difficult part to grasp in akka.net.
Nick Chamberlain
@heynickc
Feb 22 2017 17:31
sure no problem