tasks.py 3.2 KB

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