Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jobtastic default settings #49

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__author__ = 'mike'

from .jobtastic import JobtasticTask
7 changes: 6 additions & 1 deletion jobtastic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 23 additions & 2 deletions jobtastic/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import warnings
from contextlib import contextmanager
from hashlib import md5
from warnings import warn

import psutil

Expand All @@ -28,13 +29,17 @@
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
# variable so folks can make an explicit decision.
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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down