Kicking off an Oban job in the same transaction as an action

What's the right way to kick off an oban job in the same transaction as for example creating an ash?
20 Replies
rohan
rohanOP3y ago
GitHub
ash_hq/thread.ex at b94b4dfeb5b1a5f5f995efe36bff7aa4bb493fb2 · ash-...
The Ash Framework homepage and documentation site. - ash_hq/thread.ex at b94b4dfeb5b1a5f5f995efe36bff7aa4bb493fb2 · ash-project/ash_hq
ZachDaniel
ZachDaniel3y ago
Yep! So unless you say otherwise ( transaction? false in action definition), all create/update/destroy actions will open a transaction by default. As long as you are using the same repo between oban and your resources, then it should "just work" in an after action or before action hook. before/after action hooks will run in the same transaction as the parent
rohan
rohanOP3y ago
is that the right idiom to use for something like kicking off oban tasks? as in - before/after action hooks
ZachDaniel
ZachDaniel3y ago
yes, absolutely.
rohan
rohanOP3y ago
awesome the other thing I'm a little confused about is whether before action hooks run before validations it seems like they run after allow_nil gets checked on attributes - is that right?
ZachDaniel
ZachDaniel3y ago
correct. before_action hooks happen "just before" the action is actually committed, after validation and things like that.
rohan
rohanOP3y ago
is there another hook that runs before the validations
ZachDaniel
ZachDaniel3y ago
https://ash-hq.org/docs/guides/ash/latest/topics/actions#create-update-destroy-actions The lifecycle is laid out in that guide. When you say validations, do you mean validate in the resource/action? Or do you mean allow_nil? being validated? I assume you're setting some required attribute in a before_action hook or something like that?
rohan
rohanOP3y ago
yup it's a slightly different use case but essentially I'm doing a file upload (from a binary) and then grabbing the url (which is required in the db) after that I think after reading the lifecycle I just need a regular old change , not a before_action
ZachDaniel
ZachDaniel3y ago
If you are doing a file upload, I'd suggest doing it in a before_action hook or a before_transaction hook The way to think about it is that an action's changes might be run many times potentially (like if used with AshPhoenix.Form)
rohan
rohanOP3y ago
ah I see the issue i'm having with the before_action hook is that the file_url shouldn't be nil
ZachDaniel
ZachDaniel3y ago
To do what you want, though, there are a few different options
create :create do
allow_nil_input [:file_url]
end
create :create do
allow_nil_input [:file_url]
end
rohan
rohanOP3y ago
ah!
ZachDaniel
ZachDaniel3y ago
allow_nil_input says "this thing that is normally required is not required" However, you can also just define the inputs that are "accepted" by the action. In 3.0 this will be a requirement actually. However, until then, the default behavior remains which is that all actions accept all public, writable attributes.
create :create do
accept []

argument :file_data, :binary do
allow_nil? false
end
end
create :create do
accept []

argument :file_data, :binary do
allow_nil? false
end
end
If an input isn't accepted by an action, we assume that you will set it at some point in your action (but validate just prior to submission still)
rohan
rohanOP3y ago
got it that seems better than allow_nil_input which could potentially allow nil input through if I mess up my before_action in some way
ZachDaniel
ZachDaniel3y ago
I agree that it is better because it makes the action more clearly defined, but allow_nil_input only allows nil input, not nil values in the actual action submission.
rohan
rohanOP3y ago
oh ok that makes sense haha thanks!
ZachDaniel
ZachDaniel3y ago
👍 My pleasure
rohan
rohanOP3y ago
ash seems super powerful but it's taking me a while to grok it reminds me "fat models" in rails which i was a fan of.. write everything only once
ZachDaniel
ZachDaniel3y ago
Yep! I have lots of problems with ruby/rails from an execution standpoint, but ultimately the goal is to have singular definitions that lots of behavior can be derived from. And the bigger/more variated your system is, the more that pays off. I've kicked off the "Ash Primer" youtube series, so maybe I'll do next weeks video on the action lifecycle 🙂

Did you find this page helpful?