mirror of
https://github.com/esphome/esphome.git
synced 2026-09-26 14:30:23 +00:00
[host] Add graceful shutdown on SIGINT/SIGTERM (#15387)
This commit is contained in:
@@ -5,11 +5,17 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "preferences.h"
|
||||
|
||||
#include <csignal>
|
||||
#include <sched.h>
|
||||
#include <time.h>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace {
|
||||
volatile sig_atomic_t s_signal_received = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
void signal_handler(int signal) { s_signal_received = signal; }
|
||||
} // namespace
|
||||
|
||||
namespace esphome {
|
||||
|
||||
void HOT yield() { ::sched_yield(); }
|
||||
@@ -72,11 +78,17 @@ uint32_t arch_get_cpu_freq_hz() { return 1000000000U; }
|
||||
void setup();
|
||||
void loop();
|
||||
int main() {
|
||||
// Install signal handlers for graceful shutdown (flushes preferences to disk)
|
||||
std::signal(SIGINT, signal_handler);
|
||||
std::signal(SIGTERM, signal_handler);
|
||||
|
||||
esphome::host::setup_preferences();
|
||||
setup();
|
||||
while (true) {
|
||||
while (s_signal_received == 0) {
|
||||
loop();
|
||||
}
|
||||
esphome::App.run_safe_shutdown_hooks();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // USE_HOST
|
||||
|
||||
Reference in New Issue
Block a user