tasks.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from time import sleep
  4. from celery import chain, group, shared_task
  5. from celery.exceptions import SoftTimeLimitExceeded
  6. from celery.utils.log import get_task_logger
  7. from .conftest import get_redis_connection
  8. logger = get_task_logger(__name__)
  9. @shared_task
  10. def identity(x):
  11. return x
  12. @shared_task
  13. def add(x, y):
  14. """Add two numbers."""
  15. return x + y
  16. @shared_task
  17. def delayed_sum(numbers, pause_time=1):
  18. """Sum the iterable of numbers."""
  19. # Allow the task to be in STARTED state for
  20. # a limited period of time.
  21. sleep(pause_time)
  22. return sum(numbers)
  23. @shared_task
  24. def delayed_sum_with_soft_guard(numbers, pause_time=1):
  25. """Sum the iterable of numbers."""
  26. try:
  27. sleep(pause_time)
  28. return sum(numbers)
  29. except SoftTimeLimitExceeded:
  30. return 0
  31. @shared_task
  32. def tsum(nums):
  33. """Sum an iterable of numbers"""
  34. return sum(nums)
  35. @shared_task(bind=True)
  36. def add_replaced(self, x, y):
  37. """Add two numbers (via the add task)."""
  38. raise self.replace(add.s(x, y))
  39. @shared_task(bind=True)
  40. def add_to_all(self, nums, val):
  41. """Add the given value to all supplied numbers."""
  42. subtasks = [add.s(num, val) for num in nums]
  43. raise self.replace(group(*subtasks))
  44. @shared_task(bind=True)
  45. def add_to_all_to_chord(self, nums, val):
  46. for num in nums:
  47. self.add_to_chord(add.s(num, val))
  48. return 0
  49. @shared_task(bind=True)
  50. def add_chord_to_chord(self, nums, val):
  51. subtasks = [add.s(num, val) for num in nums]
  52. self.add_to_chord(group(subtasks) | tsum.s())
  53. return 0
  54. @shared_task
  55. def print_unicode(log_message='hå它 valmuefrø', print_message='hiöäüß'):
  56. """Task that both logs and print strings containing funny characters."""
  57. logger.warning(log_message)
  58. print(print_message)
  59. @shared_task
  60. def sleeping(i, **_):
  61. """Task sleeping for ``i`` seconds, and returning nothing."""
  62. sleep(i)
  63. @shared_task(bind=True)
  64. def ids(self, i):
  65. """Returns a tuple of ``root_id``, ``parent_id`` and
  66. the argument passed as ``i``."""
  67. return self.request.root_id, self.request.parent_id, i
  68. @shared_task(bind=True)
  69. def collect_ids(self, res, i):
  70. """Used as a callback in a chain or group where the previous tasks
  71. are :task:`ids`: returns a tuple of::
  72. (previous_result, (root_id, parent_id, i))
  73. """
  74. return res, (self.request.root_id, self.request.parent_id, i)
  75. @shared_task(bind=True, expires=60.0, max_retries=1)
  76. def retry_once(self):
  77. """Task that fails and is retried. Returns the number of retries."""
  78. if self.request.retries:
  79. return self.request.retries
  80. raise self.retry(countdown=0.1)
  81. @shared_task
  82. def redis_echo(message):
  83. """Task that appends the message to a redis list"""
  84. redis_connection = get_redis_connection()
  85. redis_connection.rpush('redis-echo', message)
  86. @shared_task(bind=True)
  87. def second_order_replace1(self, state=False):
  88. redis_connection = get_redis_connection()
  89. if not state:
  90. redis_connection.rpush('redis-echo', 'In A')
  91. new_task = chain(second_order_replace2.s(),
  92. second_order_replace1.si(state=True))
  93. raise self.replace(new_task)
  94. else:
  95. redis_connection.rpush('redis-echo', 'Out A')
  96. @shared_task(bind=True)
  97. def second_order_replace2(self, state=False):
  98. redis_connection = get_redis_connection()
  99. if not state:
  100. redis_connection.rpush('redis-echo', 'In B')
  101. new_task = chain(redis_echo.s("In/Out C"),
  102. second_order_replace2.si(state=True))
  103. raise self.replace(new_task)
  104. else:
  105. redis_connection.rpush('redis-echo', 'Out B')