-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
84 lines (67 loc) · 2.17 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import argparse
import sys
import requests
import time
import logging
#from datetime import datetime
from multiprocessing import Process
from time import sleep
from download.downloadScheduler import DownloadScheduler
# logger initialization
LOGGER = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", level=logging.INFO)
def ping_watchdog(process):
interval = 30 # ping interval in seconds
url = "localhost"
port = 5001
path = "/pingCheckIn/Data adapter"
while(process.is_alive()):
#print("{}: Pinging.".format(datetime.now()))
try:
r = requests.get("http://{}:{}{}".format(url, port, path))
except requests.exceptions.RequestException as e: # This is the correct syntax
logging.warning(e)
else:
logging.info('Successful ping at ' + time.ctime())
sleep(interval)
def start_scheduler(path):
scheduler = DownloadScheduler(configuration_path=path)
scheduler.run()
def main():
parser = argparse.ArgumentParser(description="scheduler")
parser.add_argument(
"-c",
"--config",
dest="config",
help=u"Config file located in ./config/ directory."
)
parser.add_argument(
"-w",
"--watchdog",
dest="watchdog",
action='store_true',
help=u"Ping watchdog",
)
# Display help if no arguments are defined
if (len(sys.argv) == 1):
parser.print_help()
sys.exit(1)
# Parse input arguments
args = parser.parse_args()
# Ping watchdog every 30 seconds if specfied
if (args.watchdog):
LOGGER.info("=== Watchdog started ===")
# Run and save a parelel process
# Start periodic download
process = Process(target=start_scheduler, args=(str(args.config),))
process.start()
# On the main thread ping watchdog if child process is alive
ping_watchdog(process)
else:
LOGGER.info("Starting scheduler")
scheduler = DownloadScheduler(configuration_path=args.config)
# Start periodic download
scheduler.run()
if (__name__ == '__main__'):
main()