fix(userbot): job queue survives a consumer failure, a flood wait defers its job instead of holding the queue
This commit is contained in:
1 parent
3373c31a02
commit
42469eb9f1
4 files changed
+76
-24
No files matched your search
@@ -32,6 +32,7 @@ async def claim_next(pool: asyncpg.Pool, account_id: int) -> Job | None:
|
||||
"updated_at = now(), attempts = attempts + 1 "
|
||||
"WHERE id = ("
|
||||
" SELECT id FROM jobs WHERE account_id = $1 AND status = 'pending' "
|
||||
" AND (run_after IS NULL OR run_after <= now()) "
|
||||
" ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1"
|
||||
") RETURNING *",
|
||||
account_id,
|
||||
@@ -76,11 +77,21 @@ async def report_progress(
|
||||
)
|
||||
|
||||
|
||||
async def bump_flood_wait(pool: asyncpg.Pool, job_id: int) -> None:
|
||||
async def defer_flood_wait(pool: asyncpg.Pool, job_id: int, seconds: int) -> None:
|
||||
await pool.execute(
|
||||
"UPDATE jobs SET flood_waits = flood_waits + 1, updated_at = now() "
|
||||
"WHERE id = $1",
|
||||
"UPDATE jobs SET status = 'pending', flood_waits = flood_waits + 1, "
|
||||
"run_after = now() + make_interval(secs => $2), updated_at = now() "
|
||||
"WHERE id = $1 AND status = 'running'",
|
||||
job_id,
|
||||
seconds,
|
||||
)
|
||||
|
||||
|
||||
async def seconds_to_next_due(pool: asyncpg.Pool, account_id: int) -> float | None:
|
||||
return await pool.fetchval(
|
||||
"SELECT extract(epoch FROM min(run_after) - now())::float FROM jobs "
|
||||
"WHERE account_id = $1 AND status = 'pending' AND run_after > now()",
|
||||
account_id,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user