DatabaseBackend
The database result backend.
Attributes
| Attribute | Type | Description |
|---|---|---|
| subpolling_interval | float = 0.5 | ResultSet.iterate should sleep this much between each pool, to not bombard the database with queries. |
| task_cls | class = Task | The SQLAlchemy model class used to represent and store task results in the database. |
| taskset_cls | class = TaskSet | The SQLAlchemy model class used to represent and store group (taskset) results in the database. |
| always_retry | bool = True | Boolean flag indicating whether the backend should always retry database operations on failure to preserve backward compatibility. |
| max_retries | int = 3 | Maximum number of retry attempts for database operations when a retryable error occurs. |
| url | string | The database connection string used to connect to the result backend. |
| engine_options | dict | A dictionary of configuration options passed to the SQLAlchemy engine, such as pool settings. |
| short_lived_sessions | bool | Determines 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
| Name | Type | Description |
|---|---|---|
| dburi | string = null | The database connection URI. |
| engine_options | dict = null | Dictionary of SQLAlchemy engine configuration options. |
| url | string = null | The backend URL used for configuration via celery.app.backends.by_url. |
| **kwargs | dict | Additional keyword arguments including 'short_lived_sessions' and 'engine_callback'. |
Signature
def DatabaseBackend(
dburi: string = None,
engine_options: dict = None,
url: string = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| dburi | string = None | The database connection URI string |
| engine_options | dict = None | Configuration options passed to the SQLAlchemy engine |
| url | string = None | The 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
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| exc | Exception | The exception instance caught during a database operation |
Returns
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| exc | Exception | The retryable exception that triggered the handler |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| session_manager | [SessionManager](session/sessionmanager.md?sid=celery_backends_database_session_sessionmanager) = None | An optional session manager to use instead of the default instance |
Returns
| Type | Description |
|---|---|
Session | A 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
| Name | Type | Description |
|---|---|---|
| task_id | string | The unique identifier of the task |
Returns
| Type | Description |
|---|---|
boolean | True if a record for the task ID is found, False otherwise |
cleanup()
@classmethod
def cleanup() - > null
Delete expired meta-data.
Returns
| Type | Description |
|---|---|
null |