Skip to content

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
Source: flowmachine/core/query_state.py

Events that trigger a transition to a new state.

Members

  • EXECUTE: execute

  • FINISH: finish

  • FINISH_RESET: finish_resetting

  • CANCEL: cancel

  • RESET: reset

  • ERROR: error

  • QUEUE: queue

Enum QueryState

QueryState
Source: flowmachine/core/query_state.py

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)
Source: flowmachine/core/query_state.py

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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as cancelled.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as queued.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as in the process of executing.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as completed.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as having finished resetting.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as having errored while running.

Returns
  • bool, tuple of QueryState

    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)
Source: flowmachine/core/query_state.py

Attempt to mark the query as in the process of resetting.

Returns
  • bool, tuple of QueryState

    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]
Source: flowmachine/core/query_state.py

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[flowmachine.core.query_state.QueryState, bool]

    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)
Source: flowmachine/core/query_state.py

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
Source: flowmachine/core/query_state.py

Returns
  • flowmachine.core.query_state.QueryState

    Current state of the query this state machine refers to

is_cancelled

is_cancelled
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query was previously queued or running but was cancelled

is_completed

is_completed
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query ran successfully and is in cache

is_errored

is_errored
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query failed to run with an error

is_executing

is_executing
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query is currently running

is_finished_executing

is_finished_executing
Source: flowmachine/core/query_state.py

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
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query has not been set running and may be queued to do so

is_queued

is_queued
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the the query's store method has been called and it has not begin running

is_resetting

is_resetting
Source: flowmachine/core/query_state.py

Returns
  • bool

    True if the query is currently being removed from cache, or recovered from cancellation or error