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+ if proc is not None :
18+ print ("Terminating stuck server..." )
19+ try :
20+ proc .terminate ()
21+ except Exception as e :
22+ print (f"Error terminating server: { e } " )
23+ print ("Restarting server..." )
24+ proc = Popen (["./run-issuer.sh" ])
25+ print ("Server restarted." )
26+
27+ if __name__ == "__main__" :
28+ parser = argparse .ArgumentParser (description = "Monitor server function" )
29+ parser .add_argument (
30+ "--host" ,
31+ type = str ,
32+ required = False ,
33+ default = DEFAULT_HOST ,
34+ help = f"URL to watch for changes, default: { DEFAULT_HOST } " ,
35+ )
36+ parser .add_argument (
37+ "--timeout" ,
38+ type = int ,
39+ required = False ,
40+ default = DEFAULT_TIMEOUT ,
41+ help = f"The timeout to use, default: { DEFAULT_TIMEOUT } " ,
42+ )
43+ args = parser .parse_args ()
44+
45+ url = f"{ args .host } /.well-known/openid-credential-issuer"
46+ print (f"Watching { url } for changes (every { DEFAULT_TIMEOUT } s)..." )
47+
48+ while True :
49+ try :
50+ response = requests .get (url , timeout = args .timeout , verify = False )
51+ response .raise_for_status ()
52+ except requests .exceptions .Timeout :
53+ print ("Request timed out." )
54+ restart ()
55+ except requests .exceptions .RequestException as e :
56+ print (f"An error occurred: { e } " )
57+ restart ()
58+ time .sleep (DEFAULT_TIMEOUT )
59+
0 commit comments