Skip to content

Commit 569f4a3

Browse files
authored
update query refresh to include terminal states (#21)
1 parent 93afe3b commit 569f4a3

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

dune_client/client.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,22 @@ def refresh(self, query: Query, ping_frequency: int = 5) -> list[DuneRecord]:
119119
Sleeps `ping_frequency` seconds between each status request.
120120
"""
121121
job_id = self.execute(query).execution_id
122-
state = self.get_status(job_id).state
123-
while state != ExecutionState.COMPLETED:
124-
log.info(
125-
f"waiting for query execution {job_id} to complete: current state {state}"
126-
)
122+
status = self.get_status(job_id)
123+
while status.state not in ExecutionState.terminal_states():
124+
log.info(f"waiting for query execution {job_id} to complete: {status}")
127125
time.sleep(ping_frequency)
128-
state = self.get_status(job_id).state
126+
status = self.get_status(job_id)
129127

130-
full_response = self.get_result(job_id)
131-
assert (
132-
full_response.result is not None
133-
), f"Expected Results on completed execution status {full_response}"
134-
return full_response.result.rows
128+
if status.state == ExecutionState.COMPLETED:
129+
full_response = self.get_result(job_id)
130+
assert (
131+
full_response.result is not None
132+
), f"Expected Results on completed execution status {full_response}"
133+
return full_response.result.rows
134+
135+
if status.state == ExecutionState.CANCELLED:
136+
log.info("Execution Cancelled, returning empty record set")
137+
return []
138+
139+
log.error(status)
140+
raise Exception(f"{status}. Perhaps your query took too long to run!")

dune_client/models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class ExecutionState(Enum):
4242
EXECUTING = "QUERY_STATE_EXECUTING"
4343
PENDING = "QUERY_STATE_PENDING"
4444
CANCELLED = "QUERY_STATE_CANCELLED"
45+
FAILED = "QUERY_STATE_FAILED"
46+
47+
@classmethod
48+
def terminal_states(cls) -> set[ExecutionState]:
49+
"""
50+
Returns the terminal states (i.e. when a query execution is no longer executing
51+
"""
52+
return {cls.COMPLETED, cls.CANCELLED, cls.FAILED}
4553

4654

4755
@dataclass
@@ -116,6 +124,17 @@ def from_dict(cls, data: dict[str, Any]) -> ExecutionStatusResponse:
116124
times=TimeData.from_dict(data), # Sending the entire data dict
117125
)
118126

127+
def __str__(self) -> str:
128+
if self.state == ExecutionState.PENDING:
129+
return f"{self.state} (queue position: {self.queue_position})"
130+
if self.state == ExecutionState.FAILED:
131+
return (
132+
f"{self.state}: execution_id={self.execution_id}, "
133+
f"query_id={self.query_id}, times={self.times}"
134+
)
135+
136+
return f"{self.state}"
137+
119138

120139
@dataclass
121140
class ResultMetadata:

0 commit comments

Comments
 (0)