11#include " TelegramMonitor.hpp"
22
33TelegramMonitor::TelegramMonitor (bool &isMonitoringEnable, CpuMonitor &cpu, MemoryMonitor &memory, const Settings settings, Log logger)
4- : isMonitoringEnable(isMonitoringEnable), cpu(cpu), memory(memory), settings(settings), logger(logger) {}
4+ : isMonitoringEnable(isMonitoringEnable), cpu(cpu), memory(memory), settings(settings), logger(logger), tgNotificationStatus( false ) {}
55
66/* *
77 * @brief Starts a new thread to handle Telegram bot requests.
@@ -70,6 +70,14 @@ void TelegramMonitor::startTelegramRequestThread()
7070 */
7171void TelegramMonitor::startTelegramNotificationWatchThread ()
7272{
73+ // if monitoring enabled
74+ if (this ->tgNotificationStatus )
75+ {
76+ return ;
77+ }
78+
79+ // if monitoring disabled
80+ this ->tgNotificationStatus = true ;
7381 notificationThread = std::thread (&TelegramMonitor::thread_telegramNotification, this );
7482 notificationThread.detach ();
7583}
@@ -285,15 +293,8 @@ void TelegramMonitor::thread_telegramNotification()
285293 TgBot::Bot bot (settings.getBotToken ());
286294
287295 // Check usage with limit
288- while (true )
296+ while (this -> tgNotificationStatus )
289297 {
290- // if monitoring disable
291- if (!this ->isMonitoringEnable )
292- {
293- std::this_thread::sleep_for (std::chrono::milliseconds (1000 ));
294- continue ;
295- }
296-
297298 // Check cpu limit
298299 if (settings.getCpuLimit () != 0 && settings.getCpuLimit () > 0 && cpu.getLastCpuUsage () >= settings.getCpuLimit ())
299300 {
@@ -311,3 +312,21 @@ void TelegramMonitor::thread_telegramNotification()
311312 std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
312313 }
313314}
315+
316+ /* *
317+ * @brief Stops the Telegram notification watching thread.
318+ *
319+ * This function sets the `tgNotificationStatus` flag to `false`,
320+ * indicating that the monitoring of Telegram notifications should
321+ * cease. As a result, the associated thread responsible for
322+ * observing notifications will be terminated, and the system will
323+ * no longer process incoming Telegram messages for monitoring.
324+ *
325+ * It is important to call this method when notification monitoring
326+ * is no longer needed to ensure proper resource management and
327+ * graceful termination of the associated thread.
328+ */
329+ void TelegramMonitor::stopTelegramNotificationWatchThread ()
330+ {
331+ this ->tgNotificationStatus = false ;
332+ }
0 commit comments