Skip to content

Commit b184743

Browse files
committed
Give plugins ability to defer service readiness
1 parent 3b16601 commit b184743

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

flashmq_plugin.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ See LICENSE for license details.
1717
#include "subscriptionstore.h"
1818
#include "globals.h"
1919
#include "utils.h"
20+
#include "mainapp.h"
2021

2122
void flashmq_logf(int level, const char *str, ...)
2223
{
@@ -311,3 +312,37 @@ void flashmq_get_client_pointer(const std::weak_ptr<Session> &session, std::weak
311312
if (!sessionLocked) return;
312313
clientOut = sessionLocked->makeSharedClient();
313314
}
315+
316+
void flashmq_defer_thread_ready()
317+
{
318+
auto d = ThreadGlobals::getThreadData();
319+
320+
if (!d)
321+
{
322+
Logger::getInstance()->log(LOG_ERROR) << "Calling flashmq_defer_thread_ready from custom thread.";
323+
return;
324+
}
325+
326+
d->deferThreadReady = true;
327+
}
328+
329+
void flashmq_signal_thread_ready()
330+
{
331+
const auto d = ThreadGlobals::getThreadData();
332+
333+
if (!d)
334+
{
335+
Logger::getInstance()->log(LOG_ERROR) << "Calling flashmq_signal_thread_ready from custom thread.";
336+
return;
337+
}
338+
339+
if (!d->deferThreadReady)
340+
return;
341+
342+
std::shared_ptr<MainApp> lockedMainApp = d->mMainApp.lock();
343+
344+
if (!lockedMainApp)
345+
return;
346+
347+
lockedMainApp->queueThreadInitDecrement();
348+
}

flashmq_public.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ uint32_t API flashmq_add_task(std::function<void()> f, uint32_t delay_in_ms);
298298
*/
299299
void API flashmq_remove_task(uint32_t id);
300300

301+
/**
302+
* @brief Use this if you don't want your thread to be signalled ready after flashmq_plugin_init().
303+
*
304+
* Normally, when threads return from flashmq_plugin_init(), the worker thread signals to the main thread it's done. Once
305+
* all threads have done this, the listeners are created and systemd is notified. You may want to defer this, until all
306+
* your auth data is loaded, for instance.
307+
*
308+
* One simple solution to queue back this event is to use flashmq_add_task() to poll on a timer and check if your data is
309+
* loaded, and run flashmq_signal_thread_ready() when it's ready.
310+
*
311+
* [Introduced in FlashMQ 1.26.0]
312+
*/
313+
void API flashmq_defer_thread_ready();
314+
315+
/**
316+
* @brief Counterpart to flashmq_defer_thread_ready().
317+
*
318+
* There is no timeout action. If you don't do this, FlashMQ will never be ready. When starting from systemd unit of
319+
* type 'notify', it will be restarted after a set time.
320+
*
321+
* [Introduced in FlashMQ 1.26.0]
322+
*/
323+
void API flashmq_signal_thread_ready();
324+
301325
}
302326

303327
#endif // FLASHMQ_PUBLIC_H

threaddata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class ThreadData
120120
public:
121121
Settings settingsLocalCopy; // Is updated on reload, within the thread loop.
122122
Authentication authentication;
123+
bool deferThreadReady = false;
123124
bool running = true;
124125
bool finished = false;
125126
bool allWillsQueued = false;

threadloop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void do_thread_work(std::shared_ptr<ThreadData> threadData)
4040

4141
auto lockedMainApp = threadData->mMainApp.lock();
4242

43-
if (lockedMainApp)
43+
if (!threadData->deferThreadReady && lockedMainApp)
4444
{
4545
lockedMainApp->queueThreadInitDecrement();
4646
}

0 commit comments

Comments
 (0)