Skip to content

Commit

Permalink
[core] Avoid constructing Timers before main() as global static objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Jul 13, 2024
1 parent f0ca663 commit 117d14f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ static void toggle_admin_port() {
class PeriodicJob {
public:
void start() { run(); }
void stop() { m_timer.cancel(); }
void stop() { if (m_timer) m_timer->cancel(); }
protected:
virtual void run() = 0;
void next() { m_timer.schedule(5, [this]() { run(); }); }
void next() {
if (!m_timer) m_timer = std::unique_ptr<Timer>(new Timer);
m_timer->schedule(5, [this]() { run(); });
}

private:
Timer m_timer;
std::unique_ptr<Timer> m_timer;
};

//
Expand Down Expand Up @@ -305,7 +309,7 @@ class SignalHandler {
private:
asio::signal_set m_signals;
bool m_admin_closed = false;
Timer m_timer;
std::unique_ptr<Timer> m_timer;

void wait() {
m_signals.async_wait(
Expand Down Expand Up @@ -343,7 +347,8 @@ class SignalHandler {
stop_all();
} else {
Log::info("[shutdown] Waiting for workers to drain...");
m_timer.schedule(1, [this]() { wait_workers(); });
if (!m_timer) m_timer = std::unique_ptr<Timer>(new Timer);
m_timer->schedule(1, [this]() { wait_workers(); });
}
}

Expand Down

0 comments on commit 117d14f

Please sign in to comment.