]> git.mxchange.org Git - fba.git/blobdiff - fba/instances.py
Continued:
[fba.git] / fba / instances.py
index 9b0323a1faed8ea79206cc246b37e7f7b0072bde..b21e85b2f8a2e8362ecbc49c885f6b5928c8fb80 100644 (file)
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-from fba import fba
 import sys
+import time
+
+from fba import fba
 
 # Found info from node, such as nodeinfo URL, detection mode that needs to be
 # written to database. Both arrays must be filled at the same time or else
 # update_instance_data() will fail
-instance_data = {
+_pending = {
     # Detection mode: 'AUTO_DISCOVERY', 'STATIC_CHECKS' or 'GENERATOR'
     # NULL means all detection methods have failed (maybe still reachable instance)
     "detection_mode"     : {},
@@ -42,7 +44,7 @@ instance_data = {
     "last_error_details" : {},
 }
 
-def set_instance_data(key: str, domain: str, value: any):
+def set(key: str, domain: str, value: any):
     # NOISY-DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',value[]='{type(value)}' - CALLED!")
     if type(key) != str:
         raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
@@ -52,13 +54,13 @@ def set_instance_data(key: str, domain: str, value: any):
         raise ValueError("Parameter domain[]='{type(domain)}' is not 'str'")
     elif domain == "":
         raise ValueError(f"Parameter 'domain' cannot be empty")
-    elif not key in instance_data:
-        raise ValueError(f"key='{key}' not found in instance_data")
+    elif not key in _pending:
+        raise ValueError(f"key='{key}' not found in _pending")
     elif not fba.is_primitive(value):
         raise ValueError(f"value[]='{type(value)}' is not a primitive type")
 
     # Set it
-    instance_data[key][domain] = value
+    _pending[key][domain] = value
 
     # DEBUG: print("DEBUG: EXIT!")
 
@@ -70,9 +72,9 @@ def has_pending_instance_data(domain: str) -> bool:
         raise ValueError(f"Parameter 'domain' cannot be empty")
 
     has_pending = False
-    for key in instance_data:
-        # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',instance_data[key]()='{len(instance_data[key])}'")
-        if domain in instance_data[key]:
+    for key in _pending:
+        # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',_pending[key]()='{len(_pending[key])}'")
+        if domain in _pending[key]:
             has_pending = True
             break
 
@@ -88,23 +90,24 @@ def update_instance_data(domain: str):
     elif not has_pending_instance_data(domain):
         raise Exception(f"Domain '{domain}' has no pending instance data, but function invoked")
 
-    # DEBUG: print(f"DEBUG: Updating nodeinfo for domain='{domain}' ...")
+    # DEBUG: print(f"DEBUG: Updating instance data for domain='{domain}' ...")
     sql_string = ''
     fields = list()
-    for key in instance_data:
+    for key in _pending:
         # DEBUG: print("DEBUG: key:", key)
-        if domain in instance_data[key]:
-           # DEBUG: print(f"DEBUG: Adding '{instance_data[key][domain]}' for key='{key}' ...")
-           fields.append(instance_data[key][domain])
+        if domain in _pending[key]:
+           # DEBUG: print(f"DEBUG: Adding '{_pending[key][domain]}' for key='{key}' ...")
+           fields.append(_pending[key][domain])
            sql_string += f" {key} = ?,"
 
+    fields.append(time.time())
     fields.append(domain)
 
     if sql_string == '':
         raise ValueError(f"No fields have been set, but method invoked, domain='{domain}'")
 
     # DEBUG: print(f"DEBUG: sql_string='{sql_string}',fields()={len(fields)}")
-    sql_string = "UPDATE instances SET" + sql_string + " last_updated = TIME() WHERE domain = ? LIMIT 1"
+    sql_string = "UPDATE instances SET" + sql_string + " last_updated = ? WHERE domain = ? LIMIT 1"
     # DEBUG: print("DEBUG: sql_string:", sql_string)
 
     try:
@@ -113,17 +116,17 @@ def update_instance_data(domain: str):
 
         # DEBUG: print(f"DEBUG: Success! (rowcount={fba.cursor.rowcount })")
         if fba.cursor.rowcount == 0:
-            print(f"WARNING: Did not update any rows: domain='{domain}',fields()={len(fields)} - EXIT!")
+            # DEBUG: print(f"DEBUG: Did not update any rows: domain='{domain}',fields()={len(fields)} - EXIT!")
             return
 
         # DEBUG: print("DEBUG: Committing changes ...")
         fba.connection.commit()
 
-        # DEBUG: print("DEBUG: Deleting instance_data for domain:", domain)
-        for key in instance_data:
+        # DEBUG: print("DEBUG: Deleting _pending for domain:", domain)
+        for key in _pending:
             try:
                 # DEBUG: print("DEBUG: Deleting key:", key)
-                del instance_data[key][domain]
+                del _pending[key][domain]
             except:
                 pass