Broadcasting pubsub update notifications only if the resource has been changed?
seven_seacat:
At the moment a broadcast is made after the action is successfully performed - even if nothing on the record actually changed.
My scenario is having a background worker that checks validity of some credentials in a loop periodically, in an action like so:
update :update_credential_validity do
change set_attribute(:invalid, true)
change set_attribute(:invalid, false), where: [{Vendor.CredentialValidation, []}]
end
A resource with valid credentials will have
invalid: false
and that attribute won’t change, but I still get a pubsub notification after each check.
It’s not causing any real problems, but a) the logs are super noisy with all the extra unnecessary pubsub notifications being sent around, and b) unnecessary work is being done by the recipients of the broadcast that doesn’t need to be. (eg. templates might re-render, or in my case the background worker presence is verified, and started if its not already).
Is there some way we could configure pubsub for updates to not broadcast unless something was actually updated?
zachdaniel:
Hmm…I’m wondering if perhaps that should be the default behavior. We won’t always be able to know, i.e for atomic operations though. So perhaps it should be action by action configuration
seven_seacat:
it seems like a sensible default to me, but it would definitely be a breaking change if anyone was relying on the old behaviour. i can’t think of an example of when the current behaviour would be wanted though.
for atomic operations (or any case where it can’t be known if something happened or not) we should probably default to sending the notification
zachdaniel:
Yeah, agreed. Which we should be able to do 👍
zachdaniel:
Come to think of it though, I think people probably are relying on this
zachdaniel:
i.e with an action like
add_comment
zachdaniel:
they are likely relying on getting the parent notification. You could monitor for a comment created, or for the
add_comment
action being called.
zachdaniel:
So I think we’re going to have to do this slightly differently. We could put it in the action:
update :update do
notify_with_no_changes? true
end
or in the pub sub notifier
update :action, "foo:bar" do
only_when_changed? true
end
thoughts?
seven_seacat:
ah right, if you had an update action that doesn’t touch the resource itself, but uses hooks to do related stuff, you still want to know about it. that makes sense
seven_seacat:
I think configuring it in the notifier would be better
zachdaniel:
Agreed. Could you open an issue in Ash? I’ll see if I can add it this week 🙂
seven_seacat:
issue created https://github.com/ash-project/ash/issues/647 so I’ll close this thread