Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task engine to increment run_count when entering RUNNING state #15436

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/prefect/task_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def begin_run(self):
new_state = Running()

self.task_run.start_time = new_state.timestamp
self.task_run.run_count += 1

flow_run_context = FlowRunContext.get()
if flow_run_context and flow_run_context.flow_run:
Expand Down Expand Up @@ -510,6 +511,7 @@ def handle_retry(self, exc: Exception) -> bool:
else:
delay = None
new_state = Retrying()
self.task_run.run_count += 1

self.logger.info(
"Task run failed with exception: %r - " "Retry %s/%s will start %s",
Expand Down Expand Up @@ -690,8 +692,11 @@ async def wait_until_ready(self):
if scheduled_time := self.state.state_details.scheduled_time:
sleep_time = (scheduled_time - pendulum.now("utc")).total_seconds()
await anyio.sleep(sleep_time if sleep_time > 0 else 0)
new_state = Retrying() if self.state.name == "AwaitingRetry" else Running()
if self.state.name == "AwaitingRetry":
self.task_run.run_count += 1
self.set_state(
Retrying() if self.state.name == "AwaitingRetry" else Running(),
new_state,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jw: would it be simpler to have an if new_state.is_running() then increment run count statement in set_state or will that have edge cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that does appear to work. Updated in 3fb2d68

force=True,
)

Expand Down Expand Up @@ -773,7 +778,6 @@ def call_task_fn(
if transaction.is_committed():
result = transaction.read()
else:
self.task_run.run_count += 1
if self.task_run.tags:
# Acquire a concurrency slot for each tag, but only if a limit
# matching the tag already exists.
Expand Down Expand Up @@ -882,6 +886,7 @@ async def begin_run(self):
new_state = Running()

self.task_run.start_time = new_state.timestamp
self.task_run.run_count += 1

flow_run_context = FlowRunContext.get()
if flow_run_context:
Expand Down Expand Up @@ -1024,6 +1029,7 @@ async def handle_retry(self, exc: Exception) -> bool:
else:
delay = None
new_state = Retrying()
self.task_run.run_count += 1

self.logger.info(
"Task run failed with exception: %r - " "Retry %s/%s will start %s",
Expand Down Expand Up @@ -1196,8 +1202,11 @@ async def wait_until_ready(self):
if scheduled_time := self.state.state_details.scheduled_time:
sleep_time = (scheduled_time - pendulum.now("utc")).total_seconds()
await anyio.sleep(sleep_time if sleep_time > 0 else 0)
new_state = Retrying() if self.state.name == "AwaitingRetry" else Running()
if self.state.name == "AwaitingRetry":
self.task_run.run_count += 1
await self.set_state(
Retrying() if self.state.name == "AwaitingRetry" else Running(),
new_state,
force=True,
)

Expand Down Expand Up @@ -1280,7 +1289,6 @@ async def call_task_fn(
if transaction.is_committed():
result = transaction.read()
else:
self.task_run.run_count += 1
if self.task_run.tags:
# Acquire a concurrency slot for each tag, but only if a limit
# matching the tag already exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def happy_path():
},
"flow_run_run_count": 1,
"name": task_run.name,
"run_count": 0,
"run_count": 1,
"tags": [],
"task_inputs": {},
"total_run_time": 0.0,
Expand Down Expand Up @@ -334,7 +334,7 @@ def happy_path():
},
"flow_run_run_count": 1,
"name": task_run.name,
"run_count": 0,
"run_count": 1,
"tags": [],
"task_inputs": {},
"total_run_time": 0.0,
Expand Down
Loading