1+ #!/usr/bin/env python3
2+
3+ import argparse
4+ import requests
5+ import time
6+ import urllib3
7+ from subprocess import Popen
8+
9+ urllib3 .disable_warnings (urllib3 .exceptions .InsecureRequestWarning )
10+
11+ DEFAULT_HOST = "https://snf-74864.ok-kno.grnetcloud.net:5000"
12+ DEFAULT_TIMEOUT = 5
13+
14+ proc = None
15+
16+ def restart ():
17+ global proc
18+ if proc is not None :
19+ print ("Terminating stuck server..." )
20+ try :
21+ proc .terminate ()
22+ except Exception as e :
23+ print (f"Error terminating server: { e } " )
24+ print ("Restarting server..." )
25+ proc = Popen (["./run-issuer.sh" ])
26+ print ("Server restarted." )
27+
28+ if __name__ == "__main__" :
29+ parser = argparse .ArgumentParser (description = "Monitor server function" )
30+ parser .add_argument (
31+ "--host" ,
32+ type = str ,
33+ required = False ,
34+ default = DEFAULT_HOST ,
35+ help = f"URL to watch for changes, default: { DEFAULT_HOST } " ,
36+ )
37+ parser .add_argument (
38+ "--timeout" ,
39+ type = int ,
40+ required = False ,
41+ default = DEFAULT_TIMEOUT ,
42+ help = f"The timeout to use, default: { DEFAULT_TIMEOUT } " ,
43+ )
44+ args = parser .parse_args ()
45+
46+ url = f"{ args .host } /.well-known/openid-credential-issuer"
47+ print (f"Watching { url } for changes (every { DEFAULT_TIMEOUT } s)..." )
48+
49+ while True :
50+ try :
51+ response = requests .get (url , timeout = args .timeout , verify = False )
52+ response .raise_for_status ()
53+ except requests .exceptions .Timeout :
54+ print ("Request timed out." )
55+ restart ()
56+ except requests .exceptions .RequestException as e :
57+ print (f"An error occurred: { e } " )
58+ restart ()
59+ time .sleep (DEFAULT_TIMEOUT )
60+
0 commit comments