From 1e81cc413d7d121eb4a476217867f004f61d73e9 Mon Sep 17 00:00:00 2001 From: Ilan Date: Wed, 15 Jul 2015 22:50:53 +0200 Subject: [PATCH 1/5] Missing newline --- django_q/cluster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_q/cluster.py b/django_q/cluster.py index 3470ccb..80bbea2 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -502,4 +502,4 @@ def set_cpu_affinity(n, process_ids, actual=not Conf.TESTING): p = psutil.Process(pid) if actual: p.cpu_affinity(affinity) - logger.info('{} will use cpu {}'.format(pid, affinity)) \ No newline at end of file + logger.info('{} will use cpu {}'.format(pid, affinity)) From 5a3f875baa9419b4a48363fc2357cfaabfc47c36 Mon Sep 17 00:00:00 2001 From: Ilan Date: Thu, 16 Jul 2015 15:17:15 +0200 Subject: [PATCH 2/5] Added management commands to tests * added --run-once option to the management commands for testing --- django_q/management/commands/qcluster.py | 9 +++++++++ django_q/management/commands/qmonitor.py | 13 +++++++++---- django_q/tests/test_commands.py | 11 +++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 django_q/tests/test_commands.py diff --git a/django_q/management/commands/qcluster.py b/django_q/management/commands/qcluster.py index a1ac799..0e4e821 100644 --- a/django_q/management/commands/qcluster.py +++ b/django_q/management/commands/qcluster.py @@ -7,7 +7,16 @@ class Command(BaseCommand): # Translators: help text for qcluster management command help = _("Starts a Django Q Cluster.") + def add_arguments(self, parser): + parser.add_argument('--run-once', + action='store_true', + dest='run_once', + default=False, + help='Run once and then stop.') + def handle(self, *args, **options): q = Cluster() q.start() + if options.get('run_once', False): + q.stop() diff --git a/django_q/management/commands/qmonitor.py b/django_q/management/commands/qmonitor.py index 998271d..def5065 100644 --- a/django_q/management/commands/qmonitor.py +++ b/django_q/management/commands/qmonitor.py @@ -2,14 +2,19 @@ from django.core.management.base import BaseCommand from django.utils.translation import ugettext as _ - from django_q.monitor import monitor + class Command(BaseCommand): # Translators: help text for qmonitor management command help = _("Monitors Q Cluster activity") + def add_arguments(self, parser): + parser.add_argument('--run-once', + action='store_true', + dest='run_once', + default=False, + help='Run once and then exit.') + def handle(self, *args, **options): - monitor() - - + monitor(run_once=options.get('run_once', False)) diff --git a/django_q/tests/test_commands.py b/django_q/tests/test_commands.py new file mode 100644 index 0000000..3b517a7 --- /dev/null +++ b/django_q/tests/test_commands.py @@ -0,0 +1,11 @@ +import pytest +from django.core.management import call_command + + +@pytest.mark.django_db +def test_qcluster(): + call_command('qcluster', '--run-once') + + +def test_qmonitor(): + call_command('qmonitor', '--run-once') From 80887ffd9c2e11dbbab0790b7dec5086a2b7d50a Mon Sep 17 00:00:00 2001 From: Ilan Date: Thu, 16 Jul 2015 15:54:38 +0200 Subject: [PATCH 3/5] Django 1.7 doesn't like argparse for management commands. Changed to optparse for compatibility --- django_q/management/commands/qcluster.py | 18 ++++++++++++------ django_q/management/commands/qmonitor.py | 15 +++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/django_q/management/commands/qcluster.py b/django_q/management/commands/qcluster.py index 0e4e821..2479386 100644 --- a/django_q/management/commands/qcluster.py +++ b/django_q/management/commands/qcluster.py @@ -1,5 +1,8 @@ +from optparse import make_option + from django.core.management.base import BaseCommand from django.utils.translation import ugettext as _ + from django_q.cluster import Cluster @@ -7,16 +10,19 @@ class Command(BaseCommand): # Translators: help text for qcluster management command help = _("Starts a Django Q Cluster.") + option_list = BaseCommand.option_list + ( + make_option('--run-once', + action='store_true', + dest='run_once', + default=False, + help='Run once and then stop.'), + ) + def add_arguments(self, parser): - parser.add_argument('--run-once', - action='store_true', - dest='run_once', - default=False, - help='Run once and then stop.') + parser.add_argument('poll_id', nargs='+', type=int) def handle(self, *args, **options): q = Cluster() q.start() if options.get('run_once', False): q.stop() - diff --git a/django_q/management/commands/qmonitor.py b/django_q/management/commands/qmonitor.py index def5065..570a3ba 100644 --- a/django_q/management/commands/qmonitor.py +++ b/django_q/management/commands/qmonitor.py @@ -1,4 +1,6 @@ # Django +from optparse import make_option + from django.core.management.base import BaseCommand from django.utils.translation import ugettext as _ @@ -9,12 +11,13 @@ class Command(BaseCommand): # Translators: help text for qmonitor management command help = _("Monitors Q Cluster activity") - def add_arguments(self, parser): - parser.add_argument('--run-once', - action='store_true', - dest='run_once', - default=False, - help='Run once and then exit.') + option_list = BaseCommand.option_list + ( + make_option('--run-once', + action='store_true', + dest='run_once', + default=False, + help='Run once and then stop.'), + ) def handle(self, *args, **options): monitor(run_once=options.get('run_once', False)) From 0dbeccdfdad94adccbfbe19c3cdb095e10c10a0d Mon Sep 17 00:00:00 2001 From: Ilan Date: Thu, 16 Jul 2015 16:11:32 +0200 Subject: [PATCH 4/5] Optparse needs named arguments in call_command --- django_q/tests/test_commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_q/tests/test_commands.py b/django_q/tests/test_commands.py index 3b517a7..3fc1dde 100644 --- a/django_q/tests/test_commands.py +++ b/django_q/tests/test_commands.py @@ -4,8 +4,8 @@ from django.core.management import call_command @pytest.mark.django_db def test_qcluster(): - call_command('qcluster', '--run-once') + call_command('qcluster', run_once=True) def test_qmonitor(): - call_command('qmonitor', '--run-once') + call_command('qmonitor', run_once=True) From 91520e9ca7c4cd6cefaa9777765c5d09624cdf79 Mon Sep 17 00:00:00 2001 From: Ilan Date: Thu, 16 Jul 2015 17:26:08 +0200 Subject: [PATCH 5/5] removes some unused test code --- django_q/management/commands/qcluster.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/django_q/management/commands/qcluster.py b/django_q/management/commands/qcluster.py index 2479386..14e312c 100644 --- a/django_q/management/commands/qcluster.py +++ b/django_q/management/commands/qcluster.py @@ -18,9 +18,6 @@ class Command(BaseCommand): help='Run once and then stop.'), ) - def add_arguments(self, parser): - parser.add_argument('poll_id', nargs='+', type=int) - def handle(self, *args, **options): q = Cluster() q.start()