-
Notifications
You must be signed in to change notification settings - Fork 1
/
callToServers.py
54 lines (37 loc) · 1.56 KB
/
callToServers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import threading
import time
import argparse
class CallToServers:
def __init__(self, server, callback = None):
self.server = server
self._stopScraping = threading.Event()
self.callback = callback
self.serverStatus = None
if self.callback:
threading.Thread(target=self._updater, args=(self.callback, self._stopScraping)).start()
def stopScraping(self):
self._stopScraping.set()
def CallToServer(self,):
return {'name':self.server,
'status':self.serverStatus,
}
def checkForUpdate(self, callback):
#perform req to the server and check for service
self.serverStatus = response_from_server
if callback:
callback(self.CallToServer())
else:
self.stopScraping
def _updater(self, callback, isScraping):
while not isScraping.is_set():
self.checkForUpdate(callback)
time.sleep(300) #sleep for 5mins before making next request to the server
def serverStatusDetails(ServerStatus):
name = ServerStatus['name']
status = ServerStatus['status']
print(f'Name -> {name}, Status -> {status}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Enter the server to send reqest')
parser.add_argument('--name', help='Name of the server', choices=['nginx', 'mongodb'], required=True)
args = parser.parse_args()
cs = CallToServers(args.name, serverStatusDetails)