Skip to content

Commit

Permalink
Pre-populate cache with service registry info
Browse files Browse the repository at this point in the history
Improve logging output
Code cleanup
  • Loading branch information
davidlougheed committed May 25, 2020
1 parent eeab117 commit ac239b3
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions chord_service_registry/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
CHORD_URL = os.environ.get("CHORD_URL", "http://127.0.0.1:5000/") # Own node's URL
CHORD_SERVICES_PATH = os.environ.get("CHORD_SERVICES", "chord_services.json")
with open(CHORD_SERVICES_PATH, "r") as f:
CHORD_SERVICES = [s for s in json.load(f) if not s.get("disabled", False)] # Skip disabled services
CHORD_SERVICES = [s for s in json.load(f) if not s.get("disabled")] # Skip disabled services


application = Flask(__name__)
Expand All @@ -48,29 +48,36 @@
application.register_error_handler(NotFound, flask_error_wrap(flask_not_found_error))


service_info_cache = {}
def get_service_url(artifact: str):
return urljoin(CHORD_URL, URL_PATH_FORMAT.format(artifact=artifact))


service_info_cache = {
# Pre-populate service-info cache with data for the current service
SERVICE_ARTIFACT: {**SERVICE_INFO, "url": get_service_url(SERVICE_ARTIFACT)},
}


def get_service(s):
s_artifact = s["type"]["artifact"]
s_url = urljoin(CHORD_URL, URL_PATH_FORMAT.format(artifact=s_artifact))

if s_artifact not in service_info_cache:
if s_artifact == SERVICE_ARTIFACT:
service_info_cache[s_artifact] = {**SERVICE_INFO, "url": s_url}
else:
print(urljoin(s_url + "/", "service-info"))

try:
r = requests.get(urljoin(s_url + "/", "service-info"), timeout=TIMEOUT)
if r.status_code != 200:
print(f"[{SERVICE_NAME}] Non-200 status code on {s_artifact}: {r.status_code}", file=sys.stderr)
return None
except requests.exceptions.Timeout:
print(f"Timeout on {s_artifact}", file=sys.stderr)
service_info_url = urljoin(f"{s_url}/", "service-info")

print(f"[{SERVICE_NAME}] Contacting {service_info_url}", flush=True)

try:
r = requests.get(service_info_url, timeout=TIMEOUT)
if r.status_code != 200:
print(f"[{SERVICE_NAME}] Non-200 status code on {s_artifact}: {r.status_code}", file=sys.stderr,
flush=True)
return None
except requests.exceptions.Timeout:
print(f"[{SERVICE_NAME}] Encountered timeout with {service_info_url}", file=sys.stderr, flush=True)
return None

service_info_cache[s_artifact] = {**r.json(), "url": s_url}
service_info_cache[s_artifact] = {**r.json(), "url": s_url}

return service_info_cache[s_artifact]

Expand Down

0 comments on commit ac239b3

Please sign in to comment.