-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
72 lines (52 loc) · 1.77 KB
/
app.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
import os
import time
import sys
import threading
from kafka_monitor.consumer import Consumer
from kafka_monitor.producer import Producer
from utils.config import Config
from utils.file import File
from utils.utils import Utils
def start_mointoring(monitors: list, producer: Producer, consumer: Consumer):
if not len(monitors):
return
for task in monitors:
#Validate URL
if 'url' not in task:
print("CORRUPTED_URL_VALUE_ERROR with item => " + str(task))
continue
if not Utils.is_valid_URL(task['url']):
print("PARSE_URL_ERROR", task['url'])
continue
producer.append_task(task)
consumer.start()
# make sure the consumer is ready with setting up db etc.
# before start producing
while not consumer.is_ready():
print("consumer not ready yet .. ")
time.sleep(1)
producer.start()
def stop_monitor(producer: Producer, consumer: Consumer):
if not producer or not consumer:
return
producer.stop()
consumer.stop()
def run(topic: str, db: str, table: str, filepath=None):
if not filepath:
filepath = Config.MONITERFILE
interval = File.read_time_interval(filepath)
monitors = File.read_monitors(filepath)
producer = Producer(topic, interval)
consumer = Consumer(topic, db, table)
start_mointoring(monitors, producer, consumer)
return producer, consumer
if __name__ == '__main__':
if len(sys.argv) > 1:
run(Config.K_MONITOR_TOPIC,
Config.PS_DATABASE_NAME,
Config.PS_WEBSITE_TABLE_NAME,
sys.argv[1])
else:
run(Config.K_MONITOR_TOPIC,
Config.PS_DATABASE_NAME,
Config.PS_WEBSITE_TABLE_NAME)