|
1 | 1 | from django.db import transaction |
2 | | -from django.core.management.base import NoArgsCommand |
| 2 | +from django.core.management.base import BaseCommand, CommandError |
3 | 3 | from django.utils.module_loading import import_by_path |
4 | 4 | from django_dbq.models import Job |
| 5 | +from optparse import make_option |
5 | 6 | from simplesignals.process import WorkerProcessBase |
6 | 7 | from time import sleep |
7 | 8 | import logging |
|
10 | 11 | logger = logging.getLogger(__name__) |
11 | 12 |
|
12 | 13 |
|
| 14 | +DEFAULT_QUEUE_NAME = 'default' |
| 15 | + |
| 16 | + |
13 | 17 | def process_job(queue_name): |
14 | 18 | """This function grabs the next available job for a given queue, and runs its next task.""" |
15 | 19 |
|
@@ -64,17 +68,32 @@ def do_work(self): |
64 | 68 | process_job(self.queue_name) |
65 | 69 |
|
66 | 70 |
|
67 | | -class Command(NoArgsCommand): |
| 71 | +class Command(BaseCommand): |
68 | 72 |
|
69 | 73 | help = "Run a queue worker process" |
70 | 74 |
|
71 | | - def handle_noargs(self, **options): |
72 | | - self.handle('default') |
| 75 | + option_list = BaseCommand.option_list + ( |
| 76 | + make_option('--dry-run', |
| 77 | + action='store_true', |
| 78 | + dest='dry_run', |
| 79 | + default=False, |
| 80 | + help="Don't actually start the worker. Used for testing."), |
| 81 | + ) |
73 | 82 |
|
74 | 83 | def handle(self, *args, **options): |
| 84 | + if not args: |
| 85 | + args = (DEFAULT_QUEUE_NAME,) |
| 86 | + |
75 | 87 | if len(args) != 1: |
76 | 88 | raise CommandError("Please supply a single queue job name") |
77 | 89 |
|
78 | | - logger.info("Starting job worker") |
79 | | - worker = Worker() |
| 90 | + queue_name = args[0] |
| 91 | + |
| 92 | + self.stdout.write("Starting job worker for queue \"%s\"" % queue_name) |
| 93 | + |
| 94 | + worker = Worker(queue_name) |
| 95 | + |
| 96 | + if options['dry_run']: |
| 97 | + return |
| 98 | + |
80 | 99 | worker.run() |
0 commit comments