Commits

Lynn Rees committed 4e9b0da

- distribute conf

  • Participants
  • Parent commits bec1015

Comments (0)

Files changed (7)

 *.egg-info
 .p*
 *~
+.hg

File django_ztask/__init__.py

 __homepage__ = 'http://github.com/dmgctrl/django-ztask'
 __docformat__ = 'markdown'
 __license__ = 'BSD (3 clause)'
+
+# example settings
+#ZTASKD_URL = getattr(settings, 'ZTASKD_URL', 'tcp://127.0.0.1:5555')
+#ZTASKD_ALWAYS_EAGER = getattr(settings, 'ZTASKD_ALWAYS_EAGER', False)
+#ZTASKD_DISABLED = getattr(settings, 'ZTASKD_DISABLED', False)
+#ZTASKD_RETRY_COUNT = getattr(settings, 'ZTASKD_RETRY_COUNT', 5)
+#ZTASKD_RETRY_AFTER = getattr(settings, 'ZTASKD_RETRY_AFTER', 5)
+#ZTASKD_LOGGER = getattr(settings, 'ZTASKD_LOGGER', 'django_ztaskd')
+#ZTASKD_ON_LOAD = getattr(settings, 'ZTASKD_ON_LOAD', ())
+#ZTASKD_ON_CALL_COMPLETE = getattr(settings, 'ZTASKD_ON_COMPLETE', ())

File django_ztask/conf/settings.py

+
+
 from django.conf import settings
 
-ZTASKD_URL = getattr(settings, 'ZTASKD_URL', 'tcp://127.0.0.1:5555')
-ZTASKD_ALWAYS_EAGER = getattr(settings, 'ZTASKD_ALWAYS_EAGER', False)
-ZTASKD_DISABLED = getattr(settings, 'ZTASKD_DISABLED', False)
-ZTASKD_RETRY_COUNT = getattr(settings, 'ZTASKD_RETRY_COUNT', 5)
-ZTASKD_RETRY_AFTER = getattr(settings, 'ZTASKD_RETRY_AFTER', 5)
-ZTASKD_LOGGER = getattr(settings, 'ZTASKD_LOGGER', 'django_ztaskd')
-ZTASKD_ON_LOAD = getattr(settings, 'ZTASKD_ON_LOAD', ())
-#ZTASKD_ON_CALL_COMPLETE = getattr(settings, 'ZTASKD_ON_COMPLETE', ())
+

File django_ztask/decorators.py

+# -*- coding: utf-8 -*-
+
 import types
 from functools import wraps
 
+from django.conf import settings
 from django.utils.log import getLogger
-from django_ztask.conf import settings
+
 try:
     from zmq import PUSH
 except:
     from zmq import DOWNSTREAM as PUSH
+
 from django_ztask.context import shared_context as context
     
-log = getLogger(settings.ZTASKD_LOGGER)
+log = getLogger(getattr(settings, 'ZTASKD_LOGGER', 'django_ztaskd'))
 
 def task():
     def wrapper(func):
         function_name = '%s.%s' % (func.__module__, func.__name__)
         log.info('Registered task: %s' % function_name)
         socket = context.socket(PUSH)
-        socket.connect(settings.ZTASKD_URL)
+        socket.connect(getattr(settings, 'ZTASKD_URL', 'tcp://127.0.0.1:5555'))
         
         @wraps(func)
         def _func(*args, **kw):
             after = kw.pop('__ztask_after', 0)
