From ae0ee6bbfb4dcd1f112876d0e7e5b3bcabdfb002 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 17 Mar 2026 17:33:14 +0000 Subject: Use std::future over std::thread for background jobs Easier checking if a job has completed [successfully] and reseting state for the next time. --- src/ingestor.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/ingestor.cpp') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index ba89991..692ddd2 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -223,17 +223,21 @@ namespace WebStat { Ingestor::runJobsAsNeeded() { auto runJobAsNeeded = [this, now = Job::LastRunTime::clock::now()](Job & job, auto freq) { - if (!job.currentRun && expired(job.lastRun, freq, now)) { - job.currentRun.emplace([this, now, freq, &job]() { + if (job.currentRun) { + if (job.currentRun->valid()) { try { - (this->*job.impl)(); + job.currentRun->get(); job.lastRun = now; } catch (const std::exception &) { // Error, retry in half the frequency job.lastRun = now - (freq / 2); } - }); + job.currentRun.reset(); + } + } + else if (expired(job.lastRun, freq, now)) { + job.currentRun.emplace(std::async(job.impl, this)); } }; runJobAsNeeded(ingestParkedLines, settings.freqIngestParkedLines); -- cgit v1.3