Commit d90c54eb authored by David Douard's avatar David Douard
Browse files

[doc] add a simple demo and document it in the README file

parent 64c33a2c8b23
......@@ -98,3 +98,52 @@ In CubicWeb test mode, tasks don't run automatically, use
True by default.
A simple demo is supplied with the source code.
We assume the present ``cubicweb-celerytask`` cube is properly
installed in a working Cubicweb environment, and there is a redis
server available on ``redis://localhost:6379/0`` (you can change this
in ``demo/`` is needed).
.. code-block:: bash
user@host:~/celerytask$ cubicweb-ctl create celerytask demo
For the sake of simplicity, choose sqlite as database driver, and say
'yes' to the question "aAllow anonymous access ?"
Start the web application:
.. code-block:: bash
user@host:~/celerytask$ cubicweb-ctl start -D -linfo demo
Open your web browser on
In another terminal, start a celery worker:
.. code-block:: bash
user@host:~$ cd celerytask/demo
user@host:~/celerytask/demo$ celery worker -l info -E
In a third terminal, launch some tasks:
.. code-block:: bash
user@host:~$ cd celerytask/demo
user@host:~/celerytask/demo$ cubicweb-ctl shell demo
You should be able to see 3 tasks on
BROKER_URL = 'redis://localhost:6379/0'
CELERY_IMPORTS = ('cw_celerytask_helpers', 'demotasks')
CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml']
from __future__ import print_function
import sys
import logging
import time
import random
from celery import current_app as app
from celery.utils.log import get_task_logger
from cw_celerytask_helpers import redirect_stdouts
# 3 kinds of logger
cw_logger = logging.getLogger('cubes.tasks')
dummy_logger = logging.getLogger('dummy')
logger = get_task_logger(__name__)
def success(n):'TASK SUCCESS: (%s)', n)
return n
def fail():'TASK FAIL')
raise RuntimeError('fail')
def log():
for out in [sys.stdout, sys.stderr]:
print('should not be in logs', file=out)
with redirect_stdouts(logger):
print('out should be in logs')
print('err should be in logs', file=sys.stderr)
for out in [sys.stdout, sys.stderr]:
print('should not be in logs', file=out)
for name, l, state in [
('cw', cw_logger, 'be'),
('celery', logger, 'be'),
('dummy', dummy_logger, 'not be')
for key in ('debug', 'info', 'warning', 'error', 'critical'):
getattr(l, key)('%s %s should %s in logs', name, key, state)
raise RuntimeError("fail")
except RuntimeError:
l.exception('%s exception should be in logs', name)
raise Exception("oops")
def add(x, y):'TASK ADD: (%s, %s)', x, y)
return x + y
def tsum(args):'TASK SUM: (%s)', len(args))
return sum(args)
def my_task(arg, kw=0):'HI %s %s!', arg, kw)
return 42
@app.task(bind=True, name='show-me-progress')
def progress(self, steps=10):
self.update_state(state='PROGRESS', meta={'progress': 0.0})
for i in range(steps):
val = random.random()*2'Step %s: going to sleep for %.2fs', i, val)
meta={'progress': float(i+1)/steps})
from cubes.celerytask.entities import start_async_task
tasks = []
# a very simple task
tasks.append(start_async_task(cnx, 'hi_there', 'THERE', kw=42))
# a task showing several aspects of logging in cubicweb-celerytask
tasks.append(start_async_task(cnx, 'log'))
# a task with a progress bar
tasks.append(start_async_task(cnx, 'show-me-progress'))
print ("Started tasks:")
print ('\n'.join(' %s'%t.cwuri for t in tasks))
......@@ -20,4 +20,4 @@ deps =
commands = flake8 {posargs}
exclude = migration/*,test/data/*,,.tox/*
exclude = migration/*,test/data/*,,.tox/*,demo/
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment