Skip to content

Commit 410b566

Browse files
committed
set flag for enable and disable monitoring telgram
1 parent 312a534 commit 410b566

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/library/TelegramMonitor.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "TelegramMonitor.hpp"
22

33
TelegramMonitor::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
*/
7171
void 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+
}

src/library/TelegramMonitor.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class TelegramMonitor
1717

1818
void startTelegramRequestThread();
1919
void startTelegramNotificationWatchThread();
20+
void stopTelegramNotificationWatchThread();
2021

2122
private:
2223
void thread_telegramBot();
@@ -27,6 +28,7 @@ class TelegramMonitor
2728
CpuMonitor &cpu;
2829
MemoryMonitor &memory;
2930
bool &isMonitoringEnable;
31+
bool tgNotificationStatus;
3032
std::thread botRequestThread;
3133
std::thread notificationThread;
3234
};

0 commit comments

Comments
 (0)