After-commit hook?

I'm running into a race condition issue when I insert a row to a table with the resource's create action, and the subscriber to the notification calculates the resulting row count. What's the right way to do this in Ash? Adding 'Process.sleep(1000)' seems to help but I'm hoping there is a better way. Thanks.
5 Replies
ZachDaniel
ZachDaniel2w ago
change fn changeset, _ ->
Ash.Changeset.after_action(changeset, fn changeset, result ->
# this is in the same transaction
end)
end
change fn changeset, _ ->
Ash.Changeset.after_action(changeset, fn changeset, result ->
# this is in the same transaction
end)
end
is that the kind of thing you're looking for?
Jason
JasonOP2w ago
Tried it by putting the broadcast statement inside the after_action block but still had the race condition issue. (ie. the subscriber's row count doesn't include the newly inserted row). I need a way for the broadcast to happen after the change is committed to the database.
ZachDaniel
ZachDaniel2w ago
Are you manually publishing events over pubsub? Why not use the pubsub notifier that is built in? https://hexdocs.pm/ash/Ash.Notifier.PubSub.html There are after transaction hooks too But you should prefer to use notifiers for this purpose
Jason
JasonOP2w ago
I was actually looking into it. Is there a broadcast_type that allows for an output very simple like that of 'Phoenix.PubSub.broadcast(MyApp.PubSub, "inbox:user_id", {:inbox_updated, nil})? I tried all broadcast_types but their outputs are more complex data structures like structs.
sevenseacat
sevenseacat2w ago
You would want a transform on the pubsub broadcast then https://hexdocs.pm/ash/dsl-ash-notifier-pubsub.html#pub_sub-transform

Did you find this page helpful?