GitHub Action commited on
Commit
ee34d15
·
1 Parent(s): 8526308

deploy from github actions

Browse files
Files changed (2) hide show
  1. app/utils/hf_storage.py +5 -2
  2. tasks.py +43 -13
app/utils/hf_storage.py CHANGED
@@ -16,11 +16,14 @@ def get_hf_api():
16
  def upload_to_hf(file_path: str, object_name: str) -> str:
17
  """
18
  Sube un archivo local al dataset de Hugging Face y retorna su URL pública de descarga.
 
19
  """
20
  api = get_hf_api()
21
  if not api:
22
- logger.warning(f"[Mock HF] Subiendo {file_path} como {object_name}")
23
- return f"https://mock-hf.huggingface.co/{HF_STORAGE_REPO}/{object_name}"
 
 
24
 
25
  try:
26
  api.upload_file(
 
16
  def upload_to_hf(file_path: str, object_name: str) -> str:
17
  """
18
  Sube un archivo local al dataset de Hugging Face y retorna su URL pública de descarga.
19
+ Lanza una excepción si HF_TOKEN no está configurado o si la subida falla.
20
  """
21
  api = get_hf_api()
22
  if not api:
23
+ raise RuntimeError(
24
+ f"HF_TOKEN no está configurado en las variables de entorno. "
25
+ f"No se puede subir '{object_name}' a Hugging Face Dataset '{HF_STORAGE_REPO}'."
26
+ )
27
 
28
  try:
29
  api.upload_file(
tasks.py CHANGED
@@ -946,6 +946,7 @@ def procesar_cancion_completa(self, file_path, task_id, stems, trim_start="", tr
946
  self.update_state(state='PROGRESS', meta={'progress': 95, 'message': 'Subiendo stems a Hugging Face Dataset...'})
947
 
948
  hf_urls = {}
 
949
  from app.utils.hf_storage import upload_to_hf
950
 
951
  for stem_name, stem_path, ext in stems_to_upload:
@@ -956,17 +957,20 @@ def procesar_cancion_completa(self, file_path, task_id, stems, trim_start="", tr
956
  object_name = f"stems/{task_id}/{stem_name}.{ext}"
957
  public_url = upload_to_hf(stem_path, object_name)
958
  hf_urls[stem_name] = public_url
 
959
 
960
- # Eliminar de disco local de inmediato tras subir
961
  if os.path.exists(stem_path):
962
  os.remove(stem_path)
 
 
963
  except Exception as stem_err:
964
- logger.warning(f"[{task_id}] Fallo al subir '{stem_name}' a Hugging Face: {stem_err}")
965
- if isinstance(stem_err, TaskCancelledException):
966
- raise stem_err
967
 
968
- # Guardar URLs y configurar expiración
969
  if hf_urls:
 
970
  try:
971
  from datetime import timedelta
972
  db = SessionLocal()
@@ -986,19 +990,45 @@ def procesar_cancion_completa(self, file_path, task_id, stems, trim_start="", tr
986
  task_record.cloud_expires_at = datetime.utcnow() + timedelta(days=30)
987
  logger.info(f"[{task_id}] cloud_expires_at = {task_record.cloud_expires_at} (30 días, normal)")
988
 
 
 
 
 
 
989
  db.commit()
990
  db.close()
991
  except Exception as db_err:
992
  logger.error(f"[{task_id}] Error al registrar en DB: {db_err}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
993
 
994
- # Limpiar la carpeta física local output_base de inmediato para liberar espacio en el disco del worker
995
- try:
996
- import shutil
997
- if os.path.exists(output_base):
998
- shutil.rmtree(output_base, ignore_errors=True)
999
- logger.info(f"[{task_id}] Carpeta de salida local limpia exitosamente.")
1000
- except Exception as rmtree_err:
1001
- logger.warning(f"[{task_id}] Error limpiando carpeta local de salida: {rmtree_err}")
 
 
 
1002
 
1003
  # ==========================================
1004
  # ESTRUCTURA DE RETORNO PARA .NET
 
946
  self.update_state(state='PROGRESS', meta={'progress': 95, 'message': 'Subiendo stems a Hugging Face Dataset...'})
947
 
948
  hf_urls = {}
949
+ failed_stems = []
950
  from app.utils.hf_storage import upload_to_hf
951
 
952
  for stem_name, stem_path, ext in stems_to_upload:
 
957
  object_name = f"stems/{task_id}/{stem_name}.{ext}"
958
  public_url = upload_to_hf(stem_path, object_name)
959
  hf_urls[stem_name] = public_url
960
+ logger.info(f"[{task_id}] Stem '{stem_name}' subido exitosamente a HF.")
961
 
962
+ # Solo eliminar del disco si la subida fue exitosa
963
  if os.path.exists(stem_path):
964
  os.remove(stem_path)
965
+ except TaskCancelledException:
966
+ raise
967
  except Exception as stem_err:
968
+ logger.error(f"[{task_id}] Fallo al subir '{stem_name}' a Hugging Face: {stem_err}")
969
+ failed_stems.append(stem_name)
970
+ # NO eliminar el archivo local — queda para diagnóstico
971
 
 
972
  if hf_urls:
973
+ # Al menos algunos stems se subieron correctamente
974
  try:
975
  from datetime import timedelta
976
  db = SessionLocal()
 
990
  task_record.cloud_expires_at = datetime.utcnow() + timedelta(days=30)
991
  logger.info(f"[{task_id}] cloud_expires_at = {task_record.cloud_expires_at} (30 días, normal)")
992
 
993
+ if failed_stems:
994
+ logger.warning(f"[{task_id}] Tarea completada PARCIALMENTE. Stems que fallaron: {failed_stems}")
995
+ else:
996
+ logger.info(f"[{task_id}] Tarea completada. Todos los stems subidos correctamente.")
997
+
998
  db.commit()
999
  db.close()
1000
  except Exception as db_err:
1001
  logger.error(f"[{task_id}] Error al registrar en DB: {db_err}")
1002
+ else:
1003
+ # Ningún stem se subió — marcar como fallido
1004
+ logger.error(f"[{task_id}] TODOS los stems fallaron al subirse a Hugging Face. Marcando tarea como 'failed'.")
1005
+ try:
1006
+ db = SessionLocal()
1007
+ task_record = db.query(TaskModel).filter(TaskModel.task_id == task_id).first()
1008
+ if task_record:
1009
+ task_record.status = "failed"
1010
+ task_record.telegram_file_ids = None
1011
+ db.commit()
1012
+ db.close()
1013
+ except Exception as db_err:
1014
+ logger.error(f"[{task_id}] Error marcando tarea como fallida en DB: {db_err}")
1015
+ raise RuntimeError(
1016
+ f"[{task_id}] No se pudo subir ningún stem a Hugging Face Dataset '{HF_STORAGE_REPO}'. "
1017
+ f"Stems que fallaron: {[s[0] for s in stems_to_upload]}. "
1018
+ f"Verifica que HF_TOKEN esté configurado y que el dataset exista."
1019
+ )
1020
 
1021
+ # Limpiar la carpeta física local output_base solo si todos los stems se subieron OK
1022
+ if not failed_stems:
1023
+ try:
1024
+ import shutil
1025
+ if os.path.exists(output_base):
1026
+ shutil.rmtree(output_base, ignore_errors=True)
1027
+ logger.info(f"[{task_id}] Carpeta de salida local limpia exitosamente.")
1028
+ except Exception as rmtree_err:
1029
+ logger.warning(f"[{task_id}] Error limpiando carpeta local de salida: {rmtree_err}")
1030
+ else:
1031
+ logger.warning(f"[{task_id}] Carpeta local NO eliminada porque algunos stems fallaron al subirse: {failed_stems}")
1032
 
1033
  # ==========================================
1034
  # ESTRUCTURA DE RETORNO PARA .NET