Django run tasks (possibly) in the far future

后端 未结 1 1146
孤城傲影
孤城傲影 2021-02-20 12:07

Suppose I have a model Event. I want to send a notification (email, push, whatever) to all invited users once the event has elapsed. Something along the lines of:

1条回答
  •  广开言路
    2021-02-20 12:18

    We're doing something like this in the company i work for, and the solution is quite simple.

    Have a cron / celery beat that runs every hour to check if any notification needs to be sent. Then send those notifications and mark them as done. This way, even if your notification time is years ahead, it will still be sent. Using ETA is NOT the way to go for a very long wait time, your cache / amqp might loose the data.

    You can reduce your interval depending on your needs, but do make sure they dont overlap.

    If one hour is too huge of a time difference, then what you can do is, run a scheduler every hour. Logic would be something like

    1. run a task (lets call this scheduler task) hourly that gets all notifications that needs to be sent in the next hour (via celery beat) -
    2. Schedule those notifications via apply_async(eta) - this will be the actual sending

    Using that methodology would get you both of best worlds (eta and beat)

    0 讨论(0)
提交回复
热议问题