Django-Celery - 从管理页面重新提交任务?

时间:2021-11-08 19:18:36

I'm currently working on a project using Django 1.3 with the Django-Celery app. It's pretty awesome, by the way, I suggest anyone who's not familiar with celery check it out.

我目前正在使用Django 1.3和Django-Celery应用程序开展项目。顺便说一句,这非常棒,我建议任何不熟悉芹菜的人都会检查出来。

I have a specific question around the admin page functionality:

我对管理页面功能有一个具体问题:

We're using celery tasks to make RESTful API calls to third party partners. These calls are actually kicked off by a user action, so you could see how a celery task would be extremely useful in this case.

我们正在使用celery任务向第三方合作伙伴发出RESTful API调用。这些调用实际上是由用户操作启动的,因此您可以看到芹菜任务在这种情况下如何非常有用。

We have a user story around how an admin should be able to re-send a callback if it fails for some reason. Now, if the callback fails with a standard HTTP response failure, we are using the celery retry mechanism to automatically resend them at various intervals. However, these callbacks could be to one of any thousands of partners (yea, theres a lot), and not all of them will use a standard HTTP Response code as their failure response.

我们有一个用户故事,讲述管理员如果由于某种原因失败,应该如何能够重新发送回调。现在,如果回调因标准HTTP响应失败而失败,我们正在使用芹菜重试机制以不同的间隔自动重新发送它们。但是,这些回调可能是数千个合作伙伴中的一个(是的,很多),而且并非所有合作伙伴都会使用标准的HTTP响应代码作为失败响应。

Long story short, I haven't been able to find anything online that states that one can re-send a celery task through the admin interface. I was hoping that someone could shed some light on this. It seems like a pretty obvious piece of functionality to have, and if there is no such functionality I'm sure there's a good reason for it. I'd love it if there isn't a way to do it, if someone could explain the reason. Just curious to learn more about the internal workings of celery.

长话短说,我无法在网上找到任何可以通过管理界面重新发送芹菜任务的内容。我希望有人可以对此有所了解。这似乎是一个非常明显的功能,如果没有这样的功能,我相信它有充分的理由。如果没有办法,如果有人能解释原因,我会喜欢它。只是好奇了解芹菜的内部运作。

Thanks everyone! Sorry for my wordiness, sometimes I tend to ramble.

感谢大家!对不起我的啰嗦,有时我倾向于絮絮叨叨。

1 个解决方案

#1


2  

You can try two approaches,

你可以尝试两种方法,

1: Small hack in model:

1:模特中的小黑客:

You can use a boolean field and name it something like celery_retry and in models save method do something like this.

您可以使用布尔字段并将其命名为celery_retry,并在模型中保存方法执行类似此操作。

def save(self, *args, **kwargs):
    if self.celery_retry and self.user.is_superuser():
          celery_task.apply_async()
    self.celery_retry = False
    super(MyModel, self).save(*args, **kwargs)

This is just an idea, you can determine your own how and when to submit celery task back.

这只是一个想法,您可以确定自己如何以及何时提交芹菜任务。

2:Extend admin template:

2:扩展管理模板:

You can extend the admin template and put in a hyperlink to a view which resubmits the task.

您可以扩展管理模板并添加指向重新提交任务的视图的超链接。

#1


2  

You can try two approaches,

你可以尝试两种方法,

1: Small hack in model:

1:模特中的小黑客:

You can use a boolean field and name it something like celery_retry and in models save method do something like this.

您可以使用布尔字段并将其命名为celery_retry,并在模型中保存方法执行类似此操作。

def save(self, *args, **kwargs):
    if self.celery_retry and self.user.is_superuser():
          celery_task.apply_async()
    self.celery_retry = False
    super(MyModel, self).save(*args, **kwargs)

This is just an idea, you can determine your own how and when to submit celery task back.

这只是一个想法,您可以确定自己如何以及何时提交芹菜任务。

2:Extend admin template:

2:扩展管理模板:

You can extend the admin template and put in a hyperlink to a view which resubmits the task.

您可以扩展管理模板并添加指向重新提交任务的视图的超链接。