queue to telegram retry
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -6,6 +6,7 @@ from managers.telegram_manager import TelegramManager
|
||||
|
||||
MESSAGE_DELAY = 3.0 # Tiempo de espera entre mensajes en segundos
|
||||
NOTIFIED_ARTICLES_LIMIT = 300 # Límite de artículos notificados a mantener en memoria
|
||||
RETRY_TIMES = 3
|
||||
|
||||
class QueueManager:
|
||||
def __init__(self):
|
||||
@@ -19,14 +20,14 @@ class QueueManager:
|
||||
self._processor_thread = threading.Thread(target=self._process_queue, daemon=True)
|
||||
self._processor_thread.start()
|
||||
|
||||
def add_to_queue(self, article, search_name=None, thread_id=None):
|
||||
def add_to_queue(self, article, search_name=None, thread_id=None, retry_times=RETRY_TIMES):
|
||||
# Verificar si el artículo ya ha sido enviado
|
||||
if article in self._notified_articles:
|
||||
return
|
||||
|
||||
if search_name is None:
|
||||
search_name = "Unknown"
|
||||
self._queue.put((search_name, article, thread_id))
|
||||
self._queue.put((search_name, article, thread_id, retry_times))
|
||||
self.logger.debug(f"Artículo añadido a la cola: {article.get_title()}")
|
||||
|
||||
self.add_to_notified_articles(article)
|
||||
@@ -45,16 +46,19 @@ class QueueManager:
|
||||
try:
|
||||
# Esperar hasta que haya un elemento en la cola (timeout de 1 segundo)
|
||||
try:
|
||||
search_name, article, thread_id = self._queue.get(timeout=1.0)
|
||||
search_name, article, thread_id, retry_times = self._queue.get(timeout=1.0)
|
||||
except queue.Empty:
|
||||
continue
|
||||
|
||||
# Procesar el artículo
|
||||
try:
|
||||
self._telegram_manager.send_telegram_article(search_name, article, thread_id)
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error al enviar artículo a Telegram: {e}")
|
||||
if retry_times > 0:
|
||||
self._queue.put((search_name, article, thread_id, retry_times - 1))
|
||||
else:
|
||||
self.logger.error(f"Artículo no enviado después de {RETRY_TIMES} intentos")
|
||||
finally:
|
||||
self._queue.task_done()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user