使django应用程序在特定时间通知人们

Making django app notify people at specific time

Short description

如何让我的应用程序在事件创建一段时间后向一组人员发送通知?

Long description

我的网站允许 Bob 放置他想在线销售的东西。好几个人看到都想买。如果 6 小时后没有人感兴趣,则应通过电子邮件通知卖家。

Solution I found that I think dirty

运行 crontab 每分钟检查人们不感兴趣的整篇文章。缺点:不太准确+扫描整个数据库+在我的应用程序外部。 ..

有更好的解决方案吗?

作为选项 1,您可以编写将由 crontab 使用的 custom management command

选项 2(实际上也与 cron 工作相关)是使用 Celery 或类似工具(在你的情况下我更喜欢选项 1)。

请随时提问,我会更新我的回答

您要找的是https://pypi.python.org/pypi/django-cron/0.3.6

本质上,您在 INSTALLED_APPS 中使用 django_cron 作为

INSTALLED_APPS = (
    'django_cron',)

通过在项目根目录中创建 cron.py 来设置 cron 作业

class SixHourEmailUpdate(CronJobBase):
    code = 'six_hour_reminder_cron'
    schedule = Schedule( run_every_mins = 360 ) # 6 hours

    def do(self):
         # Your email sending 

此外,您还必须设置一个 cron 作业,每隔一段时间 运行。

*/5 * * * * bash /home/tester/my_cron/cron.sh

cron.sh反过来调用

python /home/tester/app/my_app/manage.py runcrons

这里有更多文档 http://django-cron.readthedocs.org/en/latest/sample_cron_configurations.html#run-at-times-feature