templates.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import absolute_import
  2. import os
  3. from functools import partial
  4. from celery.five import items
  5. from kombu import Exchange, Queue
  6. from kombu.utils import symbol_by_name
  7. CSTRESS_QUEUE = os.environ.get('CSTRESS_QUEUE_NAME', 'c.stress')
  8. templates = {}
  9. def template(name=None):
  10. def _register(cls):
  11. templates[name or cls.__name__] = '.'.join([__name__, cls.__name__])
  12. return cls
  13. return _register
  14. def use_template(app, template='default'):
  15. template = template.split(',')
  16. app.after_configure = partial(mixin_templates, template[1:])
  17. app.config_from_object(templates[template[0]])
  18. def mixin_templates(templates, conf):
  19. return [mixin_template(template, conf) for template in templates]
  20. def mixin_template(template, conf):
  21. cls = symbol_by_name(templates[template])
  22. conf.update(dict(
  23. (k, v) for k, v in items(vars(cls))
  24. if k.isupper() and not k.startswith('_')
  25. ))
  26. def template_names():
  27. return ', '.join(templates)
  28. @template()
  29. class default(object):
  30. CELERY_ACCEPT_CONTENT = ['json']
  31. CELERY_DEFAULT_QUEUE = CSTRESS_QUEUE
  32. CELERY_TASK_SERIALIZER = 'json'
  33. CELERY_RESULT_SERIALIZER = 'json'
  34. CELERY_QUEUES = [
  35. Queue(CSTRESS_QUEUE,
  36. exchange=Exchange(CSTRESS_QUEUE),
  37. routing_key=CSTRESS_QUEUE),
  38. ]
  39. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'amqp://')
  40. CELERY_RESULT_BACKEND = os.environ.get('CSTRESS_BACKEND', 'rpc://')
  41. CELERYD_PREFETCH_MULTIPLIER = int(os.environ.get('CSTRESS_PREFETCH', 1))
  42. @template()
  43. class redis(default):
  44. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'redis://')
  45. CELERY_RESULT_BACKEND = os.environ.get('CSTRESS_bACKEND', 'redis://')
  46. @template()
  47. class acks_late(default):
  48. CELERY_ACKS_LATE = True
  49. @template()
  50. class pickle(default):
  51. CELERY_ACCEPT_CONTENT = ['pickle', 'json']
  52. CELERY_TASK_SERIALIZER = 'pickle'
  53. CELERY_RESULT_SERIALIZER = 'pickle'