Permalink
Browse files

#8 - implemented task filtering in gearman_server_info

  • Loading branch information...
1 parent 896486c commit c3c4995244132f987832488506e200baf0bdcaa4 @jsk jsk committed Jan 6, 2013
View
@@ -12,3 +12,4 @@ logs
dist
django_gearman_commands.egg-info
.idea
+atlassian-ide-plugin.xml
View
@@ -231,11 +231,11 @@ gearman_server_info outputs current status of Gearman servers.
If you installed prettytable dependency, here is how output looks like::
$ ./manage.py gearman_server_info
- +---------------------+------------------------+
- | Gearman Server Host | Gearman Server Version |
- +---------------------+------------------------+
- | 127.0.0.1:4730 | OK 0.29 |
- +---------------------+------------------------+.
+ +---------------------+------------------------+--------------------+
+ | Gearman Server Host | Gearman Server Version | Ping Response Time |
+ +---------------------+------------------------+--------------------+
+ | 127.0.0.1:4730 | OK 1.1.3 | 0.0006051063537598 |
+ +---------------------+------------------------+--------------------+.
+---------------+---------------+--------------+-------------+
| Task Name | Total Workers | Running Jobs | Queued Jobs |
@@ -254,6 +254,28 @@ If you installed prettytable dependency, here is how output looks like::
+-----------+------------------+-----------+-----------------+
+If you have a lot of workers, you can filter output using command argument (case-sensitive)::
+
+ $ ./manage.py gearman_server_info cleanup
+ +---------------------+------------------------+--------------------+
+ | Gearman Server Host | Gearman Server Version | Ping Response Time |
+ +---------------------+------------------------+--------------------+
+ | 127.0.0.1:4730 | OK 1.1.3 | 0.0006871223449707 |
+ +---------------------+------------------------+--------------------+.
+
+ +---------------+---------------+--------------+-------------+
+ | Task Name | Total Workers | Running Jobs | Queued Jobs |
+ +---------------+---------------+--------------+-------------+
+ | cache_cleanup | 1 | 0 | 0 |
+ +---------------+---------------+--------------+-------------+.
+
+ +-----------+------------------+-----------+-----------------+
+ | Worker IP | Registered Tasks | Client ID | File Descriptor |
+ +-----------+------------------+-----------+-----------------+
+ | 127.0.0.1 | cache_cleanup | - | 37 |
+ +-----------+------------------+-----------+-----------------+
+
+
Practical production deployment with Supervisor
===============================================
@@ -1,22 +1,15 @@
# -*- coding: utf-8 -*-
-import time
import logging
-from datetime import datetime
-
import gearman
-
from django.core.management import call_command
-from django.core.management.base import BaseCommand, CommandError
-
+from django.core.management.base import BaseCommand
import django_gearman_commands.settings
__version__ = '0.2'
-
log = logging.getLogger(__name__)
-
class HookedGearmanWorker(gearman.GearmanWorker):
"""GearmanWorker with hooks support."""
@@ -109,8 +102,9 @@ def __init__(self, host):
self.tasks = None
self.workers = None
self.ping_time = None
+ self.ping_time_str = None
- def get_server_info(self):
+ def get_server_info(self, task_filter=None):
"""Read Gearman server info - status, workers and and version."""
result = ''
@@ -123,6 +117,14 @@ def get_server_info(self):
self.ping_time = client.ping_server()
self.ping_time_str = '{0:0.016f}'.format(self.ping_time)
+ # if task_filter is set, filter list of tasks and workers by regex pattern task_filter
+ if task_filter:
+ # filter tasks
+ self.tasks = [item for item in self.tasks if task_filter in item['task']]
+
+ # filter workers by registered task name
+ self.workers = [item for item in self.workers if item['tasks'] and task_filter in [t for t in item['tasks']]]
+
# sort tasks by task name
self.tasks = sorted(self.tasks, key=lambda item: item['task'])
@@ -18,8 +18,13 @@ class Command(BaseCommand):
def handle(self, *args, **options):
result = ''
+
+ task_filter = ''
+ if len(args) > 1:
+ task_filter = args[1]
+
for server in django_gearman_commands.settings.GEARMAN_SERVERS:
server_info = GearmanServerInfo(server)
- result += server_info.get_server_info()
-
+ result += server_info.get_server_info(task_filter)
+
self.stdout.write(result)
@@ -2,7 +2,6 @@
import logging
import pickle
-
from django.core.management import call_command
from django.test import TestCase
from django.core.cache import cache
@@ -23,7 +22,22 @@ def test_server_info(self):
# verify command is callable
overview = call_command('gearman_server_info')
-
+
+ def test_server_info_task_filter(self):
+ # submit job to queue so we have something shown up in get_server_info()
+ call_command('gearman_submit_job', 'footest')
+
+ server_info = GearmanServerInfo(django_gearman_commands.settings.GEARMAN_SERVERS[0])
+
+ server_info.get_server_info("footest")
+ self.assertTrue(len(server_info.tasks) > 0, 'Unexpected server_info.tasks')
+
+ server_info.get_server_info("NON_EXISTING!!")
+ self.assertTrue(len(server_info.tasks) == 0, 'Unexpected server_info.tasks')
+
+ # let the worker process the job
+ call_command('gearman_worker_footest')
+
def test_worker_simple(self):
# submit job
call_command('gearman_submit_job', 'footest')
@@ -52,4 +66,4 @@ def test_worker_task_data_pickled(self):
call_command('gearman_worker_footest')
# verify job was processed and processed task data
- self.assertEqual(pickle.loads(cache.get('footest')), u'DATA', 'Unexpected footest worker result (data pickled)')
+ self.assertEqual(pickle.loads(cache.get('footest')), u'DATA', 'Unexpected footest worker result (data pickled)')
View
@@ -32,7 +32,7 @@
# tests dependency - run gearman server on localhost on default port
GEARMAN_SERVERS=[
- '127.0.0.1:4730'
+ 'localhost:4730'
],
# LocMemCache used as a verification storage for tests
@@ -42,6 +42,8 @@
'LOCATION': 'django-gearman-commands-tests'
}
},
+
+ GEARMAN_CLIENT_NAMESPACE = 'django-gearman-commands-tests'
)
# Start the test suite now that the settings are configured.

0 comments on commit c3c4995

Please sign in to comment.