mirror of
https://github.com/unraid/api.git
synced 2026-01-10 18:50:11 -06:00
fix: if libvirt throw incorrect connection pointer then reconnect
This commit is contained in:
@@ -60,13 +60,10 @@ export const getDomains = async (context: CoreContext): Promise<CoreResult> => {
|
||||
json: resolvedDomains
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error && error.message === 'Libvirt service is not running') {
|
||||
return {
|
||||
text: `Defined domains: ${JSON.stringify([], null, 2)}\nActive domains: ${JSON.stringify([], null, 2)}`,
|
||||
json: null
|
||||
};
|
||||
}
|
||||
|
||||
throw error;
|
||||
// If we hit an error expect libvirt to be offline
|
||||
return {
|
||||
text: `Defined domains: ${JSON.stringify([], null, 2)}\nActive domains: ${JSON.stringify([], null, 2)}`,
|
||||
json: null
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -49,9 +49,9 @@ libvirtDirWatcher.on('all', async (event, fileName) => {
|
||||
}
|
||||
});
|
||||
|
||||
export const getHypervisor = async () => {
|
||||
export const getHypervisor = async (useCache = true) => {
|
||||
// Return hypervisor if it's already connected
|
||||
if (hypervisor) {
|
||||
if (useCache && hypervisor) {
|
||||
return hypervisor;
|
||||
}
|
||||
|
||||
@@ -94,12 +94,12 @@ let cachedDomains: Array<{
|
||||
features: Record<string, unknown>;
|
||||
}>;
|
||||
|
||||
const watchLibvirt = async () => {
|
||||
const watchLibvirt = async (useCache = true) => {
|
||||
try {
|
||||
const hypervisor = await getHypervisor();
|
||||
const hypervisor = await getHypervisor(useCache);
|
||||
if (!hypervisor) {
|
||||
await sleep(1000);
|
||||
return watchLibvirt();
|
||||
return watchLibvirt(useCache);
|
||||
}
|
||||
|
||||
// We now have a hypervisor instance
|
||||
@@ -150,6 +150,13 @@ const watchLibvirt = async () => {
|
||||
await sleep(1_000);
|
||||
return watchLibvirt();
|
||||
} catch (error: unknown) {
|
||||
// We need to try and reconnect
|
||||
if ((error as Error).message.includes('invalid connection pointer')) {
|
||||
log.warn('Reconnecting to libvirt socket...');
|
||||
await sleep(5_000);
|
||||
return watchLibvirt(false);
|
||||
}
|
||||
|
||||
log.error('Failed watching libvirt with "%s"', error);
|
||||
await sleep(5_000);
|
||||
return watchLibvirt();
|
||||
|
||||
Reference in New Issue
Block a user