Skip to content

Commit

Permalink
Support for custom task names.
Browse files Browse the repository at this point in the history
* default value of imported function name.
* changed position of ALL_QUEUES constant (if it was after the make_option, the code failed)
  • Loading branch information
toudi committed Jul 3, 2012
1 parent 58faca5 commit addc3c1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
19 changes: 10 additions & 9 deletions README.md
Expand Up @@ -19,19 +19,11 @@ It's the same for both the client and worker instances of your django project:

Add ``django_gearman`` to the `INSTALLED_APPS` section of `settings.py`.

Specify the following settings in your local settings.py file:
Specify the following setting in your local settings.py file:

# One or more gearman servers
GEARMAN_SERVERS = ['127.0.0.1']

# gearman job name pattern. Namespacing etc goes here. This is the pattern
# your jobs will register as with the server, and that you'll need to use
# when calling them from a non-django-gearman client.
# replacement patterns are:
# %(app)s : django app name the job is filed under
# %(job)s : job name
GEARMAN_JOB_NAME = '%(app)s.%(job)s'

Workers
-------
### Registering jobs
Expand All @@ -45,6 +37,15 @@ Mark each of these functions as gearman jobs by decorating them with

For an example, look at the `gearman_example` app's `gearman_jobs.py` file.

### Job naming
The tasks are given a default name of their import path, with the phrase
'gearman_jobs' stripped out of them, for readability reasons. You can override
the task name, by specifying `name` parameter of the decorator. Here's how:

@gearman_job(name='my-task-name')
def my_task_function(foo):
pass
### Starting a worker
To start a worker, run `python manage.py gearman_worker`. It will start
serving all registered jobs.
Expand Down
17 changes: 8 additions & 9 deletions django_gearman/decorators.py
@@ -1,4 +1,4 @@
def gearman_job(queue='default'):
def gearman_job(queue='default', name=None):
"""
Decorator turning a function inside some_app/gearman_jobs.py into a
Gearman job.
Expand All @@ -8,16 +8,15 @@ class gearman_job_cls(object):

def __init__(self, f):
self.f = f
self.__name__ = f.__name__
# set the custom task name
self.__name__ = name
# if it's null, set the import name as the task name
# this also saves one line (no else clause) :)
if not name:
self.__name__ = '.'.join((f.__module__, f.__name__)) \
.replace('gearman_jobs.', '')
self.queue = queue

# Determine app name.
parts = f.__module__.split('.')
if len(parts) > 1:
self.app = parts[-2]
else:
self.app = ''

# Store function in per-app job list (to be picked up by a
# worker).
gm_module = __import__(f.__module__)
Expand Down
14 changes: 3 additions & 11 deletions django_gearman/management/commands/gearman_worker.py
Expand Up @@ -8,6 +8,7 @@


class Command(NoArgsCommand):
ALL_QUEUES = '*'
help = "Start a Gearman worker serving all registered Gearman jobs"
__doc__ = help
option_list = NoArgsCommand.option_list + (
Expand All @@ -17,7 +18,6 @@ class Command(NoArgsCommand):
default=ALL_QUEUES, help='Queue to register tasks from'),
)

ALL_QUEUES = '*'
children = [] # List of worker processes

@staticmethod
Expand Down Expand Up @@ -58,15 +58,7 @@ def handle_noargs(self, **options):
self.stdout.write("Available jobs:\n")
for job in jobs:
# determine right name to register function with
app = job.app
jobname = job.__name__
try:
func = settings.GEARMAN_JOB_NAME % {'app': app,
'job': jobname}
except NameError:
func = '%s.%s' % (app, jobname)
job.register_as = func
self.stdout.write("* %s\n" % func)
self.stdout.write("* %s\n" % job.__name__)

# spawn all workers and register all jobs
try:
Expand Down Expand Up @@ -111,7 +103,7 @@ def work(self, jobs):
"""Children only: register all jobs, start working."""
worker = GearmanWorker()
for job in jobs:
worker.register_task(job.register_as, job)
worker.register_task(job.__name__, job)
try:
worker.work()
except KeyboardInterrupt:
Expand Down

0 comments on commit addc3c1

Please sign in to comment.