tasks.py 2.8 KB

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