templates.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_TRANS = os.environ.get('CSTRESS_TRANS', False)
  8. default_queue = 'c.stress.trans' if CSTRESS_TRANS else 'c.stress'
  9. CSTRESS_QUEUE = os.environ.get('CSTRESS_QUEUE_NAME', default_queue)
  10. templates = {}
  11. def template(name=None):
  12. def _register(cls):
  13. templates[name or cls.__name__] = '.'.join([__name__, cls.__name__])
  14. return cls
  15. return _register
  16. def use_template(app, template='default'):
  17. template = template.split(',')
  18. app.after_configure = partial(mixin_templates, template[1:])
  19. app.config_from_object(templates[template[0]])
  20. def mixin_templates(templates, conf):
  21. return [mixin_template(template, conf) for template in templates]
  22. def mixin_template(template, conf):
  23. cls = symbol_by_name(templates[template])
  24. conf.update(dict(
  25. (k, v) for k, v in items(vars(cls))
  26. if k.isupper() and not k.startswith('_')
  27. ))
  28. def template_names():
  29. return ', '.join(templates)
  30. @template()
  31. class default(object):
  32. CELERY_ACCEPT_CONTENT = ['json']
  33. CELERY_DEFAULT_QUEUE = CSTRESS_QUEUE
  34. CELERY_TASK_SERIALIZER = 'json'
  35. CELERY_RESULT_SERIALIZER = 'json'
  36. CELERY_RESULT_PERSISTENT = True
  37. CELERY_TASK_RESULT_EXPIRES = 300
  38. CELERY_QUEUES = [
  39. Queue(CSTRESS_QUEUE,
  40. exchange=Exchange(CSTRESS_QUEUE),
  41. routing_key=CSTRESS_QUEUE,
  42. durable=not CSTRESS_TRANS,
  43. no_ack=CSTRESS_TRANS),
  44. ]
  45. CELERY_MAX_CACHED_RESULTS = -1
  46. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'amqp://')
  47. CELERY_RESULT_BACKEND = os.environ.get('CSTRESS_BACKEND', 'rpc://')
  48. CELERYD_PREFETCH_MULTIPLIER = int(os.environ.get('CSTRESS_PREFETCH', 10))
  49. CELERY_TASK_PUBLISH_RETRY_POLICY = {
  50. 'max_retries': 100,
  51. 'interval_max': 2,
  52. 'interval_step': 0.1,
  53. }
  54. if CSTRESS_TRANS:
  55. CELERY_DEFAULT_DELIVERY_MODE = 1
  56. @template()
  57. class redis(default):
  58. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'redis://')
  59. CELERY_RESULT_BACKEND = os.environ.get(
  60. 'CSTRESS_BACKEND', 'redis://?new_join=1',
  61. )
  62. BROKER_TRANSPORT_OPTIONS = {
  63. 'fanout_prefix': True,
  64. 'fanout_patterns': True,
  65. }
  66. @template()
  67. class redistore(default):
  68. CELERY_RESULT_BACKEND = 'redis://'
  69. @template()
  70. class acks_late(default):
  71. CELERY_ACKS_LATE = True
  72. @template()
  73. class pickle(default):
  74. CELERY_ACCEPT_CONTENT = ['pickle', 'json']
  75. CELERY_TASK_SERIALIZER = 'pickle'
  76. CELERY_RESULT_SERIALIZER = 'pickle'
  77. @template()
  78. class confirms(default):
  79. BROKER_URL = 'pyamqp://'
  80. BROKER_TRANSPORT_OPTIONS = {'confirm_publish': True}
  81. @template()
  82. class events(default):
  83. CELERY_SEND_EVENTS = True
  84. CELERY_SEND_TASK_SENT_EVENT = True
  85. @template()
  86. class execv(default):
  87. CELERYD_FORCE_EXECV = True