Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I run django-q qcluster with supervisor process manager #196

Closed
GabLeRoux opened this issue Oct 5, 2016 · 1 comment
Closed

Comments

@GabLeRoux
Copy link
Contributor

GabLeRoux commented Oct 5, 2016

Hey there,
I like how simple it was to configure django-q :). I'm using supervisor to auto start the cluster daemon a bit like #102 . After a while (after a few deployments), I realized I had all my server memory taken by q-cluster processes. Our provisionner and deployment procedures restarts supervisor commands which explains the problem.

When I restart supervisor django-q command, it doesn't stop the cluster and instead starts a new process leaving orphan qcluster processes in the wild.

image

My supervisor configuration is quite simple:

/etc/supervisor/conf.d/django-q.conf

[program:django-q]
command = env $(cat /srv/project/.env | xargs) /usr/local/virtualenvs/project/bin/python /srv/project/manage.py qcluster
user = vagrant

I've read the following documentation:
http://django-q.readthedocs.io/en/latest/cluster.html#process-managers

There's an example config for circus which is quite simple, but we're already using supervisor for some other processes.

By default, superviosr's numprocs is set to 1 so I don't think this is the problem.
I've found the following related ServerFault question:
http://serverfault.com/questions/608069/managing-daemons-with-supervisor-no-foreground-mode-available

I tried it, but the problem still happens with the following config:

/etc/supervisor/conf.d/django-q.conf

[program:django-q]
command = /srv/project-django-q-runner.sh
user = vagrant

/srv/project-django-q-runner.sh

#!/usr/bin/env bash
set -eu

pidfile="/var/run/project-django-q.pid"
command="env $(cat /srv/project/.env | xargs) /usr/local/virtualenvs/project/bin/python /srv/project/manage.py qcluster"

# Proxy signals
function kill_app(){
    kill $(cat $pidfile)
    exit 0 # exit okay
}
trap "kill_app" SIGINT SIGTERM

# Launch daemon
$command
sleep 2

# Loop while the pidfile and the process exist
while [ -f $pidfile ] && kill -0 $(cat $pidfile) ; do
    sleep 0.5
done
exit 1000 # exit unexpected

Here's my settings.py's Q_CLUSTER just in case:

Q_CLUSTER = {
    'name': 'project',
    'workers': 1,
    'retry': 900,
    'queue_limit': 2,
    'bulk': 10,
    'orm': 'default',
}

I tried moving out the env $(cat /srv/project/.env | xargs) part, but still the same.

Anyone running django-q cluster with supervisor could share its configuration?
I'll share my mine here if I get something to work.

@GabLeRoux
Copy link
Contributor Author

Ah I think I found it.

It seems to work fine with stopasgroup = true.

Final config looks like this:
/etc/supervisor/conf.d/django-q.conf

[program:django-q]
command = /srv/project-django-q-runner.sh
user = vagrant
stopasgroup = true

/srv/project-django-q-runner.sh

#!/usr/bin/env bash

export $(cat {{api.path}}/.env | xargs) 
/usr/local/virtualenvs/project/bin/python /srv/project/manage.py qcluster

From the docs:

If true, the flag causes supervisor to send the stop signal to the whole process group and implies killasgroup is true. This is useful for programs, such as Flask in debug mode, that do not propagate stop signals to their children, leaving them orphaned.

I'll send a PR to update documentation.

GabLeRoux added a commit to GabLeRoux/django-q that referenced this issue Oct 5, 2016
Koed00 added a commit that referenced this issue Nov 28, 2016
supervisor example in Cluster documentation fix #196
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant