I know Ken does not like signals. But I’m at the point in my development that I want to send messages when things are updated to mainly the Shift model:
It depends on what you mean by “send out a message”.
If you’re talking about sending emails, then regardless of the mechanism used to trigger that process, you may want to use Celery for that. (Sending emails can be a time-consuming process and so you may not want the response to be delayed for the time it takes to send those emails.)
Whether you want to do that depends upon when and how that field gets updated, and whether you can send the emails quickly enough to make the delay acceptable. (Note that signals are processed in-line - they are not a deferred processing mechanism, which means using a signal provides no benefit in this case.)
If your email infrastructure is fast enough, then you could override the save method on the model to include a call to send the email.
But in order to make an appropriate suggestion, we would need to understand all the circumstances under which you would want a message sent, what events would trigger those circumstances (forms being submitted, etc), and what sort of messaging you’re talking about. (i.e., If it is only the status field changing that would cause this message to be sent, my suggestion would likely be a lot different than if you had 20 different fields across 15 models that may all cause emails to be sent.)
Thanks for you thoughts Ken. In the first place i think of these messages:
when a status changes. 50 to 100 e-mails a day.
Update notification. Preferable push messages 500 a day
I use Postmark as emailprovider. I saw there is a Postmarker API library
What I need now is advise how to set this up especially how to trigger the events and initiate the messages.
Thanks in advance for your thoughts.
You cana still use signal in your model. But to be more effective and avoaid delays like what @KenWhitesell said, you need to
Specify what field to lookup for these notifications.
Call you notification method inside a thread to make this process separate to avoid delays
Each time you update your model you should call .save() and not .update()
update() is converted directly to an SQL statement; it doesn’t call save() on the model instances, and so the pre_save and post_save signals aren’t emitted. If you want your signal receivers to be called, you should loop over the queryset, and for each model instance, make your changes and call save() yourself.
I need to point out that starting a thread from within a view is a very bad idea. It may work 99.99% of the time, but there are situations where it will fail. Your application does not control the process in which it is being run, and so cannot guarantee that the thread will run to completion if it does not complete before the view returns its response.