0

My Celery works well in CLI mode:

  • My project folder is organised this way: jive/jive.py
  • jive.py file looks like:

    app = Celery(include=['tasks.dummy_tasks','tasks.people_tasks',]) app.config_from_object(CELERY_CONFIG_PATH)

  • I run a worker in CLI this way: celery worker -A jive and it works when I'm inside the jive folder.

Recently, I tried to daemonize Celery using systemd.

For this, 2 files are required. I will paste the important part only for both:

/etc/celery/conf.d

CELERYD_NODES="w1"
CELERY_BIN="/home/user1/venv/bin/celery"
CELERY_APP="jive"
CELERYD_MULTI="multi"

/etc/systemd/system/celery.service

[Service]
Type=forking
User=user1
EnvironmentFile=-/etc/celery/conf.d
WorkingDirectory=/home/user1
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
  -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

When running the service, it fails with the following errors after displaying status:

    (venv) [user1@localhost jive]$ systemctl status celery.service
    ● celery.service - Celery Service
       Loaded: loaded (/etc/systemd/system/celery.service; disabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Tue 2017-03-07 14:59:56 CET; 2s ago
      Process: 16493 ExecStart=/bin/sh -c ${CELERY_BIN} multi start ${CELERYD_NODES}    -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE}    --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS} (code=exited, status=1/FAILURE)
Mar 07 14:59:56 localhost.localdomain sh[16493]: File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
Mar 07 14:59:56 localhost.localdomain sh[16493]: ImportError: No module named 'jive'
Mar 07 14:59:56 localhost.localdomain sh[16493]: celery multi v4.0.2 (latentcall)

AttributeError: 'module' object has no attribute 'celery' -> I suspect a PATH problem but not sure how to deal with this in a service. Thanks for your help.

David Dahan
  • 10,576
  • 11
  • 64
  • 137
  • Your `WorkingDirectory` should probably be the directory containing the `jive` folder. So if `/opt/proj/jive/jive.py`, then your `WorkingDirectory` should probably be `/opt/proj`. – Dirty Penguin Mar 07 '17 at 15:13
  • Thanks! This is probably a step in the right way, but now I get a `AttributeError: 'module' object has no attribute 'celery'` instead. – David Dahan Mar 07 '17 at 15:32
  • This question is a variation of a systemd FAQ [Difference between systemd and terminal starting program](http://unix.stackexchange.com/a/339645/20239). – Mark Stosberg Mar 07 '17 at 15:38
  • @MarkStosberg you're probably right. When running `systemctl show-environment` I got a `PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin` which differs from `echo $PATH`-> `/home/user1/venv/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user1/.local/bin:/home/user1/bin` However I think I'm not supposed to set the systemctl PATH var manually. – David Dahan Mar 07 '17 at 16:03
  • @DavidD. - Can you share the full stack trace? – Dirty Penguin Mar 07 '17 at 16:25
  • @David, it's fine to set the `PATH` you need with `Environment=PATH=...` in your unit file. – Mark Stosberg Mar 07 '17 at 16:29
  • @DirtyPenguin @MarkStosberg Actually it was just because the correct path is `/home/user1/jive` and not `/home/user1` .... I also removed the `user` part. Everything is ok now. No trouble with env var. Thanks guys for your help. – David Dahan Mar 07 '17 at 16:58

0 Answers0