Persistent State
All important variables of the Strategy class, like the current state, accounting, etc. needs to be saved in the persistent state and not directly in the class variables. The reason for such a design is to be robust towards the machine shutting down at any moment. Should the machine crash for any reason, the strategy needs to be able to re-instanciate itself and retrieve its memory. When developing a strategy, assume that anything outside of the pesistent_state
can be lost at any moment. Would that affect the behaviour of the strategy?
# This
self.current_state = ...
# Becomes
self.persistent_state.current_state = ...
Each Strategy must have a ./templates/persistent_state_template.json
. The platform will use this template as a starting point when launching the Strategy.
{
"current_state": "INITIALIZATION",
"current_flowstatus": "PREPARING_ACTION",
"current_actions": [],
"current_substate": "NO_SUBSTATE",
"initialized": false,
"completed": false,
"last_message": "N/A"
}
Then a corresponding Pydantic model in models.py
in the strategy folder.
class PersistentState(PersistentStateBase):
current_state: State
current_flowstatus: InternalFlowStatus
current_actions: list[UUID]
current_substate: SubState
initialized: bool
completed: bool
last_message: str