Commit b933d579 authored by jan.koester's avatar jan.koester
Browse files

test

parent 0379aed0
Loading
Loading
Loading
Loading
+19 −4
Changes for src/service.cpp: 19 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#include <algorithm>
#include <cstring>
#include <csignal>
#include <ctime>

#include <unistd.h>
#include <sys/wait.h>
@@ -16,6 +17,12 @@

namespace syslite {

    static long long monotonicMs() {
        timespec ts{};
        clock_gettime(CLOCK_MONOTONIC, &ts);
        return static_cast<long long>(ts.tv_sec) * 1000 + ts.tv_nsec / 1000000;
    }

    const char *stateName(ServiceState s) {
        switch (s) {
        case ServiceState::Stopped:  return "stopped";
@@ -354,6 +361,7 @@ namespace syslite {
        }
        entry.restartCount = 0;
        entry.gaveUp = false;
        entry.restartAtMs = 0;

        // Start required dependencies first
        for (auto &req : entry.unit.requires) {
@@ -503,15 +511,22 @@ namespace syslite {
            }

            if (shouldRestart && entry.pid <= 0) {
                // Honor RestartSec by scheduling the restart rather than
                // sleeping: this runs in PID 1's main loop, which must keep
                // reaping children and serving sysbus meanwhile
                long long now = monotonicMs();
                if (entry.restartAtMs == 0) {
                    entry.restartAtMs = now + 1000LL * (entry.unit.restartSec > 0 ? entry.unit.restartSec : 0);
                }
                if (now < entry.restartAtMs) continue;
                entry.restartAtMs = 0;
                ++entry.restartCount;

                // Honor RestartSec — sleep before restarting
                if (entry.unit.restartSec > 0)
                    sleep(entry.unit.restartSec);

                std::cout << "[syslite] Restarting " << name
                          << " (attempt " << entry.restartCount << ")" << std::endl;
                execService(entry);
            } else {
                entry.restartAtMs = 0;
            }
        }
    }
+1 −0
Changes for src/service.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ namespace syslite {
        int           restartCount = 0;
        bool          manualStop = false; // stopped on request: Restart= doesn't apply
        bool          gaveUp = false;     // restart limit reached (reported once)
        long long     restartAtMs = 0;    // when a pending restart is due (monotonic ms), 0: none
    };