From 906f0ed3cd05bc4ae710e4ba7c526b691a801f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 26 Mar 2019 23:02:24 +0200 Subject: [PATCH] Add RAII class for stopping monitors This way the state is encapsulated in the object and the required changes are done in one place. This makes the code reusable across all functions making it easier to implement better monitor alteration code. --- server/core/config_runtime.cc | 13 +++---------- server/core/internal/monitor.hh | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/server/core/config_runtime.cc b/server/core/config_runtime.cc index e0da08d49..0acb9b454 100644 --- a/server/core/config_runtime.cc +++ b/server/core/config_runtime.cc @@ -760,21 +760,14 @@ bool do_alter_monitor(Monitor* monitor, const char* key, const char* value) bool runtime_alter_monitor(Monitor* monitor, const char* key, const char* value) { - // If the monitor is already stopped, don't stop/start it. - bool was_running = (monitor->state() == MONITOR_STATE_RUNNING); - if (was_running) - { - MonitorManager::stop_monitor(monitor); - } + MonitorStop stop(monitor); bool success = do_alter_monitor(monitor, key, value); + if (success) { MonitorManager::monitor_serialize(monitor); } - if (was_running) - { - MonitorManager::start_monitor(monitor); - } + return success; } diff --git a/server/core/internal/monitor.hh b/server/core/internal/monitor.hh index 1c5d9c543..a0cd9d0f2 100644 --- a/server/core/internal/monitor.hh +++ b/server/core/internal/monitor.hh @@ -178,3 +178,28 @@ public: */ static void debug_wait_one_tick(); }; + +// RAII helper class for temprarily stopping monitors +class MonitorStop +{ +public: + MonitorStop(mxs::Monitor* monitor) + : m_monitor(monitor->state() == MONITOR_STATE_RUNNING ? monitor : nullptr) + { + if (m_monitor) + { + MonitorManager::stop_monitor(m_monitor); + } + } + + ~MonitorStop() + { + if (m_monitor) + { + MonitorManager::start_monitor(m_monitor); + } + } + +private: + mxs::Monitor* m_monitor; +};