-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver_lifecycle.py
80 lines (61 loc) · 1.95 KB
/
server_lifecycle.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
#!/usr/bin/env python3
from pymongo import MongoClient
import json
from kafka import KafkaProducer
import time
from flask import Flask, request, jsonify
client = MongoClient(
"mongodb+srv://mongo2mongo:[email protected]/myFirstDatabase?retryWrites=true&w=majority")
db = client["initialiser_db"]
collection = db["services"]
services = []
for x in collection.find():
services.append(x["service"])
print(services)
# print("i am here")
# get dynamic ip
# ip1 = "52.140.61.100"
ip1 = "20.207.107.115"
ip2 = "20.204.220.249"
def send_message(topic_name, message):
producer = KafkaProducer(bootstrap_servers=[
'20.219.102.74:9092'], value_serializer=lambda v: json.dumps(v).encode('utf-8'))
producer.send(topic_name, message)
no_of_services = len(services)
for i in range(0, len(services)-1, 2):
data1 = {
"command": "START",
"service": services[i]
}
data2 = {
"command": "START",
"service": services[i+1]
}
# data3 = {
# "command":"START",
# "service":services[i+2]
# }
send_message("service_{}".format(ip1), data1)
send_message("service_{}".format(ip2), data2)
# send_message("service_{}".format(ip3), data3)
print("service_{}".format(ip1))
print("service_{}".format(ip2))
# print("service_{}".format(ip3))
time.sleep(2)
# data3 = {
# "command":"START",
# "service":services[len(services)-1]
# }
# send_message("service_{}".format(ip1), data3)
# time.sleep(2)
app = Flask(__name__)
collection1 = db["running_services"]
@app.route('/initialiser/getService/<serviceName>', methods=['GET'])
def getServiceAddress(serviceName):
# print("hello")
doc = collection1.find_one({"name": serviceName})
ip = doc["ip"]
port = doc["port"]
return jsonify({"ip": ip, "port": port})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5010)