91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { ClamAVManager } from '../../ts/classes.clamav.manager.js';
|
|
import { execAsync } from '../../ts/plugins.js';
|
|
|
|
let clamManager: ClamAVManager | null = null;
|
|
let isCleaningUp = false;
|
|
|
|
export async function getManager(): Promise<ClamAVManager> {
|
|
if (!clamManager) {
|
|
throw new Error('ClamAV manager not initialized');
|
|
}
|
|
return clamManager;
|
|
}
|
|
|
|
export async function setupClamAV(): Promise<ClamAVManager> {
|
|
console.log('[Helper] Setting up ClamAV...');
|
|
|
|
// First cleanup any existing containers
|
|
await forceCleanupContainer();
|
|
|
|
if (!clamManager) {
|
|
console.log('[Helper] Creating new ClamAV manager instance');
|
|
clamManager = new ClamAVManager();
|
|
await clamManager.startContainer();
|
|
console.log('[Helper] ClamAV manager initialized');
|
|
} else {
|
|
console.log('[Helper] Using existing ClamAV manager instance');
|
|
}
|
|
|
|
return clamManager;
|
|
}
|
|
|
|
export async function cleanupClamAV(): Promise<void> {
|
|
if (isCleaningUp) {
|
|
console.log('[Helper] Cleanup already in progress, skipping');
|
|
return;
|
|
}
|
|
|
|
isCleaningUp = true;
|
|
console.log('[Helper] Cleaning up ClamAV...');
|
|
|
|
try {
|
|
if (clamManager) {
|
|
await clamManager.stopContainer();
|
|
console.log('[Helper] ClamAV container stopped');
|
|
}
|
|
await forceCleanupContainer();
|
|
} catch (error) {
|
|
console.error('[Helper] Error during cleanup:', error);
|
|
throw error;
|
|
} finally {
|
|
clamManager = null;
|
|
isCleaningUp = false;
|
|
}
|
|
}
|
|
|
|
async function forceCleanupContainer(): Promise<void> {
|
|
try {
|
|
// Stop any existing container
|
|
await execAsync('docker stop clamav-daemon').catch(() => {});
|
|
// Remove any existing container
|
|
await execAsync('docker rm -f clamav-daemon').catch(() => {});
|
|
console.log('[Helper] Forced cleanup of existing containers complete');
|
|
} catch (error) {
|
|
// Ignore errors as the container might not exist
|
|
}
|
|
}
|
|
|
|
// Handle interrupts
|
|
process.on('SIGINT', async () => {
|
|
console.log('\n[Helper] Received SIGINT. Cleaning up...');
|
|
try {
|
|
await cleanupClamAV();
|
|
process.exit(0);
|
|
} catch (err) {
|
|
console.error('[Helper] Error during cleanup:', err);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
|
|
// Ensure cleanup on process exit
|
|
process.on('exit', () => {
|
|
if (clamManager && !isCleaningUp) {
|
|
console.log('[Helper] Process exit detected, attempting cleanup');
|
|
// We can't use async functions in exit handler, so we do our best
|
|
try {
|
|
execAsync('docker stop clamav-daemon').catch(() => {});
|
|
execAsync('docker rm -f clamav-daemon').catch(() => {});
|
|
} catch {}
|
|
}
|
|
});
|