mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-06 12:48:12 +08:00
adds --info summary to monitor command
This commit is contained in:
@@ -4,7 +4,7 @@ from optparse import make_option
|
|||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
from django_q.monitor import monitor
|
from django_q.monitor import monitor, info
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
@@ -17,7 +17,16 @@ class Command(BaseCommand):
|
|||||||
dest='run_once',
|
dest='run_once',
|
||||||
default=False,
|
default=False,
|
||||||
help='Run once and then stop.'),
|
help='Run once and then stop.'),
|
||||||
|
make_option('-i', '--info',
|
||||||
|
action='store_true',
|
||||||
|
dest='info',
|
||||||
|
default=False,
|
||||||
|
help='Lists general information over all clusters. ')
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
monitor(run_once=options.get('run_once', False))
|
if options.get('info', False):
|
||||||
|
info()
|
||||||
|
else:
|
||||||
|
monitor(run_once=options.get('run_once', False))
|
||||||
|
|
||||||
|
|||||||
+54
-10
@@ -10,17 +10,12 @@ from django.utils.translation import ugettext as _
|
|||||||
# local
|
# local
|
||||||
import signing
|
import signing
|
||||||
from django_q.conf import Conf, redis_client, logger
|
from django_q.conf import Conf, redis_client, logger
|
||||||
|
from django_q import models
|
||||||
|
|
||||||
|
|
||||||
def monitor(run_once=False):
|
def monitor(run_once=False, r=redis_client):
|
||||||
term = Terminal()
|
term = Terminal()
|
||||||
r = redis_client
|
ping_redis(r)
|
||||||
try:
|
|
||||||
redis_client.ping()
|
|
||||||
except Exception as e:
|
|
||||||
print(term.red('Can not connect to Redis server.'))
|
|
||||||
logger.exception(e)
|
|
||||||
raise e
|
|
||||||
with term.fullscreen(), term.hidden_cursor(), term.cbreak():
|
with term.fullscreen(), term.hidden_cursor(), term.cbreak():
|
||||||
val = None
|
val = None
|
||||||
start_width = int(term.width / 8)
|
start_width = int(term.width / 8)
|
||||||
@@ -88,7 +83,6 @@ def monitor(run_once=False):
|
|||||||
|
|
||||||
|
|
||||||
class Status(object):
|
class Status(object):
|
||||||
|
|
||||||
"""Cluster status base class."""
|
"""Cluster status base class."""
|
||||||
|
|
||||||
def __init__(self, pid):
|
def __init__(self, pid):
|
||||||
@@ -107,7 +101,6 @@ class Status(object):
|
|||||||
|
|
||||||
|
|
||||||
class Stat(Status):
|
class Stat(Status):
|
||||||
|
|
||||||
"""Status object for Cluster monitoring."""
|
"""Status object for Cluster monitoring."""
|
||||||
|
|
||||||
def __init__(self, sentinel):
|
def __init__(self, sentinel):
|
||||||
@@ -194,3 +187,54 @@ class Stat(Status):
|
|||||||
state = dict(self.__dict__)
|
state = dict(self.__dict__)
|
||||||
del state['r']
|
del state['r']
|
||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
||||||
|
def info(r=redis_client):
|
||||||
|
term = Terminal()
|
||||||
|
ping_redis(r)
|
||||||
|
stat = Stat.get_all(r)
|
||||||
|
clusters = len(stat)
|
||||||
|
workers = 0
|
||||||
|
reincarnations = 0
|
||||||
|
for cluster in stat:
|
||||||
|
workers += len(cluster.workers)
|
||||||
|
reincarnations += cluster.reincarnations
|
||||||
|
term.clear_eos()
|
||||||
|
col_width = int(term.width / 6)
|
||||||
|
print(term.black_on_green(term.center(_('-- {} clusters summary --').format(Conf.PREFIX))))
|
||||||
|
print(term.cyan(_('Clusters')) +
|
||||||
|
term.move_x(1 * col_width) +
|
||||||
|
term.white(str(clusters)) +
|
||||||
|
term.move_x(2 * col_width) +
|
||||||
|
term.cyan(_('Workers')) +
|
||||||
|
term.move_x(3 * col_width) +
|
||||||
|
term.white(str(workers)) +
|
||||||
|
term.move_x(4 * col_width) +
|
||||||
|
term.cyan(_('Restarts')) +
|
||||||
|
term.move_x(5 * col_width) +
|
||||||
|
term.white(str(reincarnations))
|
||||||
|
)
|
||||||
|
print(term.cyan(_('Queued')) +
|
||||||
|
term.move_x(1 * col_width) +
|
||||||
|
term.white(str(r.llen(Conf.Q_LIST))) +
|
||||||
|
term.move_x(2 * col_width) +
|
||||||
|
term.cyan(_('Successes')) +
|
||||||
|
term.move_x(3 * col_width) +
|
||||||
|
term.white(str(models.Success.objects.count())) +
|
||||||
|
term.move_x(4 * col_width) +
|
||||||
|
term.cyan(_('Failures')) +
|
||||||
|
term.move_x(5 * col_width) +
|
||||||
|
term.white(str(models.Failure.objects.count()))
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def ping_redis(r):
|
||||||
|
try:
|
||||||
|
r.ping()
|
||||||
|
except Exception as e:
|
||||||
|
term = Terminal()
|
||||||
|
print(term.red('Can not connect to Redis server.'))
|
||||||
|
logger.exception(e)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from django.core.management import call_command
|
|||||||
def test_qcluster():
|
def test_qcluster():
|
||||||
call_command('qcluster', run_once=True)
|
call_command('qcluster', run_once=True)
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
def test_qmonitor():
|
def test_qmonitor():
|
||||||
call_command('qmonitor', run_once=True)
|
call_command('qmonitor', run_once=True)
|
||||||
|
call_command('qmonitor', info=True)
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import pytest
|
||||||
|
import redis
|
||||||
|
|
||||||
from django_q.cluster import Cluster
|
from django_q.cluster import Cluster
|
||||||
from django_q.monitor import monitor, Stat
|
from django_q.monitor import monitor, Stat, info, ping_redis
|
||||||
|
|
||||||
|
|
||||||
def test_monitor():
|
def test_monitor():
|
||||||
@@ -17,3 +20,10 @@ def test_monitor():
|
|||||||
assert stat.empty_queues() is True
|
assert stat.empty_queues() is True
|
||||||
break
|
break
|
||||||
assert found_c is True
|
assert found_c is True
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_ping_redis():
|
||||||
|
r = redis.StrictRedis(port=6388)
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
ping_redis(r)
|
||||||
|
|||||||
Reference in New Issue
Block a user