我有一个使用 Celery 设置的 Django 项目,托管在运行 Ubuntu 22.04 的 DigitalOcean Droplet 上。我遇到了一个特殊的问题,芹菜工作人员收到任务但似乎没有执行它。这是我尝试运行的任务(由 django-cookiecutter 提供):
from django.contrib.auth import get_user_model
from config import celery_app
User = get_user_model()
@celery_app.task()
def get_users_count():
return User.objects.count()
我正在使用以下命令运行工作人员:
celery -A config.celery_app worker --loglevel=DEBUG --without-gossip --without-mingle --without-heartbeat -Ofair --pool=solo
我正在通过 django shell 手动执行任务:
get_users_count.delay()
<AsyncResult: 821982b7-f256-462b-869c-e415da168c3f>
worker日志显示它收到了任务,但没有执行:
[2023-10-06 23:57:25,568: INFO/MainProcess] Task myapp.users.tasks.get_users_count[821982b7-f256-462b-869c-e415da168c3f] received
任务出现在 Redis 中:
127.0.0.1:6379> keys *
1) "_kombu.binding.celery.pidbox"
2) "celery-task-meta-821982b7-f256-462b-869c-e415da168c3f"
3) "_kombu.binding.celery"
当我手动查询任务状态和结果时,显示任务确实成功了:
>>> res = AsyncResult("821982b7-f256-462b-869c-e415da168c3f")
>>> print("Task State: ", res.state)
Task State: SUCCESS
>>> print("Task Result: ", res.result)
Task Result: 2
尽管 AsyncResult 显示成功,但工作日志中没有指示任务已执行。这里可能出了什么问题?任何指导将不胜感激。与我的本地计算机和该服务器唯一真正不同的是我使用 requirepass 来保护 redis。
celery==5.3.1
django-celery-beat==2.5.0
Django==4.2.3
编辑:
还有一些令人费解的事情,在记录消息后它出现了,但仍然没有显示任务已执行:
@celery_app.task()
def get_users_count():
"""A pointless Celery task to demonstrate usage."""
logger.info("get_users_count task executed")
return User.objects.count()
[2023-10-07 00:55:11,000: INFO/MainProcess] mingle: searching for neighbors
[2023-10-07 00:55:12,012: INFO/MainProcess] mingle: all alone
[2023-10-07 00:55:16,573: INFO/MainProcess] Task myapp.users.tasks.get_users_count[c02f6522-2d42-4721-8600-d1c6426e67d0] received
[2023-10-07 00:55:16,577: INFO/ForkPoolWorker-2] get_users_count task executed
编辑2:
计数显示正确的数字:
@celery_app.task()
def get_users_count():
"""A pointless Celery task to demonstrate usage."""
logger.info("get_users_count task executed")
count = User.objects.count()
logger.info(f"user count is {count}")
return count
[2023-10-07 05:09:37,810: INFO/MainProcess] Task myapp.users.tasks.get_users_count[474736b2-d568-45f6-ae34-f512612cca2f] received
[2023-10-07 05:09:37,811: INFO/MainProcess] get_users_count task executed
[2023-10-07 05:09:37,821: INFO/MainProcess] user count is 2
[2023-10-07 05:11:01,741: INFO/MainProcess] Task myapp.users.tasks.get_users_count[61a3db97-712a-4fff-bb20-7d33e9e52aeb] received
[2023-10-07 05:11:01,742: INFO/MainProcess] get_users_count task executed
[2023-10-07 05:11:01,749: INFO/MainProcess] user count is 3
所以一切都正常工作,问题是,无论出于何种原因,芹菜都没有在生产设置中添加为记录器。我不确定创作者是否故意包含此内容,我们应该将其禁用。
修复:
LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s",
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
}
},
"root": {"level": "INFO", "handlers": ["console"]},
"loggers": {
"celery": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
},
// other loggers here