-
Notifications
You must be signed in to change notification settings - Fork 0
/
registerer.py
48 lines (35 loc) · 1.31 KB
/
registerer.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
import json
import requests
import logging
from requests.exceptions import ConnectionError
logging.basicConfig(level=logging.DEBUG)
DEFAULT_PORT = 3333
DEFAULT_LOCATION = "localhost"
DEFAULT_VERSION = "v1"
HEADERS = {'Content-Type': 'application/json'}
AGENT_ENDPOINT = "http://localhost:8080/register"
class ServiceRegisterer:
@staticmethod
def register(name, capacity=1, location=DEFAULT_LOCATION,
port=DEFAULT_PORT, version=DEFAULT_VERSION, service_url=AGENT_ENDPOINT):
# Basic info about this service
service_location = location + ":" + str(port)
reg = {
"name": name,
"capacity": capacity,
"location": service_location,
"version": version
}
# Try registering this service to Submit Agent
try:
res = requests.post(
service_url,
data=json.dumps([reg]),
headers=HEADERS)
status = res.status_code
logging.info("Registered: " + str(status))
except ConnectionError as ce:
logging.warning("Could not register this service to Submit Agent.")
logging.warning("Caused by: ")
logging.warning(ce)
logging.warning("Running as an independent service at: " + str(service_location))