MongoBackend
MongoDB result backend.
Attributes
| Attribute | Type | Description |
|---|---|---|
| mongo_host | list = null | A list of host strings or a single MongoDB connection URI used to establish the connection. |
| host | string = localhost | The hostname of the MongoDB server to connect to when mongo_host is not provided. |
| port | integer = 27017 | The port number on which the MongoDB server is listening. |
| user | string = null | The username used for authenticating the connection to the MongoDB database. |
| password | string = null | The password used for authenticating the connection to the MongoDB database. |
| database_name | string = celery | The name of the MongoDB database where task and group metadata will be stored. |
| taskmeta_collection | string = celery_taskmeta | The name of the MongoDB collection used to store task state and results. |
| groupmeta_collection | string = celery_groupmeta | The name of the MongoDB collection used to store group result metadata. |
| max_pool_size | integer = 10 | The maximum number of connections allowed in the MongoDB connection pool. |
| options | dict = null | A dictionary of additional configuration parameters passed directly to the MongoClient. |
| supports_autoexpire | boolean = false | Indicates whether the backend supports automatic expiration of records via MongoDB TTL indexes. |
Constructor
Signature
def MongoBackend(
app: [Celery](../../app/base/celery.md?sid=celery_app_base_celery) = None,
kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| app | [Celery](../../app/base/celery.md?sid=celery_app_base_celery) = None | The Celery application instance. |
| kwargs | dict | Additional keyword arguments passed to the base backend. |
Methods
encode()
@classmethod
def encode(
data: any
) - > any
Serializes data for storage in MongoDB, wrapping binary data in BSON-compatible types if necessary.
Parameters
| Name | Type | Description |
|---|---|---|
| data | any | The task result or metadata to be encoded. |
Returns
| Type | Description |
|---|---|
any | The serialized data or a BSON Binary object suitable for MongoDB storage. |
decode()
@classmethod
def decode(
data: any
) - > any
Deserializes data retrieved from MongoDB back into its original Python representation.
Parameters
| Name | Type | Description |
|---|---|---|
| data | any | The encoded data retrieved from the database. |
Returns
| Type | Description |
|---|---|
any | The decoded task result or metadata. |
task_result_exists()
@classmethod
def task_result_exists(
task_id: string
) - > boolean
Check if a result exists in MongoDB for the given task ID.
Parameters
| Name | Type | Description |
|---|---|---|
| task_id | string | The unique identifier of the task to check. |
Returns
| Type | Description |
|---|---|
boolean | True if a record for the task ID exists, otherwise False. |
cleanup()
@classmethod
def cleanup() - > null
Delete expired meta-data.
Returns
| Type | Description |
|---|---|
null | null |
database()
@classmethod
def database() - > pymongo.database.Database
Get database from MongoDB connection.
Returns
| Type | Description |
|---|---|
pymongo.database.Database | The authenticated MongoDB database instance. |
collection()
@classmethod
def collection() - > pymongo.collection.Collection
Get the meta-data task collection.
Returns
| Type | Description |
|---|---|
pymongo.collection.Collection | The MongoDB collection used for task metadata, with an index on date_done. |
group_collection()
@classmethod
def group_collection() - > pymongo.collection.Collection
Get the meta-data task collection.
Returns
| Type | Description |
|---|---|
pymongo.collection.Collection | The MongoDB collection used for group metadata, with an index on date_done. |
expires_delta()
@classmethod
def expires_delta() - > datetime.timedelta
Calculates the expiration time as a timedelta object based on the backend settings.
Returns
| Type | Description |
|---|---|
datetime.timedelta | The duration after which results should expire. |
as_uri()
@classmethod
def as_uri(
include_password: boolean
) - > string
Return the backend as an URI.
Parameters
| Name | Type | Description |
|---|---|---|
| include_password | boolean | Whether to include the plain-text password in the returned URI. |
Returns
| Type | Description |
|---|---|
string | The connection URI, optionally with the password sanitized. |