templates.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # mixin the rest of the templates when the config is needed
  17. @app.on_after_configure.connect(weak=False)
  18. def load_template(sender, source, **kwargs):
  19. mixin_templates(template[1:], source)
  20. app.config_from_object(templates[template[0]])
  21. def mixin_templates(templates, conf):
  22. return [mixin_template(template, conf) for template in templates]
  23. def mixin_template(template, conf):
  24. cls = symbol_by_name(templates[template])
  25. conf.update(dict(
  26. (k, v) for k, v in items(vars(cls))
  27. if k.isupper() and not k.startswith('_')
  28. ))
  29. def template_names():
  30. return ', '.join(templates)
  31. @template()
  32. class default(object):
  33. CELERY_ACCEPT_CONTENT = ['json']
  34. CELERY_DEFAULT_QUEUE = CSTRESS_QUEUE
  35. CELERY_TASK_SERIALIZER = 'json'
  36. CELERY_RESULT_SERIALIZER = 'json'
  37. CELERY_RESULT_PERSISTENT = True
  38. CELERY_TASK_RESULT_EXPIRES = 300
  39. CELERY_QUEUES = [
  40. Queue(CSTRESS_QUEUE,
  41. exchange=Exchange(CSTRESS_QUEUE),
  42. routing_key=CSTRESS_QUEUE),
  43. ]
  44. CELERY_MAX_CACHED_RESULTS = -1
  45. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'amqp://')
  46. CELERY_RESULT_BACKEND = os.environ.get('CSTRESS_BACKEND', 'rpc://')
  47. CELERYD_PREFETCH_MULTIPLIER = int(os.environ.get('CSTRESS_PREFETCH', 10))
  48. CELERY_TASK_PUBLISH_RETRY_POLICY = {
  49. 'max_retries': 100,
  50. 'interval_max': 2,
  51. 'interval_step': 0.1,
  52. }
  53. @template()
  54. class redis(default):
  55. BROKER_URL = os.environ.get('CSTRESS_BROKER', 'redis://')
  56. CELERY_RESULT_BACKEND = os.environ.get(
  57. 'CSTRESS_BACKEND', 'redis://?new_join=1',
  58. )
  59. BROKER_TRANSPORT_OPTIONS = {
  60. 'fanout_prefix': True,
  61. 'fanout_patterns': True,
  62. }
  63. @template()
  64. class redistore(default):
  65. CELERY_RESULT_BACKEND = 'redis://'
  66. @template()
  67. class acks_late(default):
  68. CELERY_ACKS_LATE = True
  69. @template()
  70. class pickle(default):
  71. CELERY_ACCEPT_CONTENT = ['pickle', 'json']
  72. CELERY_TASK_SERIALIZER = 'pickle'
  73. CELERY_RESULT_SERIALIZER = 'pickle'
  74. @template()
  75. class confirms(default):
  76. BROKER_URL = 'pyamqp://'
  77. BROKER_TRANSPORT_OPTIONS = {'confirm_publish': True}
  78. @template()
  79. class events(default):
  80. CELERY_SEND_EVENTS = True
  81. CELERY_SEND_TASK_SENT_EVENT = True
  82. @template()
  83. class execv(default):
  84. CELERYD_FORCE_EXECV = True
  85. @template()
  86. class sqs(default):
  87. BROKER_URL='sqs://'
  88. BROKER_TRANSPORT_OPTIONS = {
  89. 'region': os.environ.get('AWS_REGION', 'us-east-1'),
  90. }