@@ -157,13 +157,28 @@ export const WorkspaceReadyPage: FC<WorkspaceReadyPageProps> = ({
157157 // Cancel build
158158 const cancelBuildMutation = useMutation ( cancelBuild ( workspace , queryClient ) ) ;
159159
160- // Build Timings. Fetch build timings only when the build job is completed.
161- const readyAgents = workspace . latest_build . resources
162- . flatMap ( ( r ) => r . agents )
163- . filter ( ( a ) => a && a . lifecycle_state !== "starting" ) ;
160+ // Workspace Timings.
164161 const timingsQuery = useQuery ( {
165- ...workspaceBuildTimings ( workspace . latest_build . id , readyAgents . length ) ,
162+ ...workspaceBuildTimings ( workspace . latest_build . id ) ,
163+
164+ // Fetch build timings only when the build job is completed.
166165 enabled : Boolean ( workspace . latest_build . job . completed_at ) ,
166+
167+ // Sometimes, the timings can be fetched before the agent script timings are
168+ // done or saved in the database so we need to conditionally refetch the
169+ // timings. To refetch the timings, I found the best way was to compare the
170+ // expected amount of script timings with the current amount of script
171+ // timings returned in the response.
172+ refetchInterval : ( data ) => {
173+ const expectedScriptTimingsCount = workspace . latest_build . resources
174+ . flatMap ( ( r ) => r . agents )
175+ . flatMap ( ( a ) => a ?. scripts )
176+ . filter ( ( s ) => s !== undefined ) . length ;
177+ const currentScriptTimingsCount = data ?. agent_script_timings ?. length ?? 0 ;
178+ return expectedScriptTimingsCount === currentScriptTimingsCount
179+ ? false
180+ : 1_000 ;
181+ } ,
167182 } ) ;
168183
169184 const runLastBuild = (
0 commit comments