Speed up when the button shows

This commit is contained in:
Owen 2025-06-03 21:04:08 -04:00
parent f438d2ddbf
commit 17919192e0
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
3 changed files with 10 additions and 19 deletions

View file

@ -154,6 +154,11 @@ async function triggerFetch(siteId: number) {
`Triggering fetch containers for site ${siteId} with Newt ${newt.newtId}`
);
fetchContainers(newt.newtId);
// clear the cache for this Newt ID so that the site has to keep asking for the containers
// this is to ensure that the site always gets the latest data
dockerSocketCache.del(`${newt.newtId}:dockerContainers`);
return { siteId, newtId: newt.newtId };
}

View file

@ -78,12 +78,6 @@ export const ContainersSelector: FC<ContainerSelectorProps> = ({
}
}, [isAvailable]);
useEffect(() => {
if (isAvailable && containers.length === 0) {
fetchContainers();
}
}, [isAvailable, containers.length]);
if (!site || !isAvailable) {
return null;
}

View file

@ -121,16 +121,12 @@ export function useDockerSocket(siteId: number) {
if (error?.response?.status === 425) {
if (attempt < maxRetries) {
// Ask the newt server to check containers
await getContainers();
// Exponential backoff: 2s, 4s, 8s...
const retryDelay = Math.min(
2000 * Math.pow(2, attempt - 1),
10000
);
await fetchContainerList();
console.log(
`Containers not ready yet (attempt ${attempt}/${maxRetries}). Retrying in ${retryDelay}ms...`
`Containers not ready yet (attempt ${attempt}/${maxRetries}). Retrying in 250ms...`
);
await sleep(retryDelay);
await sleep(250);
continue;
} else {
console.warn(
@ -166,7 +162,6 @@ export function useDockerSocket(siteId: number) {
const res = await api.post<AxiosResponse<TriggerFetchResponse>>(
`/site/${siteId}/docker/trigger`
);
await sleep(1000); // Wait a second before fetching containers
// TODO: identify a way to poll the server for latest container list periodically?
await fetchContainerList();
return res.data.data;
@ -188,11 +183,8 @@ export function useDockerSocket(siteId: number) {
}
checkDockerSocket();
const timeout = setTimeout(() => {
getDockerSocketStatus();
}, 3000);
return () => clearTimeout(timeout);
}, [isEnabled, isAvailable, checkDockerSocket, getDockerSocketStatus]);
return {