| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
from django.core.management.base import BaseCommand |
| 5 |
from redis.exceptions import ConnectionError |
| 6 |
from rq.logutils import setup_loghandlers |
| 7 |
|
| 8 |
from ...utils import reset_db_connections |
| 9 |
from ...workers import get_worker |
| 10 |
|
| 11 |
|
| 12 |
class Command(BaseCommand): |
| 13 |
""" |
| 14 |
Runs RQ workers on specified queues. Note that all queues passed into a |
| 15 |
single rqworker command must share the same connection. |
| 16 |
|
| 17 |
Example usage: |
| 18 |
python manage.py rqworker high medium low |
| 19 |
""" |
| 20 |
|
| 21 |
args = '<queue queue ...>' |
| 22 |
|
| 23 |
def add_arguments(self, parser): |
| 24 |
parser.add_argument('--worker-class', action='store', dest='worker_class', help='RQ Worker class to use') |
| 25 |
parser.add_argument( |
| 26 |
'--pid', action='store', dest='pid', default=None, help='PID file to write the worker`s pid into' |
| 27 |
) |
| 28 |
parser.add_argument( |
| 29 |
'--burst', action='store_true', dest='burst', default=False, help='Run worker in burst mode' |
| 30 |
) |
| 31 |
parser.add_argument( |
| 32 |
'--with-scheduler', |
| 33 |
action='store_true', |
| 34 |
dest='with_scheduler', |
| 35 |
default=False, |
| 36 |
help='Run worker with scheduler enabled', |
| 37 |
) |
| 38 |
parser.add_argument('--name', action='store', dest='name', default=None, help='Name of the worker') |
| 39 |
parser.add_argument('--queue-class', action='store', dest='queue_class', help='Queues class to use') |
| 40 |
parser.add_argument('--job-class', action='store', dest='job_class', help='Jobs class to use') |
| 41 |
parser.add_argument( |
| 42 |
'--worker-ttl', |
| 43 |
action='store', |
| 44 |
type=int, |
| 45 |
dest='worker_ttl', |
| 46 |
default=420, |
| 47 |
help='Default worker timeout to be used', |
| 48 |
) |
| 49 |
parser.add_argument( |
| 50 |
'--max-jobs', |
| 51 |
action='store', |
| 52 |
default=None, |
| 53 |
dest='max_jobs', |
| 54 |
type=int, |
| 55 |
help='Maximum number of jobs to execute', |
| 56 |
) |
| 57 |
parser.add_argument( |
| 58 |
'--max-idle-time', |
| 59 |
action='store', |
| 60 |
default=None, |
| 61 |
dest='max_idle_time', |
| 62 |
type=int, |
| 63 |
help='Seconds to wait for job before shutting down', |
| 64 |
) |
| 65 |
parser.add_argument( |
| 66 |
'--serializer', |
| 67 |
action='store', |
| 68 |
default='rq.serializers.DefaultSerializer', |
| 69 |
dest='serializer', |
| 70 |
help='Specify a custom Serializer.', |
| 71 |
) |
| 72 |
parser.add_argument('args', nargs='*', type=str, help='The queues to work on, separated by space') |
| 73 |
|
| 74 |
def handle(self, *args, **options): |
| 75 |
pid = options.get('pid') |
| 76 |
if pid: |
| 77 |
with open(os.path.expanduser(pid), "w") as fp: |
| 78 |
fp.write(str(os.getpid())) |
| 79 |
|
| 80 |
# Verbosity is defined by default in BaseCommand for all commands |
| 81 |
verbosity = options['verbosity'] |
| 82 |
if verbosity >= 2: |
| 83 |
level = 'DEBUG' |
| 84 |
elif verbosity == 0: |
| 85 |
level = 'WARNING' |
| 86 |
else: |
| 87 |
level = 'INFO' |
| 88 |
setup_loghandlers(level) |
| 89 |
|
| 90 |
try: |
| 91 |
# Instantiate a worker |
| 92 |
worker_kwargs = { |
| 93 |
'worker_class': options['worker_class'], |
| 94 |
'queue_class': options['queue_class'], |
| 95 |
'job_class': options['job_class'], |
| 96 |
'name': options['name'], |
| 97 |
'worker_ttl': options['worker_ttl'], |
| 98 |
'serializer': options['serializer'], |
| 99 |
} |
| 100 |
w = get_worker(*args, **worker_kwargs) |
| 101 |
|
| 102 |
# Close any opened DB connection before any fork |
| 103 |
reset_db_connections() |
| 104 |
|
| 105 |
w.work( |
| 106 |
burst=options.get('burst', False), |
| 107 |
with_scheduler=options.get('with_scheduler', False), |
| 108 |
logging_level=level, |
| 109 |
max_jobs=options['max_jobs'], |
| 110 |
max_idle_time=options['max_idle_time'], |
| 111 |
) |
| 112 |
except ConnectionError as e: |
| 113 |
self.stderr.write(str(e)) |
| 114 |
sys.exit(1) |