-            if settings.ZTASKD_DISABLED:
+            if getattr(settings, 'ZTASKD_DISABLED', False):
                 try:
                     socket.send_pyobj(('ztask_log', (
                         'Would have called but ZTASKD_DISABLED is True', 
                         'True' % function_name
                     )
                 return
-            elif settings.ZTASKD_ALWAYS_EAGER:
+            elif getattr(settings, 'ZTASKD_ALWAYS_EAGER', False):
                 log.info(
                     'Running %s in ZTASKD_ALWAYS_EAGER mode' % function_name
                 )
                     )
                 kw['__ztask_after'] = after
                 _func(*args[1:], **kw)
-            except Exception, e:
-                log.info('Error adding delayed task:\n%s' % e)
+            except:
+                log.exception('Error adding delayed task:\n%s')
         setattr(func, 'async', _func)
         setattr(func, 'delay', _func_delay)
         setattr(func, 'after', _func_after)

File django_ztask/management/__init__.py

+# -*- coding: utf-8 -*-

File django_ztask/management/commands/ztaskd.py

 import datetime
 from optparse import make_option
 
+from django.conf import settings
+from django.utils.log import getLogger
+from django.utils import autoreload, importlib
+from django.core.management.base import BaseCommand
+
 try:
     from zmq import PULL
 except:
     from zmq import UPSTREAM as PULL
 from zmq.eventloop import ioloop
-from django.utils.log import getLogger
-from django.utils import autoreload, importlib
-from django.core.management.base import BaseCommand
 
 from django_ztask.models import Task
-from django_ztask.conf import settings
 from django_ztask.context import shared_context as context
 
-log = getLogger(settings.ZTASKD_LOGGER)
+log = getLogger(getattr(settings, 'ZTASKD_LOGGER', 'django_ztaskd'))
 
  
 class Command(BaseCommand):
             self._handle(use_reloader, replay_failed)
     
     def _handle(self, use_reloader, replay_failed):
+        ztask_url = getattr(settings, 'ZTASKD_URL', 'tcp://127.0.0.1:5555')
         log.info("%sServer starting on %s." % (
-            'Development ' if use_reloader else '', settings.ZTASKD_URL)
+            'Development ' if use_reloader else '', ztask_url)
         )
         self._on_load()
         socket = context.socket(PULL)
-        socket.bind(settings.ZTASKD_URL)
+        socket.bind(ztask_url)
+        
         def _queue_handler(socket, *args, **kw):
             try:
                 function_name, args, kw, after = socket.recv_pyobj()
                     function_name=function_name, 
                     args=pickle.dumps(args), 
                     kwargs=pickle.dumps(kw), 
-                    retry_count=settings.ZTASKD_RETRY_COUNT,
+                    retry_count=getattr(settings, 'ZTASKD_RETRY_COUNT', 5),
                     next_attempt=time.time() + after
                 )
                 if after:
-                    ioloop.DelayedCallback(lambda: self._call_function(
-                        task.pk, 
-                        function_name=function_name, 
-                        args=args, 
-                        kwargs=kw),
+                    ioloop.DelayedCallback(
+                        lambda: self._call_function(
+                            task.pk, 
+                            function_name=function_name, 
+                            args=args, 
+                            kw=kw
+                        ),
                         after*1000, 
                         io_loop=self.io_loop
                     ).start()
                         task.pk, 
                         function_name=function_name, 
                         args=args, 
-                        kwargs=kw,
+                        kw=kw,
                     )
             except:
                 log.exception('Error setting up function')
         except Exception, e:
             log.exception('Error calling %s' % function_name)
             try:
+                retry_count = getattr(settings, 'ZTASKD_RETRY_COUNT', 5)
                 task = Task.objects.get(pk=task_id)
                 if task.retry_count > 0:
                     task.retry_count = task.retry_count - 1
-                    task.next_attempt = time.time() + settings.ZTASKD_RETRY_AFTER
+                    task.next_attempt = time.time() + retry_count
                     ioloop.DelayedCallback(
                         lambda: self._call_function(task.pk), 
-                        settings.ZTASKD_RETRY_AFTER * 1000, 
+                        retry_count*1000, 
                         io_loop=self.io_loop
                     ).start()
                 task.failed = datetime.datetime.utcnow()
                 log.exception('Error capturing exception in _call_function')
         
     def _on_load(self):
-        for callable_name in settings.ZTASKD_ON_LOAD:
+        ztaskd_on_load = getattr(settings, 'ZTASKD_ON_LOAD', ())
+        for callable_name in ztaskd_on_load:
             log.info("ON_LOAD calling %s" % callable_name)
             parts = callable_name.split('.')
             module_name = '.'.join(parts[:-1])

File django_ztask/models.py

+# -*- coding: utf-8 -*-
+
 import uuid
 from datetime import datetime