
    7j	                        d Z ddlmZ ddlmZmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZ dd	lmZ  ed
      ZdddZdddZy)a+  Heartbeat: periodic liveness + light metrics to the Central API.

The heartbeat must be resilient: if the API is unavailable the agent logs the
failure and keeps running, retrying on the next interval. It never crashes the
service and never blocks indefinitely (the API client has its own timeout).
    )annotations)datetimetimezone)Any)	ApiClientApiError)Config)system)load_agent_tokensave_statussave_last_error)
get_logger	heartbeatc           	     2   | j                   || j                  t        j                         t	        j
                  t        j                        j                         t        j                         t        j                         t        j                         ddS )zAssemble the heartbeat payload.

    The frozen heartbeat contract requires `agent_version` and `status`; the
    agent is identified by its X-Agent-Token header, not the body. Light metrics
    are sent as additional informational fields.
    )load_average
disk_usagememory)agent_versionstatus	server_idhostname	timestampmetrics)r   r   r
   r   r   nowr   utc	isoformatr   r   memory_usage)configr   s     C/var/www/downloads.wolfpanel.net/public_html/agent/src/heartbeat.pybuild_payloadr       so      --%%OO%\\(,,/99;"//1 ++-))+
     c                   | j                   dk(  rt        j                  d       yt        | |      }	 |j	                  |t        |              t        j                  d|       t        | d       y# t        $ rf}t        | t        |             |j                  dk(  r"t        | d       t        j                  d       nt        j                  d	|       Y d}~yd}~ww xY w)
u   Send a single heartbeat. Returns True on success, False on failure.

    Never raises — transport errors are caught and reported so the caller's
    loop continues.
    revokedz$Heartbeat skipped: Agent is revoked.Fzheartbeat ok (status=%s)NTi  z3Heartbeat rejected by Central; marked agent revokedz.heartbeat failed, will retry next interval: %s)r   logwarningr    r   r   infor   r   strstatus_coder   )r   
api_clientr   payloadexcs        r   	send_oncer,   +   s     }}	!:;FF+GW&6v&>?+V4% C)??c!	*KKMNKKH#Ns   =A1 1	C :ACC N)online)r   r	   r   r'   returnzdict[str, Any])r   r	   r)   r   r   r'   r.   bool)__doc__
__future__r   r   r   typingr   r)   r   r   r   r	   	discoveryr
   identityr   r   r   loggerr   r$   r    r,    r!   r   <module>r7      s;    # '  *   C C *r!   