Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transition is possible given the current state of the query.
Parameters
----------
event: QueryEvent
Event to trigger
Returns
-------
tuple of QueryState, bool
Returns a tuple of the new query state, and a bool indicating whether this method
call caused a transition to that state
"""
state, trigger_success = self.state_machine.trigger(event)
return QueryState(state.decode()), trigger_success
def __init__(self, redis_client: StrictRedis, query_id: str, db_id: str):
self.query_id = query_id
must_populate = redis_client.get(f"finist:{db_id}:{query_id}-state") is None
self.state_machine = Finist(
redis_client, f"{db_id}:{query_id}-state", QueryState.KNOWN
)
if must_populate: # Need to create the state machine for this query
self.state_machine.on(QueryEvent.QUEUE, QueryState.KNOWN, QueryState.QUEUED)
self.state_machine.on(
QueryEvent.EXECUTE, QueryState.QUEUED, QueryState.EXECUTING
)
self.state_machine.on(
QueryEvent.ERROR, QueryState.EXECUTING, QueryState.ERRORED
)
self.state_machine.on(
QueryEvent.FINISH, QueryState.EXECUTING, QueryState.COMPLETED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.QUEUED, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.ERROR, QueryState.EXECUTING, QueryState.ERRORED
)
self.state_machine.on(
QueryEvent.FINISH, QueryState.EXECUTING, QueryState.COMPLETED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.QUEUED, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.EXECUTING, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.RESET, QueryState.CANCELLED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.ERRORED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.FINISH_RESET, QueryState.RESETTING, QueryState.KNOWN
)
self.state_machine = Finist(
redis_client, f"{db_id}:{query_id}-state", QueryState.KNOWN
)
if must_populate: # Need to create the state machine for this query
self.state_machine.on(QueryEvent.QUEUE, QueryState.KNOWN, QueryState.QUEUED)
self.state_machine.on(
QueryEvent.EXECUTE, QueryState.QUEUED, QueryState.EXECUTING
)
self.state_machine.on(
QueryEvent.ERROR, QueryState.EXECUTING, QueryState.ERRORED
)
self.state_machine.on(
QueryEvent.FINISH, QueryState.EXECUTING, QueryState.COMPLETED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.QUEUED, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.EXECUTING, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.RESET, QueryState.CANCELLED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.ERRORED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
QueryEvent.CANCEL, QueryState.QUEUED, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.EXECUTING, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.RESET, QueryState.CANCELLED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.ERRORED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.FINISH_RESET, QueryState.RESETTING, QueryState.KNOWN
)
def __init__(self, redis_client: StrictRedis, query_id: str, db_id: str):
self.query_id = query_id
must_populate = redis_client.get(f"finist:{db_id}:{query_id}-state") is None
self.state_machine = Finist(
redis_client, f"{db_id}:{query_id}-state", QueryState.KNOWN
)
if must_populate: # Need to create the state machine for this query
self.state_machine.on(QueryEvent.QUEUE, QueryState.KNOWN, QueryState.QUEUED)
self.state_machine.on(
QueryEvent.EXECUTE, QueryState.QUEUED, QueryState.EXECUTING
)
self.state_machine.on(
QueryEvent.ERROR, QueryState.EXECUTING, QueryState.ERRORED
)
self.state_machine.on(
QueryEvent.FINISH, QueryState.EXECUTING, QueryState.COMPLETED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.QUEUED, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.CANCEL, QueryState.EXECUTING, QueryState.CANCELLED
)
self.state_machine.on(
QueryEvent.RESET, QueryState.CANCELLED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.ERRORED, QueryState.RESETTING
)
self.state_machine.on(
QueryEvent.RESET, QueryState.COMPLETED, QueryState.RESETTING
)
def current_query_state(self) -> QueryState:
"""
Returns
-------
QueryState
Current state of the query this state machine refers to
"""
return QueryState(self.state_machine.state().decode())