flowmachine.core.query_state¶
Source: flowmachine/core/query_state.py
State machine for queries. Tracks where in a lifecycle a query is to allow waiting for a query to finish running, and reporting status to the user.
Enum QueryEvent¶
QueryEvent
Events that trigger a transition to a new state.
Members¶
-
execute
:execute
-
finish
:finish
-
finish_resetting
:finish_resetting
-
cancel
:cancel
-
reset
:reset
-
error
:error
-
queue
:queue
Enum QueryState¶
QueryState
Possible states for a query to be in.
Members¶
-
queued
:queued
-
completed
:completed
-
cancelled
:cancelled
-
executing
:executing
-
errored
:errored
-
resetting
:resetting
-
known
:known
Class QueryStateMachine¶
QueryStateMachine(redis_client: redis.client.Redis, query_id: str, db_id: str)
Implements a state machine for a query's lifecycle, backed by redis.
Each query, once instantiated, is in one of a number of possible states. - known, indicating that the query has been created, but not yet run, or queued for storage. - queued, which indicates that a query is going to be executed in future (i.e. store
has been called on it) - executing, for queries which are currently running in FlowDB - completed, indicating that the query has finished running successfully - errored, when a query has been run but failed to succeed - cancelled, when execution was terminated by the user - resetting, when a previously run query is being purged from cache When the query is in a queued, executing, or resetting state, methods which need to use the results of the query should wait. The wait_until_complete
method will block while the query is in any of these states. The initial state for the query is 'known'.
Attributes¶
Parameters¶
-
redis_client
:redis.client.Redis
Client for redis
-
query_id
:str
Unique query identifier
-
db_id
:str
FlowDB connection id
Note
Creating a new instance of a state machine for a query will not alter the state, as the state is persisted in redis.
Methods¶
cancel¶
cancel(self)
Attempt to mark the query as cancelled.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller triggered the query to be cancelled with this call
enqueue¶
enqueue(self)
Attempt to mark the query as queued.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller triggered the query to be queued with this call
execute¶
execute(self)
Attempt to mark the query as in the process of executing.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller marked the query as executing with this call
finish¶
finish(self)
Attempt to mark the query as completed.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller marked the query as finished with this call
finish_resetting¶
finish_resetting(self)
Attempt to mark the query as having finished resetting.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller marked the reset as complete with this call.
raise_error¶
raise_error(self)
Attempt to mark the query as having errored while running.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller marked the query as erroring with this call
reset¶
reset(self)
Attempt to mark the query as in the process of resetting.
Returns¶
-
bool
,tuple
ofQueryState
Returns a tuple of the new query state, and a bool indicating whether the caller triggered the query to be reset with this call
trigger_event¶
trigger_event(self, event: flowmachine.core.query_state.QueryEvent) -> Tuple[flowmachine.core.query_state.QueryState, bool]
Attempts to trigger a state transition - will only transition if a transition is possible given the current state of the query.
Parameters¶
-
event
:flowmachine.core.query_state.QueryEvent
Event to trigger
Returns¶
-
typing.Tuple
Returns a tuple of the new query state, and a bool indicating whether this method call caused a transition to that state
wait_until_complete¶
wait_until_complete(self, sleep_duration=1)
Blocks until the query is in a state where its result is determinate (i.e., one of "know", "errored", "completed", "cancelled").
current_query_state¶
current_query_state
Returns¶
-
flowmachine.core.query_state.QueryState
Current state of the query this state machine refers to
is_cancelled¶
is_cancelled
Returns¶
-
bool
True if the query was previously queued or running but was cancelled
is_completed¶
is_completed
Returns¶
-
bool
True if the query ran successfully and is in cache
is_errored¶
is_errored
Returns¶
-
bool
True if the query failed to run with an error
is_executing¶
is_executing
Returns¶
-
bool
True if the query is currently running
is_finished_executing¶
is_finished_executing
True if the query state is completed
or errored
. I.e., it was previously executing and is now finished either successfully or with a failure (but it wasn't cancelled manually).
Returns¶
bool
is_known¶
is_known
Returns¶
-
bool
True if the query has not been set running and may be queued to do so
is_queued¶
is_queued
Returns¶
-
bool
True if the the query's store method has been called and it has not begin running
is_resetting¶
is_resetting
Returns¶
-
bool
True if the query is currently being removed from cache, or recovered from cancellation or error