Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

beanstalk.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. kombu.transport.beanstalk
  3. =========================
  4. Beanstalk transport.
  5. :copyright: (c) 2010 - 2013 by David Ziegler.
  6. :license: BSD, see LICENSE for more details.
  7. """
  8. from __future__ import absolute_import
  9. import socket
  10. from anyjson import loads, dumps
  11. from kombu.five import Empty
  12. from kombu.utils.encoding import bytes_to_str
  13. from . import virtual
  14. try:
  15. import beanstalkc
  16. except ImportError: # pragma: no cover
  17. beanstalkc = None # noqa
  18. DEFAULT_PORT = 11300
  19. __author__ = 'David Ziegler <david.ziegler@gmail.com>'
  20. class Channel(virtual.Channel):
  21. _client = None
  22. def _parse_job(self, job):
  23. item, dest = None, None
  24. if job:
  25. try:
  26. item = loads(bytes_to_str(job.body))
  27. dest = job.stats()['tube']
  28. except Exception:
  29. job.bury()
  30. else:
  31. job.delete()
  32. else:
  33. raise Empty()
  34. return item, dest
  35. def _put(self, queue, message, **kwargs):
  36. extra = {}
  37. priority = message['properties']['delivery_info']['priority']
  38. ttr = message['properties'].get('ttr')
  39. if ttr is not None:
  40. extra['ttr'] = ttr
  41. self.client.use(queue)
  42. self.client.put(dumps(message), priority=priority, **extra)
  43. def _get(self, queue):
  44. if queue not in self.client.watching():
  45. self.client.watch(queue)
  46. [self.client.ignore(active) for active in self.client.watching()
  47. if active != queue]
  48. job = self.client.reserve(timeout=1)
  49. item, dest = self._parse_job(job)
  50. return item
  51. def _get_many(self, queues, timeout=1):
  52. # timeout of None will cause beanstalk to timeout waiting
  53. # for a new request
  54. if timeout is None:
  55. timeout = 1
  56. watching = self.client.watching()
  57. [self.client.watch(active) for active in queues
  58. if active not in watching]
  59. [self.client.ignore(active) for active in watching
  60. if active not in queues]
  61. job = self.client.reserve(timeout=timeout)
  62. return self._parse_job(job)
  63. def _purge(self, queue):
  64. if queue not in self.client.watching():
  65. self.client.watch(queue)
  66. [self.client.ignore(active)
  67. for active in self.client.watching()
  68. if active != queue]
  69. count = 0
  70. while 1:
  71. job = self.client.reserve(timeout=1)
  72. if job:
  73. job.delete()
  74. count += 1
  75. else:
  76. break
  77. return count
  78. def _size(self, queue):
  79. return 0
  80. def _open(self):
  81. conninfo = self.connection.client
  82. host = conninfo.hostname or 'localhost'
  83. port = conninfo.port or DEFAULT_PORT
  84. conn = beanstalkc.Connection(host=host, port=port)
  85. conn.connect()
  86. return conn
  87. def close(self):
  88. if self._client is not None:
  89. return self._client.close()
  90. super(Channel, self).close()
  91. @property
  92. def client(self):
  93. if self._client is None:
  94. self._client = self._open()
  95. return self._client
  96. class Transport(virtual.Transport):
  97. Channel = Channel
  98. polling_interval = 1
  99. default_port = DEFAULT_PORT
  100. connection_errors = (
  101. virtual.Transport.connection_errors + (
  102. socket.error, IOError,
  103. getattr(beanstalkc, 'SocketError', None),
  104. )
  105. )
  106. channel_errors = (
  107. virtual.Transport.channel_errors + (
  108. socket.error, IOError,
  109. getattr(beanstalkc, 'SocketError', None),
  110. getattr(beanstalkc, 'BeanstalkcException', None),
  111. )
  112. )
  113. driver_type = 'beanstalk'
  114. driver_name = 'beanstalkc'
  115. def __init__(self, *args, **kwargs):
  116. if beanstalkc is None:
  117. raise ImportError(
  118. 'Missing beanstalkc library (pip install beanstalkc)')
  119. super(Transport, self).__init__(*args, **kwargs)
  120. def driver_version(self):
  121. return beanstalkc.__version__