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)) diff --git a/django_q/management/commands/qcluster.py b/django_q/management/commands/qcluster.py index a1ac799..14e312c 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,7 +10,16 @@ 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 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..570a3ba 100644 --- a/django_q/management/commands/qmonitor.py +++ b/django_q/management/commands/qmonitor.py @@ -1,15 +1,23 @@ # Django +from optparse import make_option + 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") + 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() - - + 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..3fc1dde --- /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=True) + + +def test_qmonitor(): + call_command('qmonitor', run_once=True)