MXS-1929: Split housekeeper initialization

The initialization and starting of the housekeeper is now done
separately. This allows housekeeper tasks to be created when the services
are being created while still preventing the execution of the task before
the startup is complete.
This commit is contained in:
Markus Mäkelä
2018-07-21 12:26:31 +03:00
parent 843b8d92eb
commit a553ddba6e
3 changed files with 29 additions and 3 deletions

View File

@ -44,6 +44,13 @@ typedef bool (*TASKFN)(void *data);
*/ */
bool hkinit(); bool hkinit();
/**
* Start the housekeeper thread
*
* @return True if the housekeeper mechanism was started
*/
bool hkstart();
/** /**
* Waits for the housekeeper thread to finish. * Waits for the housekeeper thread to finish.
*/ */

View File

@ -1969,6 +1969,15 @@ int main(int argc, char **argv)
goto return_main; goto return_main;
} }
// Initialize the housekeeper
if (!hkinit())
{
const char* logerr = "Failed to initialize housekeeper";
print_log_n_stderr(true, true, logerr, logerr, 0);
rc = MAXSCALE_INTERNALERROR;
goto return_main;
}
if (!config_load(cnf_file_path)) if (!config_load(cnf_file_path))
{ {
const char* fprerr = const char* fprerr =
@ -2071,7 +2080,7 @@ int main(int argc, char **argv)
} }
// Start the housekeeper thread // Start the housekeeper thread
if (!hkinit()) if (!hkstart())
{ {
const char* logerr = "Failed to start housekeeper thread."; const char* logerr = "Failed to start housekeeper thread.";
print_log_n_stderr(true, true, logerr, logerr, 0); print_log_n_stderr(true, true, logerr, logerr, 0);

View File

@ -94,6 +94,7 @@ public:
Housekeeper(); Housekeeper();
static bool init(); static bool init();
static bool start();
void stop(); void stop();
void run(); void run();
void add(const Task& task); void add(const Task& task);
@ -130,11 +131,16 @@ Housekeeper::Housekeeper():
} }
bool Housekeeper::init() bool Housekeeper::init()
{
hk = new (std::nothrow) Housekeeper;
return hk != nullptr;
}
bool Housekeeper::start()
{ {
struct hkinit_result res; struct hkinit_result res;
sem_init(&res.sem, 0, 0); sem_init(&res.sem, 0, 0);
res.ok = false; res.ok = false;
hk = new (std::nothrow) Housekeeper;
if (hk && thread_start(&hk->m_thread, hkthread, &res, 0) != NULL) if (hk && thread_start(&hk->m_thread, hkthread, &res, 0) != NULL)
{ {
@ -148,7 +154,6 @@ bool Housekeeper::init()
sem_destroy(&res.sem); sem_destroy(&res.sem);
return res.ok; return res.ok;
} }
void Housekeeper::run() void Housekeeper::run()
{ {
while (is_running()) while (is_running())
@ -288,6 +293,11 @@ bool hkinit()
return Housekeeper::init(); return Housekeeper::init();
} }
bool hkstart()
{
return Housekeeper::start();
}
void hkfinish() void hkfinish()
{ {
if (hk) if (hk)