Fix auto-sync service not stopping when disabled

- Use global auto-sync service instance instead of creating new instances
- This ensures stopAutoSync() is called on the actual running service
- Fix .env file parsing to handle comments and empty lines properly
- Update both TRPC and REST API endpoints to use global instance
- Auto-sync service will now properly stop when disabled in settings
This commit is contained in:
Michel Roegl-Brunner
2025-10-24 22:09:16 +02:00
parent 8fc9b27f55
commit 926032e83b
3 changed files with 40 additions and 10 deletions

View File

@@ -160,8 +160,14 @@ export async function POST(request: NextRequest) {
// Reschedule auto-sync service with new settings
try {
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js');
const autoSyncService = new AutoSyncService();
const { getAutoSyncService } = await import('../../../../server/lib/autoSyncInit.js');
let autoSyncService = getAutoSyncService();
// If no global instance exists, create one
if (!autoSyncService) {
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js');
autoSyncService = new AutoSyncService();
}
if (settings.autoSyncEnabled) {
autoSyncService.scheduleAutoSync();

View File

@@ -503,7 +503,16 @@ export const scriptsRouter = createTRPCRouter({
}))
.mutation(async ({ input }) => {
try {
const autoSyncService = new AutoSyncService();
// Use the global auto-sync service instance
const { getAutoSyncService } = await import('~/server/lib/autoSyncInit');
let autoSyncService = getAutoSyncService();
// If no global instance exists, create one
if (!autoSyncService) {
const { AutoSyncService } = await import('~/server/services/autoSyncService');
autoSyncService = new AutoSyncService();
}
autoSyncService.saveSettings(input);
// Reschedule auto-sync if enabled

View File

@@ -155,13 +155,28 @@ export class AutoSyncService {
const existingKeys = new Set();
for (const line of lines) {
const [key] = line.split('=');
const trimmedKey = key?.trim();
if (trimmedKey && trimmedKey in settingsMap) {
// @ts-ignore - Dynamic key access is safe here
newLines.push(`${trimmedKey}=${settingsMap[trimmedKey]}`);
existingKeys.add(trimmedKey);
} else if (trimmedKey && !(trimmedKey in settingsMap)) {
const trimmedLine = line.trim();
// Skip empty lines and comments
if (!trimmedLine || trimmedLine.startsWith('#')) {
newLines.push(line);
continue;
}
const equalIndex = trimmedLine.indexOf('=');
if (equalIndex === -1) {
// Line doesn't contain '=', keep as is
newLines.push(line);
continue;
}
const key = trimmedLine.substring(0, equalIndex).trim();
if (key && key in settingsMap) {
// Replace existing setting
newLines.push(`${key}=${settingsMap[key]}`);
existingKeys.add(key);
} else {
// Keep other settings as is
newLines.push(line);
}
}