Skip to content

Commit 3a4cd4e

Browse files
committed
cron: clear execution status after a timeout
Finally implement the `node_timeout` column.
1 parent a64aa23 commit 3a4cd4e

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎debian/changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cm4all-workshop (7.12) unstable; urgency=low
22

3+
* cron: clear execution status after a timeout
34
* spawn: eliminate epoll_wait() roundtrip
45
* spawn: asynchronous waits, parallelize execve()
56

‎src/cron/Queue.cxx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "StickyTable.hxx"
1010
#include "pg/Reflection.hxx"
1111
#include "lib/fmt/RuntimeError.hxx"
12+
#include "event/Loop.hxx"
1213
#include "util/StringAPI.hxx"
1314

1415
#include <chrono>
@@ -49,6 +50,15 @@ CronQueue::Prepare()
4950
UPDATE cronjobs
5051
SET node_name=NULL, node_timeout=NULL, next_run=NULL
5152
WHERE node_name=$1
53+
)SQL",
54+
1);
55+
56+
db.Prepare("expire_jobs", R"SQL(
57+
UPDATE cronjobs
58+
SET node_name=NULL, node_timeout=NULL, next_run=NULL
59+
WHERE
60+
node_name IS NOT NULL AND node_name <> $1 AND
61+
node_timeout IS NOT NULL AND now() > node_timeout
5262
)SQL",
5363
1);
5464

@@ -159,6 +169,14 @@ CronQueue::ReleaseStale()
159169
logger(3, "Released ", n, " stale cronjobs");
160170
}
161171

172+
inline void
173+
CronQueue::Expire()
174+
{
175+
const unsigned n = db.ExecutePrepared("expire_jobs", node_name.c_str()).GetAffectedRows();
176+
if (n > 0)
177+
logger(2, "released ", n, " expired cronjobs");
178+
}
179+
162180
void
163181
CronQueue::InsertStickyNonLocal(const char *sticky_id) noexcept
164182
try {
@@ -187,6 +205,13 @@ CronQueue::RunScheduler() noexcept
187205
logger(4, "scheduler");
188206

189207
try {
208+
/* check expired jobs from all other nodes except us */
209+
210+
if (const auto now = GetEventLoop().SteadyNow(); now >= next_expire_check) {
211+
next_expire_check = now + std::chrono::minutes{1};
212+
Expire();
213+
}
214+
190215
if (!CalculateNextRun(logger, db))
191216
ScheduleScheduler(false);
192217

‎src/cron/Queue.hxx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class CronQueue final : private Pg::AsyncConnectionHandler {
3535

3636
FineTimerEvent scheduler_timer, claim_timer;
3737

38+
std::chrono::steady_clock::time_point next_expire_check =
39+
std::chrono::steady_clock::time_point::min();
40+
3841
const bool sticky;
3942

4043
/**
@@ -152,6 +155,7 @@ private:
152155

153156
void Prepare();
154157
void ReleaseStale();
158+
void Expire();
155159

156160
void RunScheduler() noexcept;
157161
void ScheduleScheduler(bool immediately) noexcept;

0 commit comments

Comments
 (0)