From 560c784cf856728d6ef3914067ab595ba2a34b45 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Sat, 27 Jun 2020 03:08:30 +0200
Subject: [PATCH] Run sync job in sync mode only.

---
 src/app.py       | 17 ++++++++++++++---
 src/base/node.py | 32 ++++++++++++++++++++++++++++++++
 src/service.py   | 23 ++++-------------------
 src/sync.py      |  2 +-
 4 files changed, 51 insertions(+), 23 deletions(-)
 create mode 100644 src/base/node.py

diff --git a/src/app.py b/src/app.py
index 3b27786..a91fecd 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 0000000..4d8ee8d
--- /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 4cd1da8..ddb5c49 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 cc1c6c6..cfd189d 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
 
 
-- 
GitLab