MaatDeamon
MaatDeamon

Reputation: 9771

Akka Persistence: Where do the execution of the Command Goes when it is not simply a state update

Just for clarification: Where do the execution of a command goes, when the execution is not simply a state update (like in most examples found online)

For instance, in my case,

Now in all example that i see, it is always: (i) validating the command, then, (ii) persisting the event, and finally (iii) handling the event.

It is in handling the event that i have seen custom code added in addition to the updatestate logic. Where, the custom code is usually added after the update state function. But this custom is most of the time about sending message back to the sender, or broadcasting it to the event bus.

As per my example, it is clear that i need to do quite few operation to actually call Persist(HistoryChangeSetFetched(changeSet, time)). Indeed i need the new changeset, and the time of the newest change of it.

The only way i see it possible is to do the fetch in the validating the command

That is:

case FetchLastHistoryChangeSet => val changetuple = if ValidateCommand(FetchLastHistoryChangeSet) persit(HistoryChangeSetFetched(changetuple._1, changetuple._2)) { historyChangeSetFetched =>
  updateState(historyChangeSetFetched)
}

Where the ValidateCommand(FetchLastHistoryChangeSet)

would have as logic, to read last changeSet time (newest change of the changeSet), fetch a new changeset based on it, if it exist, get the time of its newest change, and return the tuple.

My question is, is that how it is supposed to work. Validating command can be something as complex as that ? i.e. actually executing the command ?

Upvotes: 1

Views: 63

Answers (1)

justinhj
justinhj

Reputation: 11316

As it says in the documentation: "validation can mean anything, from simple inspection of a command message's fields up to a conversation with several external services"

So I think what you're trying to do is exactly right. Any interaction with an external service must be done at the command validation stage.

Upvotes: 1

Related Questions