# 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" : {},
"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'")
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!")
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
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:
# 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