![]() If theĬhangeset is invalidated for some reason, the email ends up never being sent, resulting in an atomic execution of both Using Bamboo, so as not to run within the transaction itself). The result is that an email is sent (asynchronously, This transaction will use the same transition_changeset/4 from above to compute the necessary changes to the modelĪnd will include the new callback as an call. It also hasĪn /3 function that allows you to run arbitrary code within that same transaction.įsmx in turn, integrates with Ecto.Multi by providing you with a way to run state transitions as part of anĮcto.Multi, while also providing you an additional callback that is executed in that case:ĭoor = PersistedDoor |> Repo. ![]() But you want these two operations to be a single, atomic operation.Įcto deals with atomicity via Ecto.Multi which groups multiple operations inside a database transaction. Let's say, for example, you want to get notified every time someone unlocks your door. It's one thing to transition the state machine itself and move forward with the state.īut another big benefit of state machines is the ability to deal with particular side effects that come out of each You can pattern match on those to add specific conditions for each transition. def transition (changeset, _from, "opened", params) do changeset |> cast (params, ) |> validate_acceptance ( :terms_and_conditions ) end endįsmx looks for an optional transition_changeset/4 function in your schema and calls it with the previous state as Otherwise, an error is returned (due to the last, catch-all match of the code snippet).ĭefmodule PersistedDoor do #. To use this process, you can call it with the desired transition that you want to execute. Is kept for any additional state that is needed, but we're not using that in this case. The current state with the transition requested and perform the required transformations. By sending appropriate events, we are able to match This implements a process that starts out in the :locked state. start_link ( _MODULE_, :ok, ) end :gen_statem def init ( _ ), do: end end We could model this as a finite-state machine, such as the following:ĭefmodule Door do :gen_statem def start_link do :gen_statem. e.g: Tasks start out in the backlog, can be assigned to people, moved to "in progress", and later to "done"įor this post, we'll stick with a small example that illustrates the flow of a state machine: A door.Ī door can be locked or unlocked.
0 Comments
Leave a Reply. |