diff --git a/config/sample/sample-development.engine-api.ini b/config/sample/sample-development.engine-api.ini index 44d7c1faf40744343059b16e577bd64908092691..175300ae9e8c60e8df67a7f522fb0e682d0b083c 100644 --- a/config/sample/sample-development.engine-api.ini +++ b/config/sample/sample-development.engine-api.ini @@ -20,3 +20,21 @@ debug_flask="false" [api] api_port=8008 + +[federation] + +# Engine API supports two deployment models: +# +# - "main": Deployed together with some `engine` (Single instance or for redundant engines) +# - "sync": Independent deployment, in charge of syncing data of two main-nodes +# +# The `synch_host` identifies the host where data is gathered from/synced to, depended on the +# chosen `node_type`. +# + +node_type="main" +sync_host="http://localhost:8010" + +; node_type="sync" +; main_host_1="http://localhost:8008" +; main_host_2="http://localhost:8009" diff --git a/src/service.py b/src/service.py index 7e3155ab286d30235d715a0f814e6a22071ee799..aa2ded2e241be984b4d7568dbab4ffe2ddc2cc29 100644 --- a/src/service.py +++ b/src/service.py @@ -19,9 +19,18 @@ import datetime +from enum import Enum from models import PlayLog, TrackSchema +class NodeType(Enum): + """ + Types of API Server deployment models. + """ + MAIN = "main" + SYNC = "sync" + + class ApiService(): """ @@ -30,12 +39,35 @@ class ApiService(): config = None logger = None + node_type = None + sync_host = None + main_hosts = None def __init__(self, config, logger): + """ + Initialize Service. + """ self.config = config self.logger = logger + # Evaluate deployment mode + node_type = config.get("node_type") + if not node_type == NodeType.MAIN.value: + self.node_type = NodeType.SYNC + self.main_hosts = [ config.get("main_host_1"), config.get("main_host_2") ] + self.logger.info("Running in 'SYNC' mode. Syncing data of '%s'" % (self.main_hosts)) + + else: + self.node_type = NodeType.MAIN + + # Validate sync host + self.sync_host = config.get("sync_host") + if not self.sync_host: + raise ValueError("Invalid sync_host '%s'!" % self.sync_host) + + self.logger.info("Running in 'MAIN' mode. Pushing data to '%s'" % (self.sync_host)) + def current_track(self): """ @@ -71,9 +103,9 @@ class ApiService(): Get paginated playlog entries for since the given timestamp. Args: - since_time (datetime): Get entries after this timestamp (e.g. '2020-08-29T09:12:33.001Z') page (Integer): The number of items to skip before starting to collect the result set size (Integer): The numbers of items to return per page + since_time (datetime): Optionally, get entries after this timestamp (e.g. "2020-08-29T09:12:33.001Z") Returns: (List[PlayLogEntry])