*django-gearman-jbox* is a convenience wrapper for the [Gearman][Gearman][Python Bindings][python-gearman].

With django-gearman-jbox, you can code workers as well as clients in a Django projectwith minimal overhead in your application. Server connections etc. all takeplace in django-gearman-jbox and don't unnecessarily clog your application code.

This library is based in large part on Fred Wenzel's [django-gearman] and Jozef Ševčík's [django-gearman-commands].- [django-gearman][django-gearman] for the 'decorator way' to create workers (very cool)- [django-gearman-commands][django-gearman-commands] for the 'gearman_server_info' command (very nice)

But there are some modifications :

Workers are now launched individually, so you have to pass 2 mandatory parameters to start the worker : - the Django App name where workers reside with the `-a` parameter - the worker's name with `-n`

The `-q` parameter is still here and has the same function than in [django-gearman].

- I removed the ability of lauching many wokers at once (`-w` parameter in [django-gearman]).I prefer having one process for each worker and Supervisord managing process. (screen is not an option)- I also added a signal handler to catch SIGTERM signals send by Supervisord and SIGINT when you worker is attached to the console.This gives you the possibility of executing code just before the worker terminates.(See `django_gearman_jbox\management\commands\gearman_worker.py`, line 116)

Workers-------### Registering workersCreate a directory `gearman_workers` in any of your django apps, and define as manyworkers as you like, one worker per file. Create an empty `__init__.py` so the directory will beloaded as a package.

### Registering tasksIn the worker file, you can define as many tasks as functions as you like.The function must accept a single argument as passed by the caller and mustreturn the result of the operation, if applicable. (Note : It must accept an argument, even if you don't use it).

Mark each of these functions as gearman tasks by decorating them with :

import django_gearman_jbox.decorators.gearman_task

@gearman_task() def my_task_function(foo): pass

### Task namingThe tasks are given a default name of their import path, with the phrase`gearman_task` stripped out of them, for readability reasons. You can overridethe task name by specifying `name` parameter of the decorator. Here's how :

import django_gearman_jbox.decorators.gearman_task

@gearman_task(name='my-task-name') def my_task_function(foo): pass

### Task parametersThe gearman docs specify that the task function can accept only one parameter(usually refered to as the ``data`` parameter). Additionally, that parametermay only be a string. Sometimes that may not be enough. What if you would liketo pass an array or a dict? You would need to serialize and deserialize them.Fortunately, django-gearman-jbox can take care of this, so that you can spendall of your time on coding the actual task.

would produce the error, because ``submit_job`` from Gearman's Python bindingscontains __a lot__ of arguments and it's much easier to specify them viakeyword names or a special ``args`` keyword than to type something like seven``None``s instead :

So, if you want your task definition to have, for example, ``unique`` or``background`` keyword parameters, you need to execute the task in a special,more verbose way. Here's an example of such a task and its execution :

### Task queuesQueues are a virtual abstraction layer built on top of gearman tasks. Aneasy way to describe it is the following example: Imagine you have a taskfor fetching e-mails from the server, another task for sending the emailsand one more task for sending SMS via an SMS gateway. A problem you mayencounter is that the email fetching tasks may effectively "block" the worker(there could be so many of them, it could be so time-consuming, that no othertask would be able to pass through). Of course, one solution would be to addmore workers (via the Supervisord), but that would only temporarilysolve the problem. This is where queues come in.

The first thing to do is to pass a queue name into the job description, likethis :

Be aware of the fact that if you don't specify the queue name, the workerwill load all tasks.

### Start workers with SupervisordSupervisor - http://supervisord.org/ is babysitter for processes.It allows you to launch, restart and monitor running processes. In our case it will be workers.To do so, create one config file by worker and adjust the number of workers you want with the 'numprocs' parameter :

### Execute code when workers dieWorkers catch SIGTERM and SIGINT signals to kill themselves with a `sys.exit(0)` in a callback function.At this point in the code you can add your own function(s) which will be executed before the `sys.exit(0)`See `django_gearman_jbox\management\commands\gearman_worker.py`, line 116

Note that this will impact all workers as it resides in the `gearman_worker.py` script which is global for all workers.

Clients-------To make your workers work, you need a client app passing data to them.Create and instance of the `django_gearman_jbox.GearmanClient` class and execute submit_job with it :

Licensing---------This software is licensed under the [Mozilla Tri-License][MPL]:

***** BEGIN LICENSE BLOCK ***** Version: MPL 1.1/GPL 2.0/LGPL 2.1

The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.

The Original Code is django-gearman.

The Initial Developer of the Original Code is Mozilla. Portions created by the Initial Developer are Copyright (C) 2010 the Initial Developer. All Rights Reserved.

Alternatively, the contents of this file may be used under the terms of either the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the MPL, the GPL or the LGPL.