Skip to content

Commit

Permalink
Support caching for Watchman
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger committed May 21, 2019
1 parent 0ced46f commit 8cc272c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions gordo_components/watchman/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import logging
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
from typing import Iterable, Optional
from typing import Iterable, Optional, List

import requests
from cachetools import cached, TTLCache
from flask import Flask, jsonify, make_response, request, current_app
from flask.views import MethodView

Expand Down Expand Up @@ -77,6 +78,7 @@ def _check_endpoint(
-------
EndpointStatus
"""

endpoint = endpoint[1:] if endpoint.startswith("/") else endpoint
base_url = f'http://{host}/{endpoint.rstrip("/")}'
healthcheck_resp = requests.get(f"{base_url}/healthcheck", timeout=2)
Expand Down Expand Up @@ -119,10 +121,12 @@ def _check_endpoint(
),
)

def get(self):
@staticmethod
@cached(cache=TTLCache(maxsize=1024, ttl=5))
def _endpoints_statuses(n_logs: Optional[int]) -> List[dict]:

if "logs" in request.args:
# Get a list of ModelBuilderPod instances and map to target names
# Get a list of ModelBuilderPod instances and map to target names
if n_logs is not None:
builders = list_model_builders(
namespace=current_app.config["NAMESPACE"],
project_name=current_app.config["PROJECT_NAME"],
Expand All @@ -132,12 +136,10 @@ def get(self):
else:
builders = {}

n_logs = int(request.args.get("logs") or 20) if "logs" in request.args else None

with ThreadPoolExecutor(max_workers=25) as executor:
futures = {
executor.submit(
self._check_endpoint,
WatchmanApi._check_endpoint,
host=f'ambassador.{current_app.config["AMBASSADOR_NAMESPACE"]}',
namespace=current_app.config["NAMESPACE"],
project_name=current_app.config["PROJECT_NAME"],
Expand Down Expand Up @@ -183,6 +185,12 @@ def get(self):
},
}
)
return status_results

def get(self):

n_logs = int(request.args.get("logs") or 20) if "logs" in request.args else None
status_results = self._endpoints_statuses(n_logs)

payload = jsonify(
{
Expand Down Expand Up @@ -254,4 +262,4 @@ def run_server(
namespace=namespace,
ambassador_namespace=ambassador_namespace,
)
app.run(host, port, debug=debug)
app.run(host, port, debug=debug, threaded=False)

0 comments on commit 8cc272c

Please sign in to comment.