|
| 1 | +import atexit |
1 | 2 | from typing import Any |
2 | 3 |
|
3 | 4 | from pymongo.errors import ExecutionTimeout |
|
10 | 11 | from optimade.server.mappers import BaseResourceMapper |
11 | 12 | from optimade.server.query_params import EntryListingQueryParams, SingleEntryQueryParams |
12 | 13 |
|
| 14 | +_CLIENTS: dict[tuple[str, str], Any] = {} |
| 15 | + |
| 16 | + |
| 17 | +def _close_all_clients(log: bool = True): |
| 18 | + for (backend, uri), client in list(_CLIENTS.items()): |
| 19 | + try: |
| 20 | + client.close() |
| 21 | + if log: |
| 22 | + LOGGER.debug(f"Closed MongoClient for {backend} {uri}") |
| 23 | + except Exception as exc: |
| 24 | + if log: |
| 25 | + LOGGER.warning(f"Failed closing MongoClient {backend} {uri}: {exc}") |
| 26 | + finally: |
| 27 | + _CLIENTS.pop((backend, uri), None) |
| 28 | + |
| 29 | + |
| 30 | +atexit.register(lambda: _close_all_clients(log=False)) |
| 31 | + |
13 | 32 |
|
14 | 33 | def get_mongo_client(config: ServerConfig): |
15 | | - if config.database_backend.value == "mongodb": |
16 | | - from pymongo import MongoClient, version_tuple |
| 34 | + """Return a cached MongoClient for (backend, uri), creating it if necessary.""" |
| 35 | + backend = config.database_backend.value |
| 36 | + uri = config.mongo_uri |
| 37 | + key = (backend, uri) |
17 | 38 |
|
18 | | - if version_tuple[0] < 4: |
19 | | - LOGGER.warning( |
20 | | - "Support for pymongo<=3 (and thus MongoDB v3) is deprecated and will be " |
21 | | - "removed in the next minor release." |
22 | | - ) |
| 39 | + if key in _CLIENTS: |
| 40 | + return _CLIENTS[key] |
23 | 41 |
|
24 | | - LOGGER.info("Using: Real MongoDB (pymongo)") |
| 42 | + if backend == "mongodb": |
| 43 | + from pymongo import MongoClient |
25 | 44 |
|
26 | | - elif config.database_backend.value == "mongomock": |
| 45 | + LOGGER.info(f"Using: Real MongoDB (pymongo) @ {uri}") |
| 46 | + client = MongoClient(uri) |
| 47 | + elif backend == "mongomock": |
27 | 48 | from mongomock import MongoClient |
28 | 49 |
|
29 | | - LOGGER.info("Using: Mock MongoDB (mongomock)") |
| 50 | + LOGGER.info(f"Using: Mock MongoDB (mongomock) @ {uri}") |
| 51 | + client = MongoClient(uri) |
| 52 | + else: |
| 53 | + raise ValueError(f"Unsupported backend {backend}") |
30 | 54 |
|
31 | | - if config.database_backend.value in ("mongomock", "mongodb"): |
32 | | - CLIENT = MongoClient(config.mongo_uri) |
33 | | - return CLIENT |
| 55 | + _CLIENTS[key] = client |
| 56 | + return client |
34 | 57 |
|
35 | 58 |
|
36 | 59 | class MongoCollection(EntryCollection): |
|
0 commit comments