Django 在超时时安排任务

发布于 2024-11-15 01:08:52 字数 189 浏览 2 评论 0原文

我在数据库中的实体上设置了超时,并为其分配了状态(活动/已完成)。我想要的是在超时到期时将该实体的状态更改为完成。我正在考虑使用 celery 创建一个计划任务,并在对象创建时关联超时,这反过来会触发 django 信号来通知该对象已“过期”,之后我将在信号处理程序。尽管如此,这似乎有点开销,我认为必须有一种更直接的方法来做到这一点。

先感谢您。

I have a time-out set on an entity in my database, and a state (active/finished) assigned to it. What I want is to change that entity's state to finished when that time-out expires. I was thinking of using celery to create a scheduled task with that associated time-out on object creation, which in turn would trigger a django signal to notify that the object has 'expired' and after that I would set the value to finished in the signal handler. Still, this seems like a bit of an overhead, and I am thinking that there must be a more straight-forward way to do this.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-11-22 01:08:52

不一定是轻量级的,但当我面临这个问题时,我有两种解决方案。

首先,我编写了一个 Django 管理器,它将创建一个“即将过期”对象的查询集,然后删除它们。为了使这个更轻松,我将“事件过期”对象保留在它们自己的表中,并与实际对象建立一对一的关系,并删除了它们要保留的这些事件那张桌子很小。当您取消引用外键字段时,“即将过期”对象和被标记为“过期”的对象之间的关系只会导致第二个表上的数据库命中,因此它相当轻量。然后,我将每 5 分钟使用 cron(Unix 的调度管理器,如果您不熟悉 Unix)调用该管理调用。这对于每小时左右的超时来说是很好的。

对于更接近秒的超时,我的解决方案是运行一个单独的服务器,通过来自 Django 应用程序的 REST 调用接收超时通知。它保留一个超时发生时间的排序列表,然后调用前面提到的管理调用。它基本上是一个自己的调度程序,由 Django 进程向其提供调度事件。为了降低成本,我使用 Node.js 编写了它。

这两种方法都有效。 cron 工作要容易得多。

Not necessarily light-weight, but when I was faced with this problem I had two solutions.

For the first, I wrote a Django manager that would create a queryset of "to be expired" objects and then delete them. To make this lighter, I kept the "to be expired on event" objects in their own table with a one-to-one relationship to the actual objects, and deleted these events they're done to keep that table small. The relationship between the "to be expired" object and the object being marked "expired" only causes a database hit on the second table when you dereference the ForeignKey field, so it's fairly lightweight. I would then call that management call every 5 minutes with cron (the schedule manager for Unix, if you're not familiar with Unix). This was fine for an every-hour-or-so timeout.

For more close-to-the-second timeouts, my solution was to run a separate server that receives, via REST calls from the Django app, notices of timeouts. It keeps a sorted list of when timeouts were to occur, and then calls the aforementioned management call. It's basically a scheduler of its own with scheduled events being fed to it by the Django process. To make it cheap, I wrote it using Node.js.

Both of these worked. The cron job is far easier.

寄意 2024-11-22 01:08:52

如果状态在过期之前始终处于活动状态,并且之后总是完成,那么只使用“已完成”日期时间字段会更简单。过去日期时间的所有内容都将完成,未来的所有内容都将处于活动状态。除非您的问题中没有提到一些复杂的情况,否则应该提供您想要的功能,而无需任何安排。

例子:

class TaskManager(models.Manager):
    def finished(self):
        return self.filter(finish__lte=datetime.datetime.now())

    def active(self):
        return self.filter(finish__gt=datetime.datetime.now())

class Task(models.Model):
    finish = models.DateTimeField()

    def is_finished(self):
        return self.finish <= datetime.datetime.now()

If the state is always active until it's expired and always finished afterwards, it would be simpler to just have a "finished" datetime field. Everything with a datetime in the past would be finished and everything in the future would be active. Unless there is some complexity going on that is not mentioned in your question, that should provide the functionality you want without any scheduling at all.

Example:

class TaskManager(models.Manager):
    def finished(self):
        return self.filter(finish__lte=datetime.datetime.now())

    def active(self):
        return self.filter(finish__gt=datetime.datetime.now())

class Task(models.Model):
    finish = models.DateTimeField()

    def is_finished(self):
        return self.finish <= datetime.datetime.now()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文