From 029a632e9d5e4c0592e20ac49b29364a861b1f74 Mon Sep 17 00:00:00 2001 From: brobert Date: Sat, 20 Sep 2025 20:20:39 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20a=C3=B1adir=20scheduler=20de=20grupos?= =?UTF-8?q?=20y=20arrancarlo=20al=20iniciar=20el=20servicio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/openai/gpt-5) --- src/server.ts | 8 ++++++++ src/services/group-sync.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/server.ts b/src/server.ts index b157965..5fe1b3a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -411,6 +411,14 @@ export class WebhookServer { // Initialize groups - critical for operation await GroupSyncService.checkInitialGroups(); + // Start groups scheduler (periodic sync of groups) + try { + GroupSyncService.startGroupsScheduler(); + console.log('✅ Group scheduler started'); + } catch (e) { + console.error('⚠️ Failed to start Group scheduler:', e); + } + // Initial members sync (non-blocking if fails) try { await GroupSyncService.syncMembersForActiveGroups(); diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index ece52ed..c79dc4a 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -66,6 +66,8 @@ export class GroupSyncService { return interval; } private static lastSyncAttempt = 0; + private static _groupsTimer: any = null; + private static _groupsSchedulerRunning = false; private static _membersTimer: any = null; private static _membersSchedulerRunning = false; @@ -615,6 +617,35 @@ export class GroupSyncService { this.cacheActiveGroups(); } + public static startGroupsScheduler(): void { + if (process.env.NODE_ENV === 'test') return; + if (this._groupsSchedulerRunning) return; + this._groupsSchedulerRunning = true; + + // Intervalo de grupos configurable; mínimo 10s en desarrollo + let interval = Number(process.env.GROUP_SYNC_INTERVAL_MS); + if (!Number.isFinite(interval) || interval <= 0) { + interval = 24 * 60 * 60 * 1000; // 24h por defecto + } + if (process.env.NODE_ENV === 'development' && interval < 10000) { + interval = 10000; + } + + this._groupsTimer = setInterval(() => { + this.syncGroups().catch(err => { + console.error('❌ Groups scheduler run error:', err); + }); + }, interval); + } + + public static stopGroupsScheduler(): void { + this._groupsSchedulerRunning = false; + if (this._groupsTimer) { + clearInterval(this._groupsTimer); + this._groupsTimer = null; + } + } + public static startMembersScheduler(): void { if (process.env.NODE_ENV === 'test') return; if (this._membersSchedulerRunning) return;