Fix autosync continuing to run after being disabled

- Add defensive check in cron job execution to stop if autosync is disabled
- Ensure isRunning flag is set to false when stopping autosync
- Add logging to show when autosync is disabled and not scheduling
- Fix both API route and TRPC router to properly stop service
- Prevent multiple cron jobs from running simultaneously

This fixes the issue where autosync would continue running even after
being disabled in the GUI, causing rate limit errors and unwanted syncs.
This commit is contained in:
Michel Roegl-Brunner
2025-10-24 22:20:13 +02:00
parent fdeda6c77a
commit 7b4daf8754
3 changed files with 13 additions and 13 deletions

View File

@@ -7,7 +7,6 @@ import { isValidCron } from 'cron-validator';
export async function POST(request: NextRequest) {
try {
const settings = await request.json();
console.log('Received auto-sync settings:', settings);
if (!settings || typeof settings !== 'object') {
return NextResponse.json(
@@ -46,14 +45,11 @@ export async function POST(request: NextRequest) {
// Validate sync interval type
if (!['predefined', 'custom'].includes(settings.syncIntervalType)) {
console.log('Invalid syncIntervalType:', settings.syncIntervalType);
return NextResponse.json(
{ error: 'syncIntervalType must be "predefined" or "custom"' },
{ status: 400 }
);
}
console.log('Sync interval validation - type:', settings.syncIntervalType, 'cron:', settings.syncIntervalCron);
// Validate predefined interval
if (settings.syncIntervalType === 'predefined') {
@@ -69,7 +65,6 @@ export async function POST(request: NextRequest) {
// Validate custom cron expression
if (settings.syncIntervalType === 'custom') {
if (!settings.syncIntervalCron || typeof settings.syncIntervalCron !== 'string' || settings.syncIntervalCron.trim() === '') {
console.log('Custom sync interval type but no cron expression provided, falling back to predefined');
// Fallback to predefined if custom is selected but no cron expression
settings.syncIntervalType = 'predefined';
settings.syncIntervalPredefined = settings.syncIntervalPredefined || '1hour';
@@ -159,10 +154,7 @@ export async function POST(request: NextRequest) {
}
// Write back to .env file
console.log('Writing to .env file:', envPath);
console.log('New .env content:', envContent);
fs.writeFileSync(envPath, envContent);
console.log('Successfully wrote to .env file');
// Reschedule auto-sync service with new settings
try {
@@ -177,17 +169,14 @@ export async function POST(request: NextRequest) {
}
// Update the global service instance with new settings
console.log('Updating global service instance with settings:', settings);
autoSyncService.saveSettings(settings);
if (settings.autoSyncEnabled) {
console.log('Enabling auto-sync...');
autoSyncService.scheduleAutoSync();
console.log('Auto-sync rescheduled with new settings');
} else {
console.log('Disabling auto-sync...');
autoSyncService.stopAutoSync();
console.log('Auto-sync stopped');
// Ensure the service is completely stopped and won't restart
autoSyncService.isRunning = false;
}
} catch (error) {
console.error('Error rescheduling auto-sync service:', error);

View File

@@ -523,6 +523,8 @@ export const scriptsRouter = createTRPCRouter({
console.log('Auto-sync rescheduled with new settings');
} else {
autoSyncService.stopAutoSync();
// Ensure the service is completely stopped and won't restart
autoSyncService.isRunning = false;
console.log('Auto-sync stopped');
}

View File

@@ -205,6 +205,7 @@ export class AutoSyncService {
const settings = this.loadSettings();
if (!settings.autoSyncEnabled) {
console.log('Auto-sync is disabled, not scheduling cron job');
return;
}
@@ -240,6 +241,14 @@ export class AutoSyncService {
return;
}
// Double-check that autosync is still enabled before executing
const currentSettings = this.loadSettings();
if (!currentSettings.autoSyncEnabled) {
console.log('Auto-sync has been disabled, stopping cron job');
this.stopAutoSync();
return;
}
console.log('Starting scheduled auto-sync...');
await this.executeAutoSync();
}, {