diff --git a/src/app.py b/src/app.py index 3b27786bb78f50dedcdeb6520c3fa3880b4d1d1b..a91fecdc145843ba441cc5a216699393a3669c75 100644 --- a/src/app.py +++ b/src/app.py @@ -27,6 +27,7 @@ import connexion from base.config import AuraConfig from base.logger import AuraLogger +from base.node import NodeType from rest import encoder from service import ApiService from sync import SyncJob @@ -76,10 +77,20 @@ with app.app_context(): Initialize Server. """ db.create_all() - service = ApiService(config, logger) + + # Evaluate deployment mode + node_type = NodeType.MAIN + if config.get("host_id") == 0: + node_type = NodeType.SYNC + + service = ApiService(config, logger, node_type) app.config['SERVICE'] = service - sync_job = SyncJob(config, logger, app) - sync_job.start() + + # Run sync job only in SYNC NODE mode + if node_type == NodeType.SYNC: + sync_job = SyncJob(config, logger, app) + sync_job.start() + atexit.register(shutdown) logger.info("Engine API server initialized.") diff --git a/src/base/node.py b/src/base/node.py new file mode 100644 index 0000000000000000000000000000000000000000..4d8ee8dc08852f6956a6feb93c6e28e865c2311a --- /dev/null +++ b/src/base/node.py @@ -0,0 +1,32 @@ + +# +# Aura Engine API (https://gitlab.servus.at/aura/engine-api) +# +# Copyright (C) 2020 - The Aura Engine Team. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +from enum import Enum + +from models import PlayLog, PlayLogSchema, TrackSchema, ActivityLog, HealthHistory, HealthHistorySchema + + + +class NodeType(Enum): + """ + Types of API Server deployment models. + """ + MAIN = "main" + SYNC = "sync" \ No newline at end of file diff --git a/src/service.py b/src/service.py index 4cd1da809d02cb45d60da7057823127f9fef0596..ddb5c496fbe2299aab00edf31f6e33d83bc50f9b 100644 --- a/src/service.py +++ b/src/service.py @@ -18,22 +18,13 @@ import datetime +import requests -from enum import Enum - +from base.node import NodeType from models import PlayLog, PlayLogSchema, TrackSchema, ActivityLog, HealthHistory, HealthHistorySchema -class NodeType(Enum): - """ - Types of API Server deployment models. - """ - MAIN = "main" - SYNC = "sync" - - - class ApiService(): """ Service handling for API actions. @@ -49,19 +40,13 @@ class ApiService(): api_healthlog = None - def __init__(self, config, logger): + def __init__(self, config, logger, node_type): """ Initialize Service. """ self.config = config self.logger = logger - # Evaluate deployment mode - node_type = NodeType.MAIN - host_id = config.get("host_id") - if host_id == 0: - node_type = NodeType.SYNC - # Configured as Sync Node if not node_type == NodeType.MAIN: self.node_type = NodeType.SYNC @@ -181,7 +166,7 @@ class ApiService(): else: self.logger.error("Error while pushing playlog to sync-node: " + r.json()) except Exception as e: - self.logger.error("Error while posting to sync-node API '%s'!" % (api_url), e) + self.logger.error("Error while posting to sync-node API '%s'!\n%s" % (api_url, str(e))) else: self.logger.info("Ditching playlog sent from an inactive source") diff --git a/src/sync.py b/src/sync.py index cc1c6c61d8f6bf6aa52ea6df84643984b2edf482..cfd189deacfc29bbc150fd17403851d31692f4ee 100644 --- a/src/sync.py +++ b/src/sync.py @@ -161,7 +161,7 @@ class SyncJob(threading.Thread): else: self.logger.error("Invalid status code while getting unsynced entries from remote API: " + str(response.status_code)) except Exception as e: - self.logger.error("Error while getting unsynced entries from remote API '%s'!" % (url), e) + self.logger.error("Error while getting unsynced entries from remote API '%s'!\n%s" % (url, str(e))) raise e