是否有允许按日期安排作业的作业队列系统?
我有一个 Django 应用程序。
我的一个模型如下所示:
class MyModel(models.Model):
def house_cleaning(self):
// cleaning up data of the model instance
每次更新 MyModel 的实例时,我都需要在 N 天后清理数据。所以我想安排一个工作,
this_instance.house_cleaning()
从现在起 N 天后打电话。
是否有任何作业队列可以让我:
- 与 Django 很好地集成 - 允许我调用单个模型实例的方法
- 只运行计划今天运行的作业
- 理想情况下可以优雅地处理故障
谢谢
I have a Django application.
One of my models looks like this:
class MyModel(models.Model):
def house_cleaning(self):
// cleaning up data of the model instance
Every time when I update an instance of MyModel, I'd need to clean up the data N days later. So I'd like to schedule a job to call
this_instance.house_cleaning()
N days from now.
Is there any job queue that would allow me to:
- Integrate well with Django - allow me to call a method of individual model instances
- Only run jobs that are scheduled to run today
- Ideally handle failures gracefully
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
django-chronograph 可能适合您的用例。如果您将清理作业编写为 django 命令,那么您可以安排它们在某个时间运行。它在后台使用 unix cron 运行。
django-chronograph might be good for your use case. If you write your cleanup jobs as django commands, then you schedule them to run at some time. It runs using unix cron behind the scene.
有什么原因导致 cron 作业不起作用吗?或者类似 django-cron 的行为方式相同? 非常容易编写独立的单独的 Django 脚本。如果您想在一定天数后对模型进行某些更改而触发房屋清洁,为什么不在模型中创建一个日期标志,将其设置为未来需要安排作业的 N 天呢?您可以每天运行一个脚本,该脚本会提取日期 <= 今天的所有记录,调用实例的
house_cleaning()
方法,然后清除日期字段。如果在此过程中出现异常,很容易记录它或发送电子邮件。Is there any reason why a cron job wouldn't work? Or something like django-cron that behaves the same way? It's pretty easy to write stand-alone Django scripts. If you want to trigger house cleaning on some change to you model after a certain number of days, why not create a date flag in the model which is set to N days in the future when the job needs to be scheduled? You could run a script on a daily basis which pulls all records where the date is <= today, calls the instance's
house_cleaning()
method and then clears the date field. If an exception is raised during the process, it's easy enough to log it or dispatch an email.