tasks.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from time import sleep
  4. from celery import shared_task, group
  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)