pub_sub event not being broadcasted

I have this action in my resource:
update :update_images do
accept [:status, :images]
end
update :update_images do
accept [:status, :images]
end
And here is my pub_sub section:
pub_sub do
module MarketplaceWeb.Endpoint

prefix "property"

publish :update, [:id], event: "property_updated"
publish :update_images, [:id], event: "property_updated"
end
pub_sub do
module MarketplaceWeb.Endpoint

prefix "property"

publish :update, [:id], event: "property_updated"
publish :update_images, [:id], event: "property_updated"
end
I would expect that if I call MyResource.update_images it would broadcast the event, but this doesn't seem to be the case. If I add this MarketplaceWeb.Endpoint.subscribe("property:#{property_id}") to a process I will not receive the "property_updated" event. Before I was using the default update action and was receiving the event normally.
25 Replies
ZachDaniel
ZachDaniel3y ago
You can use this config to potentially debug and see where the issue might be: config :ash, :pub_sub, debug?: true
Blibs
BlibsOP3y ago
I already use that, there is no debug message regarding pub_sub being broadcasted when I run that action
ZachDaniel
ZachDaniel3y ago
Interesting… Try adding a publish_all for updates and see if that fires
Blibs
BlibsOP3y ago
This will work with this action:
update :update do
primary? true

argument :uploaded_images, {:array, :map}, allow_nil?: false
argument :removed_images, {:array, :map}, allow_nil?: false

...
end
update :update do
primary? true

argument :uploaded_images, {:array, :map}, allow_nil?: false
argument :removed_images, {:array, :map}, allow_nil?: false

...
end
But it will not work with the :update_images action Seems like it only triggers if the update action is the default or primary 🤔
ZachDaniel
ZachDaniel3y ago
That sounds pretty strange how are you calling the action? can I see your code interface definition?
Blibs
BlibsOP3y ago
code_interface do
define_for Marketplace.Markets

define :create
define :update
define :destroy
define :get
define :get_any

define :update_status
define :update_images

define :read_admin
define :read
end
code_interface do
define_for Marketplace.Markets

define :create
define :update
define :destroy
define :get
define :get_any

define :update_status
define :update_images

define :read_admin
define :read
end
` And I call it here:
{:ok, _} =
Marketplace.Repo.transaction(fn ->
Property.update_images!(property, changes)

%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()
end)
{:ok, _} =
Marketplace.Repo.transaction(fn ->
Property.update_images!(property, changes)

%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()
end)
ZachDaniel
ZachDaniel3y ago
OHH 😆 So you should be getting a warning logged about this about unhandled notifications in your logs
Blibs
BlibsOP3y ago
At runtime or during compilation?
ZachDaniel
ZachDaniel3y ago
at runtime
Blibs
BlibsOP3y ago
Let me see if I can find it
ZachDaniel
ZachDaniel3y ago
Basically, notifications are meant to go out after a transaction happens, and normally this is handled by the action but if you are starting a transaction manually, there is nothing it can do
Blibs
BlibsOP3y ago
Oh, I see
ZachDaniel
ZachDaniel3y ago
So what you need to do is this:
{:ok, notifications} =
Marketplace.Repo.transaction(fn ->
notifications = Property.update_images!(property, changes)

%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()

notifications
end)
Ash.Notifier.notify(notifications)
{:ok, notifications} =
Marketplace.Repo.transaction(fn ->
notifications = Property.update_images!(property, changes)

%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()

notifications
end)
Ash.Notifier.notify(notifications)
Blibs
BlibsOP3y ago
Btw, regarding the warning, I'm not seeing it:
sezdocs@pop-os:~/projects/rebuilt/marketplace$ iex -S mix
Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

[info] Configuration :server was not enabled for MarketplaceWeb.Endpoint, http/https services won't start
Interactive Elixir (1.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [debug] Tzdata polling for update.
[debug] Tzdata polling shows the loaded tz database is up to date.
sezdocs@pop-os:~/projects/rebuilt/marketplace$ iex -S mix
Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

[info] Configuration :server was not enabled for MarketplaceWeb.Endpoint, http/https services won't start
Interactive Elixir (1.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [debug] Tzdata polling for update.
[debug] Tzdata polling shows the loaded tz database is up to date.
ZachDaniel
ZachDaniel3y ago
🤔 interesting
Blibs
BlibsOP3y ago
I'm gonna try that, just a sec
ZachDaniel
ZachDaniel3y ago
oh, wow, you're right We default to not showing the warning That should change
config :ash, :missed_notifications, :warn
config :ash, :missed_notifications, :warn
Blibs
BlibsOP3y ago
Ah, now I see the warning So, can I add return_notifications? directly to the update_images call or do I need to do that on the resource? I tried Property.update_images!(property, changes, return_notifications?: true) but this doesn't seem to work
ZachDaniel
ZachDaniel3y ago
You should be able to do it on the code interface call you'll need {_, notifications} = actually though
Blibs
BlibsOP3y ago
You mean like this?
{:ok, _, notifications} =
Marketplace.Repo.transaction(fn ->
%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()

Property.update_images!(property, changes, return_notifications?: true)
end)
{:ok, _, notifications} =
Marketplace.Repo.transaction(fn ->
%{property_id: property_id, removed_images: removed_images}
|> RemoveUnusedImages.new()
|> Oban.insert!()

Property.update_images!(property, changes, return_notifications?: true)
end)
ZachDaniel
ZachDaniel3y ago
well, kind of Repo.transactions returns {:ok, whatever_the_transaction_returns} So it would be {:ok, {_, notifications}}
Blibs
BlibsOP3y ago
Ahhh, I see, it returns in a tuple
ZachDaniel
ZachDaniel3y ago
Although come to think of it, I wonder if I could make a helper for this I probably could. I'll put it on my list for now you'd have to do it that way
Blibs
BlibsOP3y ago
Cool! Now it works great! Thanks a lot Zach!
ZachDaniel
ZachDaniel3y ago
My pleasure 😄

Did you find this page helpful?