'uwsgi error: "celery: not found" with celery run in a venv with attach-daemon2

If I run celery manually, from within my django app's virtual environment, it works:

(hackerspace) 90158@hackerspace:~/hackerspace/src$ celery -A hackerspace_online worker -l info -c 3 -Q default

 -------------- celery@hackerspace v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-4.4.0-151-generic-x86_64-with-Ubuntu-16.04-xenial 2019-06-27 10:19:53
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         hackerspace_online:0x7f8ecb7dbba8
- ** ---------- .> transport:   redis://127.0.0.1:6379/0
- ** ---------- .> results:     
- *** --- * --- .> concurrency: 3 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> default          exchange=default(direct) key=default


[tasks]
  . hackerspace_online.celery.debug_task
  . update_conditions_for_quest
  . update_quest_conditions_all
  . update_quest_conditions_for_user

[2019-06-27 10:19:53,439: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[2019-06-27 10:19:53,447: INFO/MainProcess] mingle: searching for neighbors
[2019-06-27 10:19:54,466: INFO/MainProcess] mingle: all alone
[2019-06-27 10:19:54,482: INFO/MainProcess] celery@hackerspace ready.

But I can't get it to run with uwsgi's attach-daemon2:

#hackerspace_uwsgi.ini
[uwsgi]
...
attach-daemon2  = cmd=%(chdir)/celery -A hackerspace_online worker -l info -c 3 -Q default

My uwsgi log gives me this error:

[uwsgi-daemons] respawning "/home/90158/hackerspace/src/celery -A hackerspace_online worker -l info -c 3 -Q default" (uid: 33 gid: 33)
/bin/sh: 1: /home/90158/hackerspace/src/celery: not found

Which seems to indicate it is not being run from within the virtual environment. How can I get uwsgi to run celery so that it works like it does when I run it manually?



Solution 1:[1]

The uwsgi docs say to try this using the smart-attach-daemon and a pidfile. celery itself recommends using multi to start workers when daemonizing:

[uwsgi]
master = true
socket = :3031
smart-attach-daemon = /tmp/celery.pid celery -A tasks worker --pidfile=/tmp/celery.pid

Solution 2:[2]

Best way I found

attach-daemon = bash -c "cd /var/www/application/ ; source /srv/nopow_link/bin/activate && celery -A main worker --loglevel=info -f /var/log/uwsgi/app/celery.log"

To log, make sure www-data or user used by uwsgi has permission to write inside.

chown www-data:www-data /var/log/uwsgi/app/celery.log
chmod 644 /var/log/uwsgi/app/celery.log

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 2ps
Solution 2 CallMarl