diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e1f7f16 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +__author__ = 'mike' + +from .jobtastic import JobtasticTask \ No newline at end of file diff --git a/jobtastic/__init__.py b/jobtastic/__init__.py index a2cc903..1ed8b47 100644 --- a/jobtastic/__init__.py +++ b/jobtastic/__init__.py @@ -14,4 +14,9 @@ # -eof meta- -from jobtastic.task import JobtasticTask # NOQA +# Reserved for default settings +# works on Ubuntu after ~$ sudo apt-get install libmemcached-dev +DEFAULT_RESULT_BACKEND = 'cache' +DEFAULT_CACHE_BACKEND = 'memcached://127.0.0.1:11211/' + +from .task import JobtasticTask # NOQA diff --git a/jobtastic/task.py b/jobtastic/task.py index 3eff83d..ebed1ec 100644 --- a/jobtastic/task.py +++ b/jobtastic/task.py @@ -13,6 +13,7 @@ import warnings from contextlib import contextmanager from hashlib import md5 +from warnings import warn import psutil @@ -28,6 +29,8 @@ pass # get_task_logger is new in Celery 3.X cache = None + + try: # For now, let's just say that if Django exists, we should use it. # Otherwise, try Flask. This definitely needs an actual configuration @@ -35,6 +38,8 @@ from django.core.cache import cache HAS_DJANGO = True except ImportError: + HAS_DJANGO = False + warn('Jobtastic failed to import django cache, looking for celery backend now...') try: # We should really have an explicitly-defined way of doing this, but # for now, let's just use werkzeug Memcached if it exists @@ -47,14 +52,29 @@ cache = MemcachedCache(uris) HAS_WERKZEUG = True except ImportError: - pass + HAS_WERKZEUG = False + warn('Jobtastic failed to use celery backend, trying with default settings now...') + + try: + from werkzeug.contrib.cache import MemcachedCache + from . import DEFAULT_CACHE_BACKEND, DEFAULT_RESULT_BACKEND + if DEFAULT_RESULT_BACKEND == 'cache': + uri_str = DEFAULT_CACHE_BACKEND.strip('memcached://') + uris = uri_str.split(';') + cache = MemcachedCache(uris) + HAS_WERKZEUG = True + warn('Jobtastic uses default settings now') + except Exception, e: + HAS_WERKZEUG = False + warn("Jobtastic failed to use default settings") + raise e if cache is None: raise Exception( "Jobtastic requires either Django or Flask + Memcached result backend") -from jobtastic.states import PROGRESS # NOQA +from .states import PROGRESS # NOQA @contextmanager @@ -225,6 +245,7 @@ def apply_async(self, args, kwargs, **options): synchronizing the task uuid. Additionally, identical task calls will return those results for the next ``cache_duration`` seconds. """ + self._validate_required_class_vars() cache_key = self._get_cache_key(**kwargs)