]> git.parisson.com Git - timeside.git/commitdiff
Add working celery task_run (needs some workers), add sandbox secret key
authorGuillaume Pellerin <yomguy@parisson.com>
Fri, 13 Mar 2015 22:23:59 +0000 (23:23 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Fri, 13 Mar 2015 22:23:59 +0000 (23:23 +0100)
examples/sandbox/__init__.py
examples/sandbox/celery_app.py [new file with mode: 0644]
examples/sandbox/settings.py
timeside/server/celery.py [deleted file]
timeside/server/models.py
timeside/server/tasks.py

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8b137891791fe96927ad78e64b0aad7bded08bdc 100644 (file)
@@ -0,0 +1 @@
+
diff --git a/examples/sandbox/celery_app.py b/examples/sandbox/celery_app.py
new file mode 100644 (file)
index 0000000..f558ac7
--- /dev/null
@@ -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))
index 9a00ba8a34fd1e06fd5fcc8bedb23666deaeeaf1..66c666f2c50a9987970629bef2641ba124737a18 100644 (file)
@@ -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 (file)
index cf5df39..0000000
+++ /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()
index 9e9547431c69232ce83ef0cef3c5f670c25b3ba5..034909557c5abec5e5f04fea5f45453fd63ed0a8 100644 (file)
@@ -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)
index 336494fd937b02140e3ae991741ad7bbb4f594b8..1ad7db65e67d076aa9fa27c2df563a0599604743 100644 (file)
@@ -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