Permalink
Browse files

untested, initial gearman wrapper

  • Loading branch information...
1 parent 6a89f47 commit 596b0d64df884e77fd4ccaa3f9a969ce75d4b43b @fwenzel fwenzel committed Jan 14, 2010
Showing with 55 additions and 0 deletions.
  1. +10 −0 django_gearman/__init__.py
  2. +45 −0 django_gearman/models.py
View
@@ -0,0 +1,10 @@
+"""
+Django Gearman Interface
+"""
+import gearman
+
+from models import DjangoGearmanClient as GearmanClient
+from models import DjangoGearmanWorker as GearmanWorker
+from models import DjangoGearmanTask as Task
+from gearman.task import Taskset
+
View
@@ -0,0 +1,45 @@
+from gearman import GearmanClient, GearmanWorker, Task
+
+import settings
+
+
+class DjangoGearmanClient(GearmanClient):
+ """gearman client, automatically connecting to server"""
+
+ def __init__(self, **kwargs):
+ """instanciate Gearman client with servers from settings file"""
+ return super(DjangoGearmanClient, self).__init__(
+ settings.GEARMAN_SERVERS, **kwargs)
+
+class DjangoGearmanWorker(GearmanWorker):
+ """
+ gearman worker, automatically connecting to server and discovering
+ available jobs
+ """
+
+ def __init__(self, **kwargs):
+ """instanciate Gearman worker with servers from settings file"""
+ return super(DjangoGearmanWorker, self).__init__(
+ settings.GEARMAN_SERVERS, **kwargs)
+
+class DjangoGearmanTask(Task):
+ """Gearman Task, namespacing jobs according to config file"""
+
+ def __init__(self, func, arg, **kwargs):
+ # get app and job name from function call, namespace as configured,
+ # then execute
+ parts = func.partition('.')
+ if parts[2]:
+ app = parts[0]
+ job = parts[2]
+ else:
+ app = ''
+ job = parts[0]
+
+ try:
+ func = settings.GEARMAN_JOB_NAME % {'app': app, 'job': job}
+ except NameError:
+ pass
+
+ return super(DjangoGearmanTask, self).__init__(func, args, **kwargs)
+

0 comments on commit 596b0d6

Please sign in to comment.