87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
import sys
|
|
import logging
|
|
|
|
from __version_sync_factuges__ import __version__
|
|
from datetime import datetime
|
|
from dateutil import tz
|
|
from config import setup_logging, load_config
|
|
from db import get_mysql_connection, get_factuges_connection, sync_invoices_factuges
|
|
from utils import obtener_fecha_ultima_ejecucion, actualizar_fecha_ultima_ejecucion, log_system_metrics, send_orders_mail
|
|
|
|
|
|
def main():
|
|
|
|
# Cargar la configuración
|
|
config = load_config()
|
|
|
|
local_tz = tz.gettz(config['LOCAL_TZ'])
|
|
|
|
# Logging
|
|
setup_logging()
|
|
|
|
logging.info("== START SYNC FACTUGES ==")
|
|
logging.info(f"Version: {__version__}")
|
|
logging.info(f"Environment: {config['ENVIRONMENT']}")
|
|
log_system_metrics()
|
|
|
|
conn_factuges = None
|
|
conn_mysql = None
|
|
try:
|
|
# Obtener la fecha de la última ejecución del programa
|
|
last_execution_date_utc = obtener_fecha_ultima_ejecucion(config['LAST_RUN_PATH'])
|
|
last_execution_date_local_tz = last_execution_date_utc.astimezone(
|
|
tz=local_tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
logging.info("Last execution (UTC): %s",
|
|
last_execution_date_utc.strftime("%Y-%m-%d %H:%M:%S %Z"))
|
|
logging.info("Last execution (Local time): %s",
|
|
last_execution_date_local_tz)
|
|
|
|
# Abrimos conexiones con una única transacción para que todo esté controlado
|
|
conn_factuges = get_factuges_connection(config)
|
|
conn_mysql = get_mysql_connection(config)
|
|
|
|
# Sincronizamos
|
|
logging.info(
|
|
f">>>>>>>>>>> INI Sync invoices FactuGES escritorio to FactuGES web")
|
|
sync_invoices_factuges(conn_factuges, conn_mysql, last_execution_date_local_tz)
|
|
|
|
# Confirmar los cambios
|
|
conn_mysql.commit()
|
|
conn_factuges.commit()
|
|
conn_factuges.close()
|
|
conn_mysql.close()
|
|
logging.info(f">>>>>>>>>>> FIN Sync invoices FactuGES escritorio to FactuGES web")
|
|
|
|
actualizar_fecha_ultima_ejecucion(config['LAST_RUN_PATH'])
|
|
|
|
# Enviar email
|
|
# send_orders_mail(inserted_orders)
|
|
|
|
logging.info("== END (0) ==")
|
|
sys.exit(0)
|
|
|
|
except Exception as e:
|
|
logging.error("Se ha producido un error en la última ejecución.")
|
|
logging.error(e)
|
|
logging.error("Traceback:", exc_info=True)
|
|
logging.info("== END (1) ==")
|
|
|
|
if conn_mysql is not None:
|
|
conn_mysql.rollback()
|
|
|
|
if conn_factuges is not None:
|
|
conn_factuges.rollback()
|
|
|
|
sys.exit(1)
|
|
|
|
finally:
|
|
if conn_factuges:
|
|
conn_factuges.close()
|
|
if conn_mysql:
|
|
conn_mysql.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|