@@ -184,13 +184,31 @@ async def _update_groups(context: ContextTypes.DEFAULT_TYPE):
184184
185185from handlers .admin import admin_ids
186186
187+ _update_in_progress = False
188+
189+ async def _background_update (update : Update , context : ContextTypes .DEFAULT_TYPE ):
190+ global _update_in_progress
191+ try :
192+ await _update_groups (context )
193+ await context .bot .send_message (chat_id = update .effective_chat .id , text = "¡Grupos actualizados!" )
194+ except Exception as e :
195+ logger .error (f"Background update failed: { e } " , exc_info = True )
196+ await context .bot .send_message (chat_id = update .effective_chat .id , text = f"Error actualizando grupos: { e } " )
197+ finally :
198+ _update_in_progress = False
199+
187200async def actualizar_grupos (update : Update , context : ContextTypes .DEFAULT_TYPE ):
201+ global _update_in_progress
188202 user_id = update .effective_user .id
189203 if user_id not in admin_ids and str (user_id ) not in admin_ids :
190204 logger .warning (f"Unauthorized user { user_id } tried to access /actualizar_grupos" )
191205 return
192206
207+ if _update_in_progress :
208+ await update .message .reply_text ("Ya hay una actualización de grupos en progreso. Por favor, esperá a que termine." )
209+ return
210+
211+ _update_in_progress = True
193212 logger .info (f"Manual update of groups triggered by { user_id } " )
194- await update .message .reply_text ("Actualizando grupos (esto puede demorar varios minutos)..." )
195- await _update_groups (context )
196- await update .message .reply_text ("¡Grupos actualizados!" )
213+ await update .message .reply_text ("Actualizando grupos en segundo plano (esto puede demorar varios minutos)..." )
214+ asyncio .create_task (_background_update (update , context ))
0 commit comments