Commit 7484870a authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

Drop demo code

It was unmaintained and I don't want to maintain it.
parent afa5167d6b83
......@@ -99,52 +99,3 @@ In CubicWeb test mode, tasks don't run automatically, use
Also, CELERY_ALWAYS_EAGER and CELERY_EAGER_PROPAGATES_EXCEPTIONS are set to
True by default.
Demo
----
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/celeryconfig.py`` is needed).
Then:
.. 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 http://127.0.0.1:8080/
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 launchtasks.py
You should be able to see 3 tasks on http://127.0.0.1:8080/CeleryTask
BROKER_URL = "redis://127.0.0.1:6379/3"
CELERYD_CONCURRENCY = 1
CELERYD_PREFETCH_MULTIPLIER = 1
CELERY_SEND_EVENTS = 1
CELERY_RESULT_BACKEND = BROKER_URL
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml']
CELERY_IMPORTS = ('cw_celerytask_helpers.redislogger', 'demotasks')
CUBICWEB_CELERYTASK_LOG_ALL = True
CUBICWEB_CELERYTASK_REDIS_URL = BROKER_URL
from __future__ import print_function
import sys
import logging
import time
import random
from celery import current_app as app
from celery import chord
from celery.utils.log import get_task_logger
from cw_celerytask_helpers.redislogger import redirect_stdouts
# 3 kinds of logger
cw_logger = logging.getLogger('cubes.tasks')
dummy_logger = logging.getLogger('dummy')
logger = get_task_logger(__name__)
@app.task(name='success')
def success(n):
logger.info('TASK SUCCESS: (%s)', n)
time.sleep(random.random()*2)
return n
@app.task(name='fail')
def fail():
logger.info('TASK FAIL')
raise RuntimeError('fail')
@app.task(name='log')
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)
try:
raise RuntimeError("fail")
except RuntimeError:
l.exception('%s exception should be in logs', name)
raise Exception("oops")
@app.task(name="add")
def add(x, y):
logger.info('TASK ADD: (%s, %s)', x, y)
return x + y
@app.task(bind=True, name="tsum")
def tsum(self, args, root_id=None):
if root_id is None:
root_id = self.request.id
logger.info('TASK SUM: (%s) <root=%s>', len(args), root_id)
return sum(args)
@app.task(bind=True, name='hi_there')
def my_task(self, arg, kw=0, root_id=None):
if root_id is None:
root_id = self.request.id
logger.info('HI %s %s! <root=%s>', arg, kw, root_id)
if kw is 'wait':
time.sleep(1)
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
logger.info('Step %s: going to sleep for %.2fs', i, val)
time.sleep(val)
self.update_state(state='PROGRESS',
meta={'progress': (i+1/steps)})
@app.task(bind=True, name='multi')
def multi(self):
logger.info('Starting multi!')
time.sleep(2)
logger.info('Running subtasks!')
r = chord([my_task.si(i, kw='wait') for i in range(5)],
tsum.s())
logger.info('multi done!')
return r().serializable()
from cubicweb_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'))
cnx.commit()
print ("Started tasks:")
print ('\n'.join(' %s'%t.cwuri for t in tasks))
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