Skip to content

Commit 613e793

Browse files
adamruzickaclaude
andcommitted
Stop job invocation polling when the job finishes
handleSuccess unconditionally rescheduled the next poll, so the useEffect's stopJobInvocationPolling call raced against in-flight requests — the cleared timeout was immediately replaced by the completing fetch. Check the response data inside handleSuccess instead, and simplify the effect to only handle unmount cleanup. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 71de629 commit 613e793

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

webpack/JobInvocationDetail/JobInvocationActions.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ import {
66
CANCEL_RECURRING_LOGIC,
77
CHANGE_ENABLED_RECURRING_LOGIC,
88
JOB_INVOCATION_KEY,
9+
STATUS,
910
} from './JobInvocationConstants';
1011

1112
let pollTimeoutId = null;
1213

14+
const isJobFinished = ({ status_label: statusLabel, task }) => {
15+
const finished =
16+
statusLabel === STATUS.FAILED ||
17+
statusLabel === STATUS.SUCCEEDED ||
18+
statusLabel === STATUS.CANCELLED;
19+
const autoRefresh = task?.state === STATUS.PENDING || false;
20+
return finished && !autoRefresh;
21+
};
22+
1323
const scheduleNextPoll = (dispatch, url) => {
1424
pollTimeoutId = setTimeout(() => fetchJobInvocation(dispatch, url), 1000);
1525
};
@@ -20,7 +30,13 @@ const fetchJobInvocation = (dispatch, url, params = {}) => {
2030
key: JOB_INVOCATION_KEY,
2131
params,
2232
url,
23-
handleSuccess: () => scheduleNextPoll(dispatch, url),
33+
handleSuccess: ({ data }) => {
34+
if (!isJobFinished(data)) {
35+
scheduleNextPoll(dispatch, url);
36+
} else {
37+
pollTimeoutId = null;
38+
}
39+
},
2440
handleError: () => {
2541
pollTimeoutId = null;
2642
},

webpack/JobInvocationDetail/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,10 @@ const JobInvocationDetailPage = ({
7474

7575
useEffect(() => {
7676
dispatch(getJobInvocation(`/api/job_invocations/${id}`));
77-
if (finished && !autoRefresh) {
78-
stopJobInvocationPolling();
79-
}
8077
return () => {
8178
stopJobInvocationPolling();
8279
};
83-
}, [dispatch, id, finished, autoRefresh]);
80+
}, [dispatch, id]);
8481

8582
const pageStatus =
8683
items.id === undefined

0 commit comments

Comments
 (0)