我正在开发一个 Django 博客,我需要能够安排帖子在以后发布。Celery 非常适合最初安排帖子,但是当用户尝试更新帖子以使其重新安排或无限期取消时,我遇到了问题。
这是我正在尝试做的事情:
def save(self, **kwargs):
'''
Saves an event. If the event is currently scheduled to publish,
sets a celery task to publish the event at the selected time.
If there is an existing scheduled task,cancel it and reschedule it
if necessary.
'''
import celery
this_task_id = 'publish-post-%s' % self.id
celery.task.control.revoke(task_id=this_task_id)
if self.status == self.STATUS_SCHEDULED:
from blog import tasks
tasks.publish_post.apply_async(args=[self.id], eta=self.date_published,
task_id=this_task_id)
else:
self.date_published = datetime.now()
super(Post, self).save(**kwargs)
问题是,一旦 Celery 任务 ID 被列为已撤销,即使我尝试重新安排它,它仍会保持撤销状态。这似乎是一项足够常见的任务,应该有一个简单的解决方案。