AF
Ash Framework•2w ago
Fred

When to use Ash.Oban vs Ash.StateMachine

have a very simple pipeline that needs to be durable, i.e. automatic retry. I am porting a Python code that uses Temporal.io durable execution.
I am not sure if I have to use Oban or StateMachine, when is each more suitable?
Solution:
in general combining ash_state_machine and ash_oban to trigger state transitions is one of my favorite combinations of Ash features out there
Jump to solution
7 Replies
ZachDaniel
ZachDaniel•2w ago
Generally speaking, I would suggest using them together actually 😄 ash_state_machine is only about the rules about what transitions can be made
Solution
ZachDaniel
ZachDaniel•2w ago
in general combining ash_state_machine and ash_oban to trigger state transitions is one of my favorite combinations of Ash features out there
Fred
FredOP•2w ago
Got it. I have to look for an example of this working somewhere. I am very newbie and a lern-by-example learner.
ZachDaniel
ZachDaniel•2w ago
Yeah, this is something it would be great to have a full-blown example of TBH but I'm not sure if such a holistic example exists
ZachDaniel
ZachDaniel•2w ago
state_machine do
default_initial_state :pending
transition :send_package, from: :pending, to: :en_route
transition :delivered, from: :en_route, to: :delivered
end

actions do
create :create do
accept [:destination]
end

update :send_package do
# this action is called by the trigger
change ... # logic to actually send a package
change transition_state(:en_route)
end

update :delivered do
# maybe some API trigger does this when package is delievered
change transition_state(:delivered)
end
end

ash_oban do
triggers do
trigger :send_package do
where expr(state == :pending)
end
end
end
state_machine do
default_initial_state :pending
transition :send_package, from: :pending, to: :en_route
transition :delivered, from: :en_route, to: :delivered
end

actions do
create :create do
accept [:destination]
end

update :send_package do
# this action is called by the trigger
change ... # logic to actually send a package
change transition_state(:en_route)
end

update :delivered do
# maybe some API trigger does this when package is delievered
change transition_state(:delivered)
end
end

ash_oban do
triggers do
trigger :send_package do
where expr(state == :pending)
end
end
end
Fred
FredOP•2w ago
I can start with a state_machine and just use oban afterwards... seems a baby step.
ZachDaniel
ZachDaniel•2w ago
Yep! You can even start with a state machine with no data layer and just test transitions and then add persistence with a data layer and then add automation with ash_oban

Did you find this page helpful?