Skip to content

Commit

Permalink
Extract superclass HealthApp
Browse files Browse the repository at this point in the history
… and dissolve CommonEndpointSpecs
  • Loading branch information
hannes-ucsc committed Nov 14, 2024
1 parent 580bc60 commit 71002ad
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 321 deletions.
77 changes: 2 additions & 75 deletions lambdas/indexer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
config,
)
from azul.chalice import (
AzulChaliceApp,
LambdaMetric,
)
from azul.deployment import (
aws,
)
from azul.health import (
HealthController,
HealthApp,
)
from azul.hmac import (
HMACAuthentication,
Expand All @@ -42,9 +41,6 @@
from azul.openapi.responses import (
json_content,
)
from azul.openapi.spec import (
CommonEndpointSpecs,
)

log = logging.getLogger(__name__)

Expand All @@ -65,12 +61,7 @@
}


class IndexerApp(AzulChaliceApp, SignatureHelper):

@cached_property
def health_controller(self) -> HealthController:
return self._controller(HealthController,
lambda_name=self.unqualified_app_name)
class IndexerApp(HealthApp, SignatureHelper):

@cached_property
def index_controller(self) -> IndexController:
Expand Down Expand Up @@ -108,70 +99,6 @@ def decorator(f):
def _authenticate(self) -> Optional[HMACAuthentication]:
return self.auth_from_request(self.current_request)

def default_routes(self):
common_specs = CommonEndpointSpecs(app_name=self.unqualified_app_name)

@self.route(
'/health',
methods=['GET'],
cors=True,
**common_specs.full_health
)
def health():
return self.health_controller.health()

@self.route(
'/health/basic',
methods=['GET'],
cors=True,
**common_specs.basic_health
)
def basic_health():
return self.health_controller.basic_health()

@self.route(
'/health/cached',
methods=['GET'],
cors=True,
**common_specs.cached_health
)
def cached_health():
return self.health_controller.cached_health()

@self.route(
'/health/fast',
methods=['GET'],
cors=True,
**common_specs.fast_health
)
def fast_health():
return self.health_controller.fast_health()

@self.route(
'/health/{keys}',
methods=['GET'],
cors=True,
**common_specs.custom_health
)
def custom_health(keys: Optional[str] = None):
return self.health_controller.custom_health(keys)

@self.metric_alarm(metric=LambdaMetric.errors,
threshold=1,
period=24 * 60 * 60)
@self.metric_alarm(metric=LambdaMetric.throttles)
@self.retry(num_retries=0)
# FIXME: Remove redundant prefix from name
# https://github.com/DataBiosphere/azul/issues/5337
@self.schedule(
'rate(1 minute)',
name=self.unqualified_app_name + 'cachehealth'
)
def update_health_cache(_event: chalice.app.CloudWatchEvent):
self.health_controller.update_cache()

return super().default_routes() | locals()


app = IndexerApp()
configure_app_logging(app, log)
Expand Down
73 changes: 2 additions & 71 deletions lambdas/service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import urllib.parse

import attr
import chalice
from chalice import (
BadRequestError as BRE,
ChaliceViewError,
Expand Down Expand Up @@ -52,9 +51,7 @@
OAuth2,
)
from azul.chalice import (
AzulChaliceApp,
C,
LambdaMetric,
)
from azul.collections import (
OrderedSet,
Expand All @@ -63,6 +60,7 @@
AccessMethod,
)
from azul.health import (
HealthApp,
HealthController,
)
from azul.indexer.document import (
Expand All @@ -80,9 +78,6 @@
responses,
schema,
)
from azul.openapi.spec import (
CommonEndpointSpecs,
)
from azul.plugins import (
ManifestFormat,
MetadataPlugin,
Expand Down Expand Up @@ -276,7 +271,7 @@
}


class ServiceApp(AzulChaliceApp):
class ServiceApp(HealthApp):

def spec(self) -> JSON:
return {
Expand Down Expand Up @@ -460,70 +455,6 @@ def manifest_url(self,
url = self.base_url.add(path=path)
return url.set(args=params)

def default_routes(self):
common_specs = CommonEndpointSpecs(app_name=self.unqualified_app_name)

@self.route(
'/health',
methods=['GET'],
cors=True,
**common_specs.full_health
)
def health():
return self.health_controller.health()

@self.route(
'/health/basic',
methods=['GET'],
cors=True,
**common_specs.basic_health
)
def basic_health():
return self.health_controller.basic_health()

@self.route(
'/health/cached',
methods=['GET'],
cors=True,
**common_specs.cached_health
)
def cached_health():
return self.health_controller.cached_health()

@self.route(
'/health/fast',
methods=['GET'],
cors=True,
**common_specs.fast_health
)
def fast_health():
return self.health_controller.fast_health()

@self.route(
'/health/{keys}',
methods=['GET'],
cors=True,
**common_specs.custom_health
)
def custom_health(keys: Optional[str] = None):
return self.health_controller.custom_health(keys)

@self.metric_alarm(metric=LambdaMetric.errors,
threshold=1,
period=24 * 60 * 60)
@self.metric_alarm(metric=LambdaMetric.throttles)
@self.retry(num_retries=0)
# FIXME: Remove redundant prefix from name
# https://github.com/DataBiosphere/azul/issues/5337
@self.schedule(
'rate(1 minute)',
name=self.unqualified_app_name + 'cachehealth'
)
def update_health_cache(_event: chalice.app.CloudWatchEvent):
self.health_controller.update_cache()

return super().default_routes() | locals()


app = ServiceApp()
configure_app_logging(app, log)
Expand Down
Loading

0 comments on commit 71002ad

Please sign in to comment.