@@ -680,13 +680,41 @@ async def test_load_monitors(clear_database):
680680 "(9999457, 'def get_value(): return 12');"
681681 )
682682
683- await monitors_loader ._load_monitors ()
683+ await monitors_loader ._load_monitors (None )
684684
685685 assert len (registry ._monitors ) == 2
686686 assert isinstance (registry ._monitors [9999123 ]["module" ], ModuleType )
687687 assert isinstance (registry ._monitors [9999456 ]["module" ], ModuleType )
688688
689689
690+ async def test_load_monitors_only_updated (clear_database ):
691+ """'_load_monitors' should load all enabled monitors that were updated after the provided
692+ timestamp"""
693+ await databases .execute_application (
694+ 'insert into "Monitors"(id, name, enabled) values'
695+ "(9999123, 'monitor_1', true),"
696+ "(9999456, 'internal.monitor_2', true),"
697+ "(9999457, 'disabled_monitor', false);"
698+ )
699+ await databases .execute_application (
700+ 'insert into "CodeModules"(monitor_id, code, registered_at) values'
701+ "(9999123, 'def get_value(): return 10', '2025-01-10 00:00'),"
702+ "(9999456, 'def get_value(): return 11', '2025-01-20 00:00'),"
703+ "(9999457, 'def get_value(): return 12', '2025-01-30 00:00');"
704+ )
705+
706+ registry .add_monitor (9999123 , "monitor_1" , ModuleType (name = "MockMonitorModule" ))
707+ await monitors_loader ._load_monitors (datetime (2025 , 1 , 15 , tzinfo = timezone .utc ))
708+
709+ assert len (registry ._monitors ) == 2
710+ assert isinstance (registry ._monitors [9999123 ]["module" ], ModuleType )
711+ assert isinstance (registry ._monitors [9999456 ]["module" ], ModuleType )
712+ # Only the monitor 9999456 was updated after the timestamp, so only it should be reloaded
713+ with pytest .raises (AttributeError ):
714+ registry ._monitors [9999123 ]["module" ].get_value ()
715+ assert registry ._monitors [9999456 ]["module" ].get_value () == 11
716+
717+
690718async def test_load_monitors_monitors_ready_flag (monkeypatch , clear_database ):
691719 """'_load_monitors' should clear and set the registry's 'monitors_ready' while loading the
692720 monitors"""
@@ -711,7 +739,7 @@ async def slow_get_all(self, *args, **kwargs):
711739 assert registry .monitors_ready .is_set ()
712740 assert registry .monitors_pending .is_set ()
713741
714- load_monitors_task = asyncio .create_task (monitors_loader ._load_monitors ())
742+ load_monitors_task = asyncio .create_task (monitors_loader ._load_monitors (None ))
715743
716744 await asyncio .sleep (0.1 )
717745 assert not registry .monitors_ready .is_set ()
@@ -722,30 +750,6 @@ async def slow_get_all(self, *args, **kwargs):
722750 assert not registry .monitors_pending .is_set ()
723751
724752
725- async def test_load_monitors_monitor_without_code_module (caplog , monkeypatch , clear_database ):
726- """'_load_monitors' should disable the monitor if it doesn't have a code module"""
727- monitor_get_all = Monitor .get_all
728-
729- async def slow_get_all (self , * args , ** kwargs ):
730- await asyncio .sleep (0.2 )
731- return await monitor_get_all (self , * args , ** kwargs )
732-
733- monkeypatch .setattr (Monitor , "get_all" , slow_get_all )
734-
735- await databases .execute_application (
736- "insert into \" Monitors\" (id, name, enabled) values (9999123, 'monitor_1', true);"
737- )
738-
739- await monitors_loader ._load_monitors ()
740-
741- monitor = await Monitor .get_by_id (9999123 )
742- assert monitor is not None
743- assert not monitor .enabled
744-
745- assert len (registry ._monitors ) == 0
746- assert_message_in_log (caplog , "Monitor 'monitor_1' has no code module, it will be disabled" )
747-
748-
749753async def test_load_monitors_error (caplog , clear_database ):
750754 """'_load_monitors' should load all the monitors from the database and add them to the
751755 registry, even if an error occurs while loading any of them. Monitors with errors will not be
@@ -761,7 +765,7 @@ async def test_load_monitors_error(caplog, clear_database):
761765 "(9999456, 'def get_value(): return 10');"
762766 )
763767
764- await monitors_loader ._load_monitors ()
768+ await monitors_loader ._load_monitors (None )
765769
766770 assert len (registry ._monitors ) == 1
767771 assert isinstance (registry ._monitors [9999456 ]["module" ], ModuleType )
0 commit comments