- 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
77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
import { AutoSyncService } from '../services/autoSyncService.js';
|
|
|
|
let autoSyncService = null;
|
|
|
|
/**
|
|
* Initialize auto-sync service and schedule cron job if enabled
|
|
*/
|
|
export function initializeAutoSync() {
|
|
try {
|
|
console.log('Initializing auto-sync service...');
|
|
autoSyncService = new AutoSyncService();
|
|
console.log('AutoSyncService instance created');
|
|
|
|
// Load settings and schedule if enabled
|
|
const settings = autoSyncService.loadSettings();
|
|
console.log('Settings loaded:', settings);
|
|
|
|
if (settings.autoSyncEnabled) {
|
|
console.log('Auto-sync is enabled, scheduling cron job...');
|
|
autoSyncService.scheduleAutoSync();
|
|
console.log('Cron job scheduled');
|
|
} 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);
|
|
console.error('Error stack:', error.stack);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Stop auto-sync service and clean up cron jobs
|
|
*/
|
|
export function stopAutoSync() {
|
|
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() {
|
|
return autoSyncService;
|
|
}
|
|
|
|
/**
|
|
* Set the auto-sync service instance (for external management)
|
|
*/
|
|
export function setAutoSyncService(service) {
|
|
autoSyncService = service;
|
|
}
|
|
|
|
/**
|
|
* Graceful shutdown handler
|
|
*/
|
|
export function setupGracefulShutdown() {
|
|
const shutdown = (signal) => {
|
|
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
|
|
}
|