Running python manage.py run_huey in production

1k Views Asked by At

I recently deployed a django web app using Ubuntu 20.04, gunicorn and nginx, Huey was implemented which worked perfectly locally. At production, I need to run python manage.py run_huey using systemd.

I've tried the suggestion here Can't enable huey service on deploy server but not working.

My code is similar to that suggestion. I will appreciate any contribution. Thanks in advance

2

There are 2 best solutions below

0
On

I was able to solve this issue by adding Requires=gunicorn.socket to the code. Now my huey.service looks

[Unit]
Description=Huey Service
Requires=gunicorn.socket
After=redis.service

[Service]
User=deploy
Group=www-data
WorkingDirectory=/home/username/projectdir
ExecStart=/home/username/projectdir/env/bin/python manage.py run_huey
Restart=always

[Install]
WantedBy=multi-user.target

After that I ran sudo systemctl start gunicorn.sock. It worked but showing active:dead which I don't like

0
On

I eventually used supervisord to run huey I put supervisord.conf /home/user. It looks like this

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
chmod=0700                 ; socket file mode (default 0700)
chown=user:user       ; socket file uid:gid owner


[supervisord]
logfile=/home/user/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=1MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=5           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
silent=false                 ; no logs to stdout if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
childlogdir=/home/medsjoin            ; 'AUTO' child log dir, default $TEMP

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface


[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[program:huey]
command=python manage.py run_huey              ; the program (relative uses PATH, can take args)
directory=/home/user/projectdir; directory to cwd to before exec (def no cwd)
autostart=true                ; start at supervisord start (default: true)
redirect_stderr=true          ; redirect proc stderr to stdout (default false)
stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=2     ; # of stdout logfile backups (0 means none, default 10)
stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=2     ; # of stderr logfile backups (0 means none, default 10)
environment =
  DJANGO_SETTINGS_MODULE="special.settings",
  PYTHONPATH="/home/user/projectdir/special",
  PATH="/home/user/projectdir/env/bin:%(ENV_PATH)s"

I started that with supervisord.service located in /etc/systemd/system/supervisord.service

[Unit]
Description=supervisord daemon
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/local/bin/supervisord -n -c /home/user/supervisord.conf
ExecReload=/usr/local/bin/supervisorctl -c /home/user/supervisord.conf reload
ExecStop=/usr/local/bin/supervisorctl -c /home/user/supervisord.conf shutdown

[Install]
WantedBy=multi-user.target

Then start the service with

sudo systemctl start supervisord.service

Check status by

sudo systemctl status supervisord.service

You may also need to run

sudo systemctl enable supervisord

Note user rep my user, projectdir: where my django project is located, project: my django project