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.
This commit is contained in:
Markus Mäkelä 2019-03-26 23:02:24 +02:00
parent ac0a3d1d47
commit 906f0ed3cd
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 28 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;
};