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.

couchbase.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.backends.couchbase
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~
  5. CouchBase result store backend.
  6. """
  7. from __future__ import absolute_import
  8. import logging
  9. try:
  10. from couchbase import Couchbase
  11. from couchbase.connection import Connection
  12. from couchbase.exceptions import NotFoundError
  13. except ImportError:
  14. Couchbase = Connection = NotFoundError = None # noqa
  15. from kombu.utils.url import _parse_url
  16. from celery.exceptions import ImproperlyConfigured
  17. from celery.utils.timeutils import maybe_timedelta
  18. from .base import KeyValueStoreBackend
  19. __all__ = ['CouchBaseBackend']
  20. class CouchBaseBackend(KeyValueStoreBackend):
  21. """CouchBase backend.
  22. :raises celery.exceptions.ImproperlyConfigured: if
  23. module :mod:`couchbase` is not available.
  24. """
  25. bucket = 'default'
  26. host = 'localhost'
  27. port = 8091
  28. username = None
  29. password = None
  30. quiet = False
  31. conncache = None
  32. unlock_gil = True
  33. timeout = 2.5
  34. transcoder = None
  35. def __init__(self, url=None, *args, **kwargs):
  36. super(CouchBaseBackend, self).__init__(*args, **kwargs)
  37. self.url = url
  38. self.expires = kwargs.get('expires') or maybe_timedelta(
  39. self.app.conf.CELERY_TASK_RESULT_EXPIRES)
  40. if Couchbase is None:
  41. raise ImproperlyConfigured(
  42. 'You need to install the couchbase library to use the '
  43. 'CouchBase backend.',
  44. )
  45. uhost = uport = uname = upass = ubucket = None
  46. if url:
  47. _, uhost, uport, uname, upass, ubucket, _ = _parse_url(url)
  48. ubucket = ubucket.strip('/') if ubucket else None
  49. config = self.app.conf.get('CELERY_COUCHBASE_BACKEND_SETTINGS', None)
  50. if config is not None:
  51. if not isinstance(config, dict):
  52. raise ImproperlyConfigured(
  53. 'Couchbase backend settings should be grouped in a dict',
  54. )
  55. else:
  56. config = {}
  57. self.host = uhost or config.get('host', self.host)
  58. self.port = int(uport or config.get('port', self.port))
  59. self.bucket = ubucket or config.get('bucket', self.bucket)
  60. self.username = uname or config.get('username', self.username)
  61. self.password = upass or config.get('password', self.password)
  62. self._connection = None
  63. def _get_connection(self):
  64. """Connect to the Couchbase server."""
  65. if self._connection is None:
  66. kwargs = {'bucket': self.bucket, 'host': self.host}
  67. if self.port:
  68. kwargs.update({'port': self.port})
  69. if self.username:
  70. kwargs.update({'username': self.username})
  71. if self.password:
  72. kwargs.update({'password': self.password})
  73. logging.debug('couchbase settings %r', kwargs)
  74. self._connection = Connection(**kwargs)
  75. return self._connection
  76. @property
  77. def connection(self):
  78. return self._get_connection()
  79. def get(self, key):
  80. try:
  81. return self.connection.get(key).value
  82. except NotFoundError:
  83. return None
  84. def set(self, key, value):
  85. self.connection.set(key, value)
  86. def mget(self, keys):
  87. return [self.get(key) for key in keys]
  88. def delete(self, key):
  89. self.connection.delete(key)