Skip to main content

RecoverableState

A class for managing persistent recoverable state using a Pydantic model.

This class facilitates state persistence to a KeyValueStore, allowing data to be saved and retrieved across migrations or restarts. It manages the loading, saving, and resetting of state data, with optional persistence capabilities.

The state is represented by a Pydantic model that can be serialized to and deserialized from JSON. The class automatically hooks into the event system to persist state when needed.

Type Parameters: TStateModel: A Pydantic BaseModel type that defines the structure of the state data. Typically, it should be inferred from the default_state constructor parameter.

Index

Methods

__init__

  • __init__(*, default_state, persist_state_key, persistence_enabled, persist_state_kvs_name, persist_state_kvs_id, logger): None
  • Initialize a new recoverable state object.


    Parameters

    • keyword-onlydefault_state: TStateModel

      The default state model instance to use when no persisted state is found. A deep copy is made each time the state is used.

    • keyword-onlypersist_state_key: str

      The key under which the state is stored in the KeyValueStore

    • optionalkeyword-onlypersistence_enabled: bool = False

      Flag to enable or disable state persistence

    • optionalkeyword-onlypersist_state_kvs_name: str | None = None

      The name of the KeyValueStore to use for persistence. If neither a name nor and id are supplied, the default store will be used.

    • optionalkeyword-onlypersist_state_kvs_id: str | None = None

      The identifier of the KeyValueStore to use for persistence. If neither a name nor and id are supplied, the default store will be used.

    • keyword-onlylogger: logging.Logger

      A logger instance for logging operations related to state persistence

    Returns None

initialize

  • Initialize the recoverable state.

    This method must be called before using the recoverable state. It loads the saved state if persistence is enabled and registers the object to listen for PERSIST_STATE events.


    Returns TStateModel

persist_state

  • async persist_state(event_data): None
  • Persist the current state to the KeyValueStore.

    This method is typically called in response to a PERSIST_STATE event, but can also be called directly when needed.


    Parameters

    • optionalevent_data: EventPersistStateData | None = None

      Optional data associated with a PERSIST_STATE event

    Returns None

reset

  • async reset(): None
  • Reset the state to the default values and clear any persisted state.

    Resets the current state to the default state and, if persistence is enabled, clears the persisted state from the KeyValueStore.


    Returns None

teardown

  • async teardown(): None
  • Clean up resources used by the recoverable state.

    If persistence is enabled, this method deregisters the object from PERSIST_STATE events and persists the current state one last time.


    Returns None

Properties

current_value

current_value: TStateModel

Get the current state.