
    i                         S r SSKrSSKrSSKrSSKrSSKJrJr  SSK	J	r	  SSSS.r
S	\S
\S\4S jr " S S\5      r\S:X  a  \R                   " SS9r\R%                  S\SSS9  \R%                  S\SSS9  \R)                  5       r\" \R,                  \R.                  4\5      r\" S\R,                   S\R.                   S35         \R5                  5         gg! \ a    \" S5        \R9                  5          gf = f)u   Unified health check endpoint for TipSharks stack.

Runs on port 8082 by default.
Reports health status of all three services.
Zero external dependencies — uses only stdlib.
    N)
HTTPServerBaseHTTPRequestHandler)datetimezhttp://localhost:9090/healthzhttp://localhost:8000/healthz http://localhost:8001/api/health)ztab-api-ingestztipsharks-elo-apiztipsharks-client-backendnameurlreturnc                 V    [         R                  R                  USS9 nUR                  5       nU(       a  [        R
                  " U5      O0 nU UUR                  S:X  a  SOSUS.sS S S 5        $ ! , (       d  f       g = f! [         R                  R                   a+  nU USSUR                   SUR                   3S	.s S nA$ S nAf[         R                  R                   a  nU US
SUR                   3S	.s S nA$ S nAf[         a  nU US
[        U5      S	.s S nA$ S nAff = f)N   )timeout   healthydegraded)r   r   statusresponsezHTTP z: )r   r   r   error	unhealthyzConnection failed: )urllibrequesturlopenreadjsonloadsr   r   	HTTPErrorcodereasonURLError	Exceptionstr)r   r   respbodydataes         ./root/tipsharks/scripts/health-check-server.pycheck_servicer$      s   
^^##C#3t99;D'+4::d#D'+{{c'9)z 	 433 <<!! 
 QVVHBqxxj1	
 	
 <<   
!*188*5	
 	
  
!V	
 	

s_   B AA2(	B 2
B <B  B D(! CD(!D((D;D(D(D#D(#D(c                        \ rS rSrS rS rSrg)HealthHandler9   c           	      v   U R                   S:X  Ga%  [        R                  5        VVs0 s H  u  pU[        X5      _M     nnn[	        S UR                  5        5       5      (       a  SOSnUU[        R                  " 5       R                  5       S-   S.nUS:X  a  SOSn[        R                  " US	S
9R                  5       nU R                  U5        U R                  SS5        U R                  S[        [        U5      5      5        U R!                  5         U R"                  R%                  U5        g U R                  S5        U R                  SS5        U R!                  5         U R"                  R%                  [        R                  " SU R                   S.5      R                  5       5        g s  snnf )N/healthc              3   0   #    U  H  oS    S:H  v   M     g7f)r   r   N ).0rs     r#   	<genexpr>'HealthHandler.do_GET.<locals>.<genexpr>B   s     J9IA{i/9Is   r   r   Z)r   services	timestampr   i     )indentzContent-Typezapplication/jsonzContent-Lengthi  z	Not found)r   path)r5   SERVICESitemsr$   allvaluesr   utcnow	isoformatr   dumpsencodesend_responsesend_headerr   lenend_headerswfilewrite)selfr   r   resultsoverallr   status_coder    s           r#   do_GETHealthHandler.do_GET:   sp   99	! "*!1!1ID mD..!1   J9IJJJ   "#%__.88:S@H ")I!5#3K::hq188:D{+^-?@-s3t9~>JJT"s#^-?@JJ

[$))DELLN9s   F5c                     g )Nr+   )rD   formatargss      r#   log_messageHealthHandler.log_message\   s        r+   N)__name__
__module____qualname____firstlineno__rH   rM   __static_attributes__r+   rO   r#   r&   r&   9   s     DrO   r&   __main__z%TipSharks unified health check server)descriptionz--porti  z!Port to listen on (default: 8082))typedefaulthelpz--bindz0.0.0.0z%Address to bind to (default: 0.0.0.0)z&Health check server running on http://:r)   z#
Shutting down health check server.)__doc__r   urllib.requestr   urllib.errorargparsehttp.serverr   r   r   r6   r   dictr$   r&   rP   ArgumentParserparseradd_argumentint
parse_argsrL   bindportserverprintserve_foreverKeyboardInterruptserver_closer+   rO   r#   <module>rm      s=       :  57 B
 
# 
$ 
D$* $N z$$;F sD/R   4	   DDII.>F	
01TYYKwO) *  45s   
C C=<C=