fix: installer starts the service reliably (retry + verify + installer.log), fails loudly; 1.1.6

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
gti
2026-09-24 10:54:19 -03:00
co-authored by Claude Sonnet 5
parent 7e229f4b84
commit 40a4d835fe
22 changed files with 153 additions and 35 deletions
+16 -9
View File
@@ -108,14 +108,13 @@ def install_flow(code: str, *, hub: str | None = None, install_dir_path: str | N
extract_payload(install)
started = heartbeat = False
service_detail = ""
if register_service:
common.install_service(install)
common.write_arp_uninstall(install)
if chosen: # without a Firebird database the service would only fail preflight in a loop
progress("Iniciando o serviço…")
if not common.service_running():
common.start_service()
started = common.wait_service_state("RUNNING", timeout=45)
started, service_detail = common.start_and_verify()
if started and wait_heartbeat:
progress("Aguardando o primeiro heartbeat…")
heartbeat = common.wait_for_heartbeat(result["agent_token"], result.get("hub_url") or hub, timeout=120)
@@ -125,7 +124,7 @@ def install_flow(code: str, *, hub: str | None = None, install_dir_path: str | N
except Exception: # noqa: BLE001 — best effort
pass
result.update(install_dir=str(install), agent_version=agent_version, firebird_path=chosen["path"] if chosen else None,
result.update(service_detail=service_detail, install_dir=str(install), agent_version=agent_version, firebird_path=chosen["path"] if chosen else None,
service_running=started, heartbeat=heartbeat, mode="INSTALL")
return result
@@ -149,8 +148,8 @@ def upgrade_flow(*, install_dir_path: str | None = None, progress: Callable[[str
common.write_arp_uninstall(install)
os.environ["AGENT_VERSION"] = version
progress("Iniciando o serviço…")
common.start_service()
started = common.wait_service_state("RUNNING", timeout=45)
common.ilog(f"upgrade to {version} from {existing.get('agent_id')}")
started, service_detail = common.start_and_verify()
heartbeat = False
try:
from gti_fiscal_agent.credential_store import read_credential
@@ -158,7 +157,7 @@ def upgrade_flow(*, install_dir_path: str | None = None, progress: Callable[[str
except Exception: # noqa: BLE001
pass
return {"agent_id": existing["agent_id"], "installation_id": existing["installation_id"], "install_dir": str(install), "agent_version": version,
"service_running": started, "heartbeat": heartbeat, "mode": "UPGRADE", "hub_url": existing["hub_url"]}
"service_running": started, "service_detail": service_detail, "heartbeat": heartbeat, "mode": "UPGRADE", "hub_url": existing["hub_url"]}
def extract_payload(install: Path) -> None:
@@ -195,8 +194,9 @@ def final_message(result: dict) -> str:
if result.get("service_running"):
return ("Instalação concluída e serviço em execução, mas o primeiro heartbeat ainda não foi confirmado.\n\n"
"O serviço continua tentando sozinho; acompanhe pelo painel. Nenhuma edição de arquivo é necessária.")
return ("Instalação concluída, mas o serviço não iniciou.\n\nVerifique o log em "
f"{result.get('install_dir')}\\logs\\agent.log ou contate o suporte.")
return ("ATENÇÃO: o serviço GTI Fiscal Agent NÃO iniciou.\n\n"
f"Motivo: {result.get('service_detail') or 'desconhecido'}\n"
f"Detalhes em {common.log_path()} e {result.get('install_dir')}\\logs\\agent.log.")
class SetupWizard:
@@ -343,12 +343,19 @@ def cli() -> int:
if not args.code:
result = upgrade_flow(install_dir_path=args.install_dir, progress=print)
print(f"UPGRADE_OK agent_id={result['agent_id']} service_running={result['service_running']} heartbeat={result['heartbeat']}")
if not result["service_running"]:
print(f"SERVICE_START_FAILED {result.get('service_detail')} (see {common.log_path()})", file=sys.stderr)
return 3
return 0
explicit = {"path": args.fdb, "user": args.fb_user, "password": args.fb_password or ""} if args.fdb else None
result = install_flow(args.code, hub=args.hub, install_dir_path=args.install_dir, register_service=not args.no_service,
firebird=explicit, require_firebird=not args.no_firebird, progress=print)
print(f"INSTALL_OK agent_id={result.get('agent_id')} install_dir={result.get('install_dir')} hub={result.get('hub_url')} "
f"service_running={result.get('service_running')} heartbeat={result.get('heartbeat')}")
if result.get("service_detail") and not result.get("service_running"): # a start was attempted and failed
if True:
print(f"SERVICE_START_FAILED {result.get('service_detail')} (see {common.log_path()})", file=sys.stderr)
return 3
return 0