是否有可能將django-celery作爲virtualenv中的項目的systemd服務進行守護進程?django-celery作爲virtualenv中的systemd服務
這裏是我的配置:
/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
這裏提到的所有目錄確實存在,並正確設置
Django的芹菜權限在settings.py中:
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
以下命令正常啓動芹菜,我可以看到正在執行任務:
python manage.py celery worker --loglevel=INFO -Q availability,celery
這基本上是一樣的,在服務的conf指定,但它發佈到標準輸出
然而,當我嘗試systemctl start celery.service
,它只是失敗默默:systemctl status celery.service
報告無效(死亡)
我很感激這個問題的任何提示。我可能會丟失一些東西很明顯,但我有一種感覺,這個過程不應該那麼複雜,因爲它現在((
UPDATE
芹菜日誌告訴,芹菜正常啓動,但對於某些原因的sysctl不接受它下面是從芹菜日誌,--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] [email protected] 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不明白,工人實際上是正常的話,並且關閉它:
Job for celery.service failed because a timeout was exceeded. See "systemctl status celery.service" and "journalctl -xe" for details.
[email protected]:~$ 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'.
注:我已經減少了工人的NUM一個爲簡單起見,和編輯conf文件。這使問題更清楚一點(這不是芹菜問題,但可能在systemd中),但我仍然不知道導致此超時的原因。
你可以嘗試從'sudo journalctl -u celery.service'獲取更多日誌嗎? –
感謝您的關注,我已更新描述與日誌的詳細信息 – funkifunki
我會檢查'$ {CELERY_LOG_FILE}'會有一些提示爲什麼工人在開始後被殺死... – Dawid