Skip to main content

DatabaseBackend

The database result backend.

Attributes

AttributeTypeDescription
subpolling_intervalfloat = 0.5ResultSet.iterate should sleep this much between each pool, to not bombard the database with queries.
task_clsclass = TaskThe SQLAlchemy model class used to represent and store task results in the database.
taskset_clsclass = TaskSetThe SQLAlchemy model class used to represent and store group (taskset) results in the database.
always_retrybool = TrueBoolean flag indicating whether the backend should always retry database operations on failure to preserve backward compatibility.
max_retriesint = 3Maximum number of retry attempts for database operations when a retryable error occurs.
urlstringThe database connection string used to connect to the result backend.
engine_optionsdictA dictionary of configuration options passed to the SQLAlchemy engine, such as pool settings.
short_lived_sessionsboolDetermines if database sessions should be closed immediately after use rather than being kept open.
session_manager[SessionManager](session/sessionmanager.md?sid=celery_backends_database_session_sessionmanager)The manager responsible for creating and handling SQLAlchemy session factories for database interactions.

Constructor

Signature

def DatabaseBackend(
dburi: string = null,
engine_options: dict = null,
url: string = null,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
dburistring = nullThe database connection URI.
engine_optionsdict = nullDictionary of SQLAlchemy engine configuration options.
urlstring = nullThe backend URL used for configuration via celery.app.backends.by_url.
**kwargsdictAdditional keyword arguments including 'short_lived_sessions' and 'engine_callback'.

Signature

def DatabaseBackend(
dburi: string = None,
engine_options: dict = None,
url: string = None
) - > null

Parameters

NameTypeDescription
dburistring = NoneThe database connection URI string
engine_optionsdict = NoneConfiguration options passed to the SQLAlchemy engine
urlstring = NoneThe backend URL used for configuration

Methods


extended_result()

@classmethod
def extended_result() - > boolean

Checks the application configuration to determine if extended result metadata should be stored.

Returns

TypeDescription
booleanTrue if extended results are enabled, False otherwise

exception_safe_to_retry()

@classmethod
def exception_safe_to_retry(
exc: Exception
) - > boolean

Determines if a database exception is transient and safe for a retry attempt.

Parameters

NameTypeDescription
excExceptionThe exception instance caught during a database operation

Returns

TypeDescription
booleanTrue if the exception is in the list of retryable database errors

on_backend_retryable_error()

@classmethod
def on_backend_retryable_error(
exc: Exception
) - > null

Handles retryable errors by invalidating the current session manager connection for the database URL.

Parameters

NameTypeDescription
excExceptionThe retryable exception that triggered the handler

Returns

TypeDescription
null

ResultSession()

@classmethod
def ResultSession(
session_manager: [SessionManager](session/sessionmanager.md?sid=celery_backends_database_session_sessionmanager) = None
) - > Session

Creates and returns a new database session using the configured session manager and engine options.

Parameters

NameTypeDescription
session_manager[SessionManager](session/sessionmanager.md?sid=celery_backends_database_session_sessionmanager) = NoneAn optional session manager to use instead of the default instance

Returns

TypeDescription
SessionA SQLAlchemy session object for database operations

task_result_exists()

@classmethod
def task_result_exists(
task_id: string
) - > boolean

Check if a result exists in the database for the given task ID.

Parameters

NameTypeDescription
task_idstringThe unique identifier of the task

Returns

TypeDescription
booleanTrue if a record for the task ID is found, False otherwise

cleanup()

@classmethod
def cleanup() - > null

Delete expired meta-data.

Returns

TypeDescription
null