Files
ProxmoxVE-Local/src/server/lib/autoSyncInit.ts
Michel Roegl-Brunner 5acaf144fb Fix autosync toggle disable functionality
- Fix service instance management to use global instance for stopping autosync
- Add automatic saving when toggle is changed (no manual save required)
- Fix validation issue where custom sync type without cron expression caused 400 error
- Add comprehensive debugging and error handling
- Ensure .env file is properly updated with AUTO_SYNC_ENABLED value
- Improve service lifecycle management with proper state cleanup
- Add fallback logic for invalid sync interval configurations

Resolves issue where disabling autosync in GUI didn't update .env file or stop service
2025-10-24 22:15:24 +02:00

73 lines
1.9 KiB
TypeScript

import { AutoSyncService } from '~/server/services/autoSyncService';
let autoSyncService: AutoSyncService | null = null;
/**
* Initialize auto-sync service and schedule cron job if enabled
*/
export function initializeAutoSync(): void {
try {
console.log('Initializing auto-sync service...');
autoSyncService = new AutoSyncService();
// Load settings and schedule if enabled
const settings = autoSyncService.loadSettings();
if (settings.autoSyncEnabled) {
console.log('Auto-sync is enabled, scheduling cron job...');
autoSyncService.scheduleAutoSync();
} else {
console.log('Auto-sync is disabled');
}
console.log('Auto-sync service initialized successfully');
} catch (error) {
console.error('Failed to initialize auto-sync service:', error);
}
}
/**
* Stop auto-sync service and clean up cron jobs
*/
export function stopAutoSync(): void {
try {
if (autoSyncService) {
console.log('Stopping auto-sync service...');
autoSyncService.stopAutoSync();
autoSyncService = null;
console.log('Auto-sync service stopped');
}
} catch (error) {
console.error('Error stopping auto-sync service:', error);
}
}
/**
* Get the auto-sync service instance
*/
export function getAutoSyncService(): AutoSyncService | null {
return autoSyncService;
}
/**
* Set the auto-sync service instance (for external management)
*/
export function setAutoSyncService(service: AutoSyncService | null): void {
autoSyncService = service;
}
/**
* Graceful shutdown handler
*/
export function setupGracefulShutdown(): void {
const shutdown = (signal: string) => {
console.log(`Received ${signal}, shutting down gracefully...`);
stopAutoSync();
process.exit(0);
};
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGUSR2', () => shutdown('SIGUSR2')); // For nodemon
}