Skip to content

Method StartHealthWatchdogAsync

Namespace: Belay.Core.Examples
Assembly: Belay.Core.dll

StartHealthWatchdogAsync()

Start a watchdog thread that monitors system health.

csharp
[Thread(Name = "health_watchdog", Priority = ThreadPriority.High)]
[PythonCode("\n        import _thread\n        import time\n        import gc\n        \n        def health_watchdog():\n            print('Starting health watchdog...')\n            last_check = time.ticks_ms()\n            \n            while globals().get('monitoring_active', False):\n                try:\n                    current_time = time.ticks_ms()\n                    \n                    # Check memory usage\n                    free_mem = gc.mem_free()\n                    if free_mem < 2000:\n                        print(f'WARNING: Low memory: {free_mem} bytes')\n                        gc.collect()  # Force garbage collection\n                    \n                    # Check if main monitoring is still running\n                    last_reading = globals().get('last_reading_time', 0)\n                    if time.ticks_diff(current_time, last_reading) > 30000:  # 30 second timeout\n                        print('WARNING: Main monitoring appears stalled')\n                    \n                    # Check error rate\n                    total_readings = globals().get('reading_count', 1)\n                    total_errors = globals().get('error_count', 0) \n                    error_rate = total_errors / total_readings\n                    if error_rate > 0.1:  # More than 10% errors\n                        print(f'WARNING: High error rate: {error_rate:.1%}')\n                    \n                    # Health report every 60 seconds\n                    if time.ticks_diff(current_time, last_check) > 60000:\n                        print(f'Health: {total_readings} readings, {total_errors} errors, {free_mem} bytes free')\n                        last_check = current_time\n                    \n                    time.sleep_ms(10000)  # Check every 10 seconds\n                    \n                except Exception as e:\n                    print(f'Watchdog error: {e}')\n                    time.sleep_ms(15000)  # Back off on watchdog errors\n            \n            print('Health watchdog stopped')\n        \n        _thread.start_new_thread(health_watchdog, ())\n    ", EnableParameterSubstitution = false)]
Task StartHealthWatchdogAsync()

Returns

Task

Released under the Apache License 2.0.