tasks.py 3.7 KB

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