mirror of
https://github.com/django-q2/django-q2.git
synced 2026-09-21 01:18: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.utils.translation import ugettext as _
|
||||
|
||||
from django_q.monitor import monitor
|
||||
from django_q.monitor import monitor, info
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
@@ -17,7 +17,16 @@ class Command(BaseCommand):
|
||||
dest='run_once',
|
||||
default=False,
|
||||
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):
|
||||
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
|
||||
import signing
|
||||
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()
|
||||
r = redis_client
|
||||
try:
|
||||
redis_client.ping()
|
||||
except Exception as e:
|
||||
print(term.red('Can not connect to Redis server.'))
|
||||
logger.exception(e)
|
||||
raise e
|
||||
ping_redis(r)
|
||||
with term.fullscreen(), term.hidden_cursor(), term.cbreak():
|
||||
val = None
|
||||
start_width = int(term.width / 8)
|
||||
@@ -88,7 +83,6 @@ def monitor(run_once=False):
|
||||
|
||||
|
||||
class Status(object):
|
||||
|
||||
"""Cluster status base class."""
|
||||
|
||||
def __init__(self, pid):
|
||||
@@ -107,7 +101,6 @@ class Status(object):
|
||||
|
||||
|
||||
class Stat(Status):
|
||||
|
||||
"""Status object for Cluster monitoring."""
|
||||
|
||||
def __init__(self, sentinel):
|
||||
@@ -194,3 +187,54 @@ class Stat(Status):
|
||||
state = dict(self.__dict__)
|
||||
del state['r']
|
||||
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():
|
||||
call_command('qcluster', run_once=True)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_qmonitor():
|
||||
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.monitor import monitor, Stat
|
||||
from django_q.monitor import monitor, Stat, info, ping_redis
|
||||
|
||||
|
||||
def test_monitor():
|
||||
@@ -17,3 +20,10 @@ def test_monitor():
|
||||
assert stat.empty_queues() is True
|
||||
break
|
||||
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