MongoDB result backend. Raises: celery.exceptions.ImproperlyConfigured: if module :pypi:`pymongo` is not available.
| 38 | |
| 39 | |
| 40 | class MongoBackend(BaseBackend): |
| 41 | """MongoDB result backend. |
| 42 | |
| 43 | Raises: |
| 44 | celery.exceptions.ImproperlyConfigured: |
| 45 | if module :pypi:`pymongo` is not available. |
| 46 | """ |
| 47 | |
| 48 | mongo_host = None |
| 49 | host = 'localhost' |
| 50 | port = 27017 |
| 51 | user = None |
| 52 | password = None |
| 53 | database_name = 'celery' |
| 54 | taskmeta_collection = 'celery_taskmeta' |
| 55 | groupmeta_collection = 'celery_groupmeta' |
| 56 | max_pool_size = 10 |
| 57 | options = None |
| 58 | |
| 59 | supports_autoexpire = False |
| 60 | |
| 61 | # Bytes round-trip through BSON as a binary field. |
| 62 | supports_result_compression = True |
| 63 | |
| 64 | _connection = None |
| 65 | |
| 66 | def __init__(self, app=None, **kwargs): |
| 67 | self.options = {} |
| 68 | |
| 69 | super().__init__(app, **kwargs) |
| 70 | |
| 71 | if not pymongo: |
| 72 | raise ImproperlyConfigured( |
| 73 | 'You need to install the pymongo library to use the ' |
| 74 | 'MongoDB backend.') |
| 75 | |
| 76 | # Set option defaults |
| 77 | for key, value in self._prepare_client_options().items(): |
| 78 | self.options.setdefault(key, value) |
| 79 | |
| 80 | # update conf with mongo uri data, only if uri was given |
| 81 | if self.url: |
| 82 | self.url = self._ensure_mongodb_uri_compliance(self.url) |
| 83 | |
| 84 | uri_data = uri_parser.parse_uri(self.url) |
| 85 | # build the hosts list to create a mongo connection |
| 86 | hostslist = [ |
| 87 | f'{x[0]}:{x[1]}' for x in uri_data['nodelist'] |
| 88 | ] |
| 89 | self.user = uri_data['username'] |
| 90 | self.password = uri_data['password'] |
| 91 | self.mongo_host = hostslist |
| 92 | if uri_data['database']: |
| 93 | # if no database is provided in the uri, use default |
| 94 | self.database_name = uri_data['database'] |
| 95 | |
| 96 | self.options.update(uri_data['options']) |
| 97 |
no outgoing calls