refactor: usar getLastChangedActive y eliminar changedActive de syncGroups

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
main
brobert 1 week ago
parent 28264f9369
commit 5efcbbc98b

@ -211,8 +211,11 @@ export class WebhookServer {
try { try {
const res = await GroupSyncService.syncGroups(); const res = await GroupSyncService.syncGroups();
GroupSyncService.refreshActiveGroupsCache(); GroupSyncService.refreshActiveGroupsCache();
if (Array.isArray((res as any).changedActive) && (res as any).changedActive.length > 0) { const changed = GroupSyncService.getLastChangedActive();
await GroupSyncService.syncMembersForGroups((res as any).changedActive); if (changed.length > 0) {
await GroupSyncService.syncMembersForGroups(changed);
} else {
await GroupSyncService.syncMembersForActiveGroups();
} }
} catch (e) { } catch (e) {
console.error('❌ Error handling groups.upsert:', e); console.error('❌ Error handling groups.upsert:', e);

@ -75,8 +75,9 @@ export class GroupSyncService {
private static _groupsIntervalMs: number | null = null; private static _groupsIntervalMs: number | null = null;
private static _groupsNextTickAt: number | null = null; private static _groupsNextTickAt: number | null = null;
private static _membersGlobalCooldownUntil: number = 0; private static _membersGlobalCooldownUntil: number = 0;
private static _lastChangedActive: string[] = [];
static async syncGroups(force: boolean = false): Promise<{ added: number; updated: number; changedActive: string[] }> { static async syncGroups(force: boolean = false): Promise<{ added: number; updated: number }> {
if (!this.shouldSync(force)) { if (!this.shouldSync(force)) {
return { added: 0, updated: 0 }; return { added: 0, updated: 0 };
} }
@ -189,7 +190,9 @@ export class GroupSyncService {
// Duración opcional // Duración opcional
Metrics.set('last_sync_duration_ms', Date.now() - (typeof startedAt !== 'undefined' ? startedAt : Date.now())); Metrics.set('last_sync_duration_ms', Date.now() - (typeof startedAt !== 'undefined' ? startedAt : Date.now()));
return { ...result, changedActive: newlyActivatedIds }; // Guardar lista de grupos que han pasado a activos para consumo externo
this._lastChangedActive = Array.isArray(newlyActivatedIds) ? newlyActivatedIds : [];
return result;
} catch (error) { } catch (error) {
console.error('Group sync failed:', error); console.error('Group sync failed:', error);
Metrics.inc('sync_errors_total'); Metrics.inc('sync_errors_total');
@ -1010,6 +1013,14 @@ export class GroupSyncService {
this.cacheActiveGroups(); this.cacheActiveGroups();
} }
public static getLastChangedActive(): string[] {
try {
return Array.from(this._lastChangedActive || []);
} catch {
return [];
}
}
public static startGroupsScheduler(): void { public static startGroupsScheduler(): void {
if (process.env.NODE_ENV === 'test') return; if (process.env.NODE_ENV === 'test') return;
if (this._groupsSchedulerRunning) return; if (this._groupsSchedulerRunning) return;

Loading…
Cancel
Save