Skip to main content

MongoBackend

MongoDB result backend.

Attributes

AttributeTypeDescription
mongo_hostlist = nullA list of host strings or a single MongoDB connection URI used to establish the connection.
hoststring = localhostThe hostname of the MongoDB server to connect to when mongo_host is not provided.
portinteger = 27017The port number on which the MongoDB server is listening.
userstring = nullThe username used for authenticating the connection to the MongoDB database.
passwordstring = nullThe password used for authenticating the connection to the MongoDB database.
database_namestring = celeryThe name of the MongoDB database where task and group metadata will be stored.
taskmeta_collectionstring = celery_taskmetaThe name of the MongoDB collection used to store task state and results.
groupmeta_collectionstring = celery_groupmetaThe name of the MongoDB collection used to store group result metadata.
max_pool_sizeinteger = 10The maximum number of connections allowed in the MongoDB connection pool.
optionsdict = nullA dictionary of additional configuration parameters passed directly to the MongoClient.
supports_autoexpireboolean = falseIndicates 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

NameTypeDescription
app[Celery](../../app/base/celery.md?sid=celery_app_base_celery) = NoneThe Celery application instance.
kwargsdictAdditional 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

NameTypeDescription
dataanyThe task result or metadata to be encoded.

Returns

TypeDescription
anyThe 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

NameTypeDescription
dataanyThe encoded data retrieved from the database.

Returns

TypeDescription
anyThe 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

NameTypeDescription
task_idstringThe unique identifier of the task to check.

Returns

TypeDescription
booleanTrue if a record for the task ID exists, otherwise False.

cleanup()

@classmethod
def cleanup() - > null

Delete expired meta-data.

Returns

TypeDescription
nullnull

database()

@classmethod
def database() - > pymongo.database.Database

Get database from MongoDB connection.

Returns

TypeDescription
pymongo.database.DatabaseThe authenticated MongoDB database instance.

collection()

@classmethod
def collection() - > pymongo.collection.Collection

Get the meta-data task collection.

Returns

TypeDescription
pymongo.collection.CollectionThe 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

TypeDescription
pymongo.collection.CollectionThe 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

TypeDescription
datetime.timedeltaThe duration after which results should expire.

as_uri()

@classmethod
def as_uri(
include_password: boolean
) - > string

Return the backend as an URI.

Parameters

NameTypeDescription
include_passwordbooleanWhether to include the plain-text password in the returned URI.

Returns

TypeDescription
stringThe connection URI, optionally with the password sanitized.