fix: if libvirt throw incorrect connection pointer then reconnect

This commit is contained in:
Alexis
2021-08-30 14:30:12 +09:30
parent 9bbcee8093
commit b887fe2d8d
2 changed files with 17 additions and 13 deletions

View File

@@ -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
};
}
};

View File

@@ -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();