🐛 Fix Docker integration actions timeouts

This commit is contained in:
ajnart
2022-07-22 18:08:32 +02:00
parent 5ccdf735ae
commit 385b4a3b24
3 changed files with 39 additions and 50 deletions

View File

@@ -9,19 +9,22 @@ import {
IconRefresh, IconRefresh,
IconRotateClockwise, IconRotateClockwise,
IconTrash, IconTrash,
IconX,
} from '@tabler/icons'; } from '@tabler/icons';
import axios from 'axios'; import axios from 'axios';
import Dockerode from 'dockerode'; import Dockerode from 'dockerode';
import { tryMatchService } from '../../../tools/addToHomarr'; import { tryMatchService } from '../../../tools/addToHomarr';
import { useConfig } from '../../../tools/state';
import { AddAppShelfItemForm } from '../../AppShelf/AddAppShelfItem'; import { AddAppShelfItemForm } from '../../AppShelf/AddAppShelfItem';
function sendDockerCommand(action: string, containerId: string, containerName: string) { function sendDockerCommand(
action: string,
containerId: string,
containerName: string,
reload: () => void
) {
showNotification({ showNotification({
id: containerId, id: containerId,
loading: true, loading: true,
title: `${action}ing container ${containerName.substring(1)}`, title: `${action}ing container ${containerName}`,
message: undefined, message: undefined,
autoClose: false, autoClose: false,
disallowClose: true, disallowClose: true,
@@ -29,7 +32,6 @@ function sendDockerCommand(action: string, containerId: string, containerName: s
axios axios
.get(`/api/docker/container/${containerId}?action=${action}`) .get(`/api/docker/container/${containerId}?action=${action}`)
.then((res) => { .then((res) => {
if (res.data.success === true) {
updateNotification({ updateNotification({
id: containerId, id: containerId,
title: `Container ${containerName} ${action}ed`, title: `Container ${containerName} ${action}ed`,
@@ -37,7 +39,6 @@ function sendDockerCommand(action: string, containerId: string, containerName: s
icon: <IconCheck />, icon: <IconCheck />,
autoClose: 2000, autoClose: 2000,
}); });
}
}) })
.catch((err) => { .catch((err) => {
updateNotification({ updateNotification({
@@ -47,6 +48,9 @@ function sendDockerCommand(action: string, containerId: string, containerName: s
message: err.response.data.reason, message: err.response.data.reason,
autoClose: 2000, autoClose: 2000,
}); });
})
.finally(() => {
reload();
}); });
} }
@@ -56,7 +60,6 @@ export interface ContainerActionBarProps {
} }
export default function ContainerActionBar({ selected, reload }: ContainerActionBarProps) { export default function ContainerActionBar({ selected, reload }: ContainerActionBarProps) {
const { config, setConfig } = useConfig();
const [opened, setOpened] = useBooleanToggle(false); const [opened, setOpened] = useBooleanToggle(false);
return ( return (
<Group> <Group>
@@ -78,19 +81,9 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
onClick={() => onClick={() =>
Promise.all( Promise.all(
selected.map((container) => selected.map((container) =>
sendDockerCommand('restart', container.Id, container.Names[0].substring(1)) sendDockerCommand('restart', container.Id, container.Names[0].substring(1), reload)
) )
) )
.catch((err) => {
showNotification({
color: 'red',
title: 'There was an error with your container.',
message: err.message,
icon: <IconX />,
autoClose: 2000,
});
})
.then(() => reload())
} }
variant="light" variant="light"
color="orange" color="orange"
@@ -103,9 +96,9 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
onClick={() => onClick={() =>
Promise.all( Promise.all(
selected.map((container) => selected.map((container) =>
sendDockerCommand('stop', container.Id, container.Names[0].substring(1)) sendDockerCommand('stop', container.Id, container.Names[0].substring(1), reload)
)
) )
).then(() => reload())
} }
variant="light" variant="light"
color="red" color="red"
@@ -118,9 +111,9 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
onClick={() => onClick={() =>
Promise.all( Promise.all(
selected.map((container) => selected.map((container) =>
sendDockerCommand('start', container.Id, container.Names[0].substring(1)) sendDockerCommand('start', container.Id, container.Names[0].substring(1), reload)
)
) )
).then(() => reload())
} }
variant="light" variant="light"
color="green" color="green"
@@ -140,7 +133,7 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
if (selected.length !== 1) { if (selected.length !== 1) {
showNotification({ showNotification({
autoClose: 5000, autoClose: 5000,
title: <Title order={4}>Please only add one service at a time!</Title>, title: <Title order={5}>Please only add one service at a time!</Title>,
color: 'red', color: 'red',
message: undefined, message: undefined,
}); });
@@ -158,18 +151,10 @@ export default function ContainerActionBar({ selected, reload }: ContainerAction
radius="md" radius="md"
onClick={() => onClick={() =>
Promise.all( Promise.all(
selected.map((container) => { selected.map((container) =>
if (container.State === 'running') { sendDockerCommand('remove', container.Id, container.Names[0].substring(1), reload)
return showNotification({ )
id: container.Id, )
title: `Failed to delete ${container.Names[0].substring(1)}`,
message: "You can't delete a running container",
autoClose: 1000,
});
}
return sendDockerCommand('remove', container.Id, container.Names[0].substring(1));
})
).then(() => reload())
} }
> >
Remove Remove

View File

@@ -53,7 +53,7 @@ export default function DownloadComponent() {
useEffect(() => { useEffect(() => {
setIsLoading(true); setIsLoading(true);
if (downloadServices.length === 0) return; if (downloadServices.length === 0) return;
const interval = setSafeInterval(() => { const interval = setInterval(() => {
// Send one request with each download service inside // Send one request with each download service inside
axios axios
.post('/api/modules/downloads') .post('/api/modules/downloads')
@@ -66,14 +66,16 @@ export default function DownloadComponent() {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error('Error while fetching torrents', error.response.data); console.error('Error while fetching torrents', error.response.data);
setIsLoading(false); setIsLoading(false);
clearInterval(interval);
showNotification({ showNotification({
title: 'Error fetching torrents', title: 'Error fetching torrents',
autoClose: false, autoClose: 1000,
disallowClose: true,
id: 'fail-torrent-downloads-module',
color: 'red', color: 'red',
message: message:
'Please check your config for any potential errors, check the console for more info', 'Please check your config for any potential errors, check the console for more info',
}); });
clearInterval(interval);
}); });
}, 5000); }, 5000);
}, []); }, []);

View File

@@ -43,8 +43,8 @@ export default function TotalDownloadsComponent() {
const totalDownloadSpeed = torrents.reduce((acc, torrent) => acc + torrent.downloadSpeed, 0); const totalDownloadSpeed = torrents.reduce((acc, torrent) => acc + torrent.downloadSpeed, 0);
const totalUploadSpeed = torrents.reduce((acc, torrent) => acc + torrent.uploadSpeed, 0); const totalUploadSpeed = torrents.reduce((acc, torrent) => acc + torrent.uploadSpeed, 0);
useEffect(() => { useEffect(() => {
if (downloadServices.length === 0) return;
const interval = setSafeInterval(() => { const interval = setSafeInterval(() => {
// Send one request with each download service inside
axios axios
.post('/api/modules/downloads') .post('/api/modules/downloads')
.then((response) => { .then((response) => {
@@ -54,14 +54,16 @@ export default function TotalDownloadsComponent() {
setTorrents([]); setTorrents([]);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error('Error while fetching torrents', error.response.data); console.error('Error while fetching torrents', error.response.data);
clearInterval(interval);
showNotification({ showNotification({
title: 'Error fetching torrents', title: 'Torrent speed module failed to fetch torrents',
autoClose: false, autoClose: 1000,
disallowClose: true,
id: 'fail-torrent-speed-module',
color: 'red', color: 'red',
message: message:
'Please check your config for any potential errors, check the console for more info', 'Error fetching torrents, please check your config for any potential errors, check the console for more info',
}); });
clearInterval(interval);
}); });
}, 1000); }, 1000);
}, [config.services]); }, [config.services]);