| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
from django.core.management.base import BaseCommand |
| 5 |
from rq.logutils import setup_loghandlers |
| 6 |
from rq.serializers import resolve_serializer |
| 7 |
from rq.worker_pool import WorkerPool |
| 8 |
|
| 9 |
from ...jobs import get_job_class |
| 10 |
from ...queues import get_queues |
| 11 |
from ...utils import reset_db_connections |
| 12 |
from ...workers import get_worker_class |
| 13 |
|
| 14 |
|
| 15 |
class Command(BaseCommand): |
| 16 |
""" |
| 17 |
Runs RQ pool with x number of workers on specified queues. |
| 18 |
Note that all queues passed into a |
| 19 |
single rqworker-pool command must share the same connection. |
| 20 |
|
| 21 |
Example usage: |
| 22 |
python manage.py rqworker-pool high medium low --num-workers 4 |
| 23 |
""" |
| 24 |
|
| 25 |
args = '<queue queue ...>' |
| 26 |
|
| 27 |
def add_arguments(self, parser): |
| 28 |
parser.add_argument( |
| 29 |
'--num-workers', action='store', dest='num_workers', type=int, default=1, help='Number of workers to spawn' |
| 30 |
) |
| 31 |
parser.add_argument('--worker-class', action='store', dest='worker_class', help='RQ Worker class to use') |
| 32 |
parser.add_argument( |
| 33 |
'--pid', action='store', dest='pid', default=None, help='PID file to write the worker`s pid into' |
| 34 |
) |
| 35 |
parser.add_argument( |
| 36 |
'--burst', action='store_true', dest='burst', default=False, help='Run worker in burst mode' |
| 37 |
) |
| 38 |
parser.add_argument('--queue-class', action='store', dest='queue_class', help='Queues class to use') |
| 39 |
parser.add_argument('--job-class', action='store', dest='job_class', help='Jobs class to use') |
| 40 |
parser.add_argument( |
| 41 |
'--serializer', |
| 42 |
action='store', |
| 43 |
default='rq.serializers.DefaultSerializer', |
| 44 |
dest='serializer', |
| 45 |
help='Specify a custom Serializer.', |
| 46 |
) |
| 47 |
parser.add_argument('args', nargs='*', type=str, help='The queues to work on, separated by space') |
| 48 |
|
| 49 |
# Args present in `rqworker` command but not yet implemented here |
| 50 |
# parser.add_argument('--worker-ttl', action='store', type=int, |
| 51 |
# dest='worker_ttl', default=420, |
| 52 |
# help='Default worker timeout to be used') |
| 53 |
# parser.add_argument('--max-jobs', action='store', default=None, dest='max_jobs', type=int, |
| 54 |
# help='Maximum number of jobs to execute') |
| 55 |
# parser.add_argument('--with-scheduler', action='store_true', dest='with_scheduler', |
| 56 |
# default=False, help='Run worker with scheduler enabled') |
| 57 |
|
| 58 |
def handle(self, *args, **options): |
| 59 |
pid = options.get('pid') |
| 60 |
if pid: |
| 61 |
with open(os.path.expanduser(pid), "w") as fp: |
| 62 |
fp.write(str(os.getpid())) |
| 63 |
|
| 64 |
# Verbosity is defined by default in BaseCommand for all commands |
| 65 |
verbosity: int = options['verbosity'] |
| 66 |
if verbosity >= 2: |
| 67 |
logging_level = 'DEBUG' |
| 68 |
elif verbosity == 0: |
| 69 |
logging_level = 'WARNING' |
| 70 |
else: |
| 71 |
logging_level = 'INFO' |
| 72 |
setup_loghandlers(logging_level) |
| 73 |
|
| 74 |
job_class = get_job_class(options['job_class']) |
| 75 |
queues = get_queues(*args, **{'job_class': job_class, 'queue_class': options['queue_class']}) |
| 76 |
worker_class = get_worker_class(options.get('worker_class', None)) |
| 77 |
serializer = resolve_serializer(options['serializer']) |
| 78 |
|
| 79 |
pool = WorkerPool( |
| 80 |
queues=queues, |
| 81 |
connection=queues[0].connection, |
| 82 |
num_workers=options['num_workers'], |
| 83 |
serializer=serializer, |
| 84 |
worker_class=worker_class, |
| 85 |
job_class=job_class, |
| 86 |
) |
| 87 |
# Close any opened DB connection before any fork. |
| 88 |
reset_db_connections() |
| 89 |
pool.start(burst=options.get('burst', False), logging_level=logging_level) |