ElasticsearchBackend
Elasticsearch Backend.
Attributes
| Attribute | Type | Description |
|---|---|---|
| index | string = celery | The name of the Elasticsearch index where task results are stored. |
| doc_type | string = null | The document type used for indexing and retrieving results within the Elasticsearch index. |
| scheme | string = http | The protocol used to connect to the Elasticsearch server, such as http or https. |
| host | string = localhost | The hostname or IP address of the Elasticsearch server. |
| port | integer = 9200 | The network port on which the Elasticsearch server is listening. |
| username | string = null | The username for authenticating with the Elasticsearch server via HTTP Basic Auth. |
| password | string = null | The password for authenticating with the Elasticsearch server via HTTP Basic Auth. |
| es_retry_on_timeout | boolean = false | Flag indicating whether the client should retry requests that fail due to a timeout. |
| es_timeout | integer = 10 | The maximum number of seconds to wait for a response from the Elasticsearch server before timing out. |
| es_max_retries | integer = 3 | The maximum number of times the client will attempt to retry a failed request. |
Constructor
Signature
def ElasticsearchBackend(
url: string = null,
*args: any = null,
**kwargs: any = null
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| url | string = null | The URL used to connect to the Elasticsearch server, containing scheme, host, port, and credentials. |
| *args | any = null | Variable length argument list passed to the parent class constructor. |
| **kwargs | any = null | Arbitrary keyword arguments passed to the parent class constructor. |
Methods
exception_safe_to_retry()
@classmethod
def exception_safe_to_retry(
exc: Exception
) - > boolean
Determines if a given exception is safe to retry based on Elasticsearch error codes and transport errors.
Parameters
| Name | Type | Description |
|---|---|---|
| exc | Exception | The exception instance caught during an Elasticsearch operation. |
Returns
| Type | Description |
|---|---|
boolean | True if the exception is a transient or retryable error like a timeout or specific HTTP status codes (401, 409, 500, 502, 504), otherwise False. |
get()
@classmethod
def get(
key: string
) - > any
Fetches the result associated with a specific key from the Elasticsearch index.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier for the task result document. |
Returns
| Type | Description |
|---|---|
any | The task result stored in the document's source, or None if the key is not found. |
set()
@classmethod
def set(
key: string,
value: any
) - > null
Stores a value in the backend for the given key without specifying a specific task state.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The unique identifier for the data being stored. |
| value | any | The data to be persisted in the Elasticsearch index. |
Returns
| Type | Description |
|---|---|
null | null |
encode()
@classmethod
def encode(
data: any
) - > any
Serializes task metadata, optionally encoding specific fields like 'result' and 'traceback' if full text saving is disabled.
Parameters
| Name | Type | Description |
|---|---|---|
| data | any | The task metadata or result object to be encoded for storage. |
Returns
| Type | Description |
|---|---|
any | The encoded data, either as a serialized string or a dictionary with encoded sub-fields. |
decode()
@classmethod
def decode(
payload: any
) - > any
Deserializes task metadata retrieved from the backend into Python objects.
Parameters
| Name | Type | Description |
|---|---|---|
| payload | any | The raw data or dictionary retrieved from Elasticsearch. |
Returns
| Type | Description |
|---|---|
any | The decoded task metadata, including deserialized results and tracebacks. |
mget()
@classmethod
def mget(
keys: array
) - > array
Retrieves multiple task results simultaneously for a list of keys.
Parameters
| Name | Type | Description |
|---|---|---|
| keys | array | A list of document IDs to fetch from the index. |
Returns
| Type | Description |
|---|---|
array | A list of task results corresponding to the provided keys. |
delete()
@classmethod
def delete(
key: string
) - > null
Removes a specific task result document from the Elasticsearch index.
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | The document ID to be deleted. |
Returns
| Type | Description |
|---|---|
null | null |
server()
@classmethod
def server() - > elasticsearch.Elasticsearch
Provides a cached Elasticsearch client instance, initializing the connection on the first access.
Returns
| Type | Description |
|---|---|
elasticsearch.Elasticsearch | The active Elasticsearch client used for all backend operations. |