diff --git a/checker.py b/checker.py index 8b140fd..b30de40 100644 --- a/checker.py +++ b/checker.py @@ -202,7 +202,7 @@ def check_logs(logger, initial_sync_count, previous_status): formatted_id = format_number(last_checking_info["last_sub_id"]) if last_checking_info["num_subs_to_sync"] > 0: current_status_type = "Sync" - status_message = f"Sync: {formatted_id} ({current_sync_count})" # Use current_sync_count + status_message = f"Sync: {formatted_id})" # Use current_sync_count logger.info(f"Node is syncing. Last sub ID: {last_checking_info['last_sub_id']}, Num subs to sync: {last_checking_info['num_subs_to_sync']}") else: current_status_type = "OK" @@ -210,14 +210,14 @@ def check_logs(logger, initial_sync_count, previous_status): if previous_status == "Sync": current_sync_count += 1 # Increment local count logger.info(f"Sync completed. Sync count incremented to {current_sync_count}.") - status_message = f"OK: {formatted_id} ({current_sync_count})" # Use current_sync_count + status_message = f"OK: {formatted_id})" # Use current_sync_count logger.info(f"Node is OK. Last sub ID: {last_checking_info['last_sub_id']}") elif last_ignored_id: # Fallback to "Ignored" logs if "Checking" is missing formatted_id = format_number(last_ignored_id) current_status_type = "Sync" # Assume sync if we only see ignored creations recently - status_message = f"Sync: {formatted_id} ({current_sync_count})" # Use current_sync_count + status_message = f"Sync: {formatted_id}" # Use current_sync_count logger.info(f"Node possibly syncing (based on ignored logs). Last ignored ID: {last_ignored_id}") elif last_head_sub_id: @@ -225,7 +225,7 @@ def check_logs(logger, initial_sync_count, previous_status): formatted_id = format_number(last_head_sub_id) current_status_type = "OK" # Assume OK if this is the latest relevant info # Don't increment sync count here, only on Sync -> OK transition based on "Checking" logs - status_message = f"OK: {formatted_id} ({current_sync_count})" # Use current_sync_count + status_message = f"OK: {formatted_id}" # Use current_sync_count logger.info(f"Node status based on head sub id. Head sub ID: {last_head_sub_id}") else: @@ -284,9 +284,31 @@ if __name__ == "__main__": current_vm = grist.find_record(name=GRIST_ROW_NAME, table=NODES_TABLE)[0] def grist_callback(msg): grist.update(current_vm.id, msg, NODES_TABLE) - # Get initial state from Grist - initial_sync_count = int(current_vm.Syncs or 0) - reboot_count = int(current_vm.Reboots or 0) + # Initialize updates dictionary + initial_updates = {} + # Check and prepare update for Syncs if it's None or empty + if not current_vm.Syncs: # Handles None, empty string, potentially 0 if that's how Grist stores it + initial_updates["Syncs"] = 0 + # Check and prepare update for Reboots if it's None or empty + if not current_vm.Reboots: # Handles None, empty string, potentially 0 + initial_updates["Reboots"] = 0 + + # If there are updates, send them to Grist + if initial_updates: + try: + logger.info(f"Found empty initial values, updating Grist: {initial_updates}") + grist.update(current_vm.id, initial_updates, NODES_TABLE) + # Re-fetch the record to ensure subsequent logic uses the updated values + current_vm = grist.find_record(name=GRIST_ROW_NAME, table=NODES_TABLE)[0] + logger.info("Grist updated successfully with initial zeros.") + except Exception as e: + logger.error(f"Failed to update Grist with initial zeros: {e}") + # Decide how to proceed: maybe exit, maybe continue with potentially incorrect defaults + # For now, we'll log the error and continue using the potentially incorrect defaults from the first fetch + + # Get initial state from Grist (now potentially updated) + initial_sync_count = int(current_vm.Syncs or 0) # 'or 0' still useful as fallback + reboot_count = int(current_vm.Reboots or 0) # 'or 0' still useful as fallback # Determine previous status type based on Health string (simplified) previous_health_status = current_vm.Health or "Idle" previous_status_type = "Idle" # Default