From: Guillaume Pellerin Date: Fri, 13 Mar 2015 22:23:59 +0000 (+0100) Subject: Add working celery task_run (needs some workers), add sandbox secret key X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=22e806e291f67422e1fce54dd3de1289801797eb;p=timeside.git Add working celery task_run (needs some workers), add sandbox secret key --- diff --git a/examples/sandbox/__init__.py b/examples/sandbox/__init__.py index e69de29..8b13789 100644 --- a/examples/sandbox/__init__.py +++ b/examples/sandbox/__init__.py @@ -0,0 +1 @@ + diff --git a/examples/sandbox/celery_app.py b/examples/sandbox/celery_app.py new file mode 100644 index 0000000..f558ac7 --- /dev/null +++ b/examples/sandbox/celery_app.py @@ -0,0 +1,24 @@ +from __future__ import absolute_import + +import os + +from celery import Celery + +from django.conf import settings + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') + +app = Celery('sandbox') + +# Using a string here means the worker will not have to +# pickle the object when using Windows. +app.config_from_object('django.conf:settings') +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) +app.conf.update( + CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend', +) + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) diff --git a/examples/sandbox/settings.py b/examples/sandbox/settings.py index 9a00ba8..66c666f 100644 --- a/examples/sandbox/settings.py +++ b/examples/sandbox/settings.py @@ -1,5 +1,4 @@ # Django settings for server project. - DEBUG = True TEMPLATE_DEBUG = DEBUG @@ -30,6 +29,9 @@ DATABASES = { # See https://docs.djangoproject.com/en/1.4/ref/settings/#allowed-hosts ALLOWED_HOSTS = [] +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'ghv8us2587n97dq&w$c((o5rj_$-9#d-8j#57y_a9og8wux1h7' + # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. @@ -134,7 +136,7 @@ INSTALLED_APPS = ( 'timeside.server', 'timeside.player', 'rest_framework', - 'django_gearman', + 'djcelery', ) # A sample logging configuration. The only tangible logging @@ -170,4 +172,12 @@ REST_FRAMEWORK = { } # One or more gearman servers -GEARMAN_SERVERS = ['127.0.0.1'] +# GEARMAN_SERVERS = ['127.0.0.1'] + +BROKER_URL = 'amqp://guest:guest@localhost//' + + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +CELERY_IMPORTS = ("timeside.server.tasks", ) +from celery_app import app diff --git a/timeside/server/celery.py b/timeside/server/celery.py deleted file mode 100644 index cf5df39..0000000 --- a/timeside/server/celery.py +++ /dev/null @@ -1,17 +0,0 @@ - -from __future__ import absolute_import - -from celery import Celery - -app = Celery('timeside', - broker='amqp://', - backend='amqp://', - include=['timeside.tasks']) - -# Optional configuration, see the application user guide. -app.conf.update( - CELERY_TASK_RESULT_EXPIRES=3600, -) - -if __name__ == '__main__': - app.start() diff --git a/timeside/server/models.py b/timeside/server/models.py index 9e95474..0349095 100644 --- a/timeside/server/models.py +++ b/timeside/server/models.py @@ -404,13 +404,10 @@ def set_hash(sender, **kwargs): def run(sender, **kwargs): + from timeside.server.tasks import task_run instance = kwargs['instance'] if instance.status == _PENDING: - from django_gearman import GearmanClient - client = GearmanClient() - completed_job_request = client.submit_job("timeside.server.task_run", instance.id) - print "Result: '%s'" % completed_job_request.result - # instance.run() + task_run.delay(instance.id) post_save.connect(set_mimetype, sender=Item) diff --git a/timeside/server/tasks.py b/timeside/server/tasks.py index 336494f..1ad7db6 100644 --- a/timeside/server/tasks.py +++ b/timeside/server/tasks.py @@ -1,9 +1,25 @@ - from __future__ import absolute_import -from timeside.server.celery import app +from celery import shared_task +from .models import Task + + +@shared_task +def task_run(id): + task = Task.objects.get(id=id) + task.run() + + +@shared_task +def add(x, y): + return x + y + + +@shared_task +def mul(x, y): + return x * y -@app.task -def process(pipe): - pipe.run() +@shared_task +def xsum(numbers): + return sum(numbers) \ No newline at end of file