gpt4 book ai didi

python - django-celery 作为 virtualenv 中的系统服务

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:11 27 4
gpt4 key购买 nike

是否可以将 django-celery 守护进程作为 virtualenv 中项目的系统服务?

这是我的配置:

/etc/systemd/system/celery.service

[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=vagrant
Group=vagrant
Restart=no
WorkingDirectory=/vagrant/myproj/
ExecStart=/bin/sh -c '/var/www/vhost/myproj_env/bin/python \
/vagrant/myproj/manage.py celery worker \
--loglevel=DEBUG \
--logfile=/var/log/celery/worker.log \
--pidfile=/var/run/celery/worker.pid \
-Q availability,celery --time-limit=300'
ExecStop=/bin/sh -c '/var/www/vhost/myproj_env/bin/python \
/vagrant/myproj/manage.py celery stop \
--pidfile=/var/run/celery/worker.pid'

[Install]
WantedBy=multi-user.target

这里提到的所有目录都存在,并且权限设置正确

settings.py 中的 django-celery:

INSTALLED_APPS = (
...
'djcelery',
'celery_haystack',
...
)

import djcelery
djcelery.setup_loader()

CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = BROKER_URL
CELERY_REDIS_MAX_CONNECTIONS = 30

下面的命令可以正常启动celery,可以看到正在执行的任务:

python manage.py celery worker --loglevel=INFO -Q availability,celery

这与服务 conf 中指定的基本相同,除了它发布到 stdout

但是,当我尝试 systemctl start celery.service 时,它只是默默地失败了:systemctl status celery.service 报告不活动(已死)

如能就此问题提供任何提示,我将不胜感激。我可能遗漏了一些明显的东西,尽管我觉得这个过程不应该像现在这样复杂 ((

更新

celery 日志告诉我们,celery 正常启动,但出于某种原因 sysctl 不接受它。这是来自 celery 日志的片段 --loglevel=DEBUG:

[2017-07-09 21:55:09,435: DEBUG/MainProcess] | Worker: Preparing bootsteps.
[2017-07-09 21:55:09,439: DEBUG/MainProcess] | Worker: Building graph...
[2017-07-09 21:55:09,439: DEBUG/MainProcess] | Worker: New boot order: {Beat, Timer, Hub, Queues (intra), Pool, Autoreloader, StateDB, Autoscaler, Consumer}
...
[2017-07-09 21:55:10,696: DEBUG/MainProcess] ^-- substep ok
[2017-07-09 21:55:10,696: DEBUG/MainProcess] | Consumer: Starting Heart
[2017-07-09 21:55:10,697: DEBUG/MainProcess] ^-- substep ok
[2017-07-09 21:55:10,697: DEBUG/MainProcess] | Consumer: Starting event loop
[2017-07-09 21:55:10,697: WARNING/MainProcess] /var/www/vhost/myproj_env/local/lib/python2.7/site-packages/djcelery/loaders.py:130: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warn('Using settings.DEBUG leads to a memory leak, never '
[2017-07-09 21:55:10,698: WARNING/MainProcess] celery@vagrant ready.
[2017-07-09 21:55:10,698: DEBUG/MainProcess] | Worker: Hub.register Pool...
[2017-07-09 21:55:10,699: DEBUG/MainProcess] basic.qos: prefetch_count->4

它甚至还在处理队列中的任务!

但是大约 30 秒后,sysctl 无法理解 worker 实际上正在正常运行,并将其关闭:

Job for celery.service failed because a timeout was exceeded. See "systemctl status celery.service" and "journalctl -xe" for details.

vagrant@vagrant:~$ sudo systemctl status celery.service
● celery.service - Celery Service
Loaded: loaded (/etc/systemd/system/celery.service; enabled; vendor preset: enabled)
Active: failed (Result: timeout) since Sun 2017-07-09 21:56:38 UTC; 3min 0s ago
Process: 3139 ExecStart=/bin/sh -c /var/www/vhost/myproj_env/bin/python /vagrant/myproj/manage.py c
Tasks: 0
Memory: 47.4M
CPU: 1.532s

Jul 09 21:55:08 vagrant systemd[1]: Starting Celery Service...
Jul 09 21:56:38 vagrant systemd[1]: celery.service: Start operation timed out. Terminating.
Jul 09 21:56:38 vagrant systemd[1]: Failed to start Celery Service.
Jul 09 21:56:38 vagrant systemd[1]: celery.service: Unit entered failed state.
Jul 09 21:56:38 vagrant systemd[1]: celery.service: Failed with result 'timeout'.

注意:为简单起见,我已将工作人员数量减少到一个,并编辑了 conf 文件。这使问题更清楚了(这不是 celery 中的问题,但可能是 systemd 中的问题),但我仍然不知道是什么导致了这个超时..

最佳答案

设法通过从 /etc/systemd/system/celery.service 中删除 Type=forking 来修复它

关于python - django-celery 作为 virtualenv 中的系统服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com