fix(watcher): resume the bot backlog from a saved cursor instead of restarting it
This commit is contained in:
1 parent
59d3f214e8
commit
468f72ed11
4 files changed
+156
-32
No files matched your search
@@ -35,6 +35,28 @@ SET enabled = false, status = 'removed', status_detail = NULL, updated_at = now(
|
||||
WHERE bot_id = $1 AND enabled
|
||||
"""
|
||||
|
||||
_BACKLOG_CURSOR = """
|
||||
SELECT backfill_pts, backfill_qts, backfill_date
|
||||
FROM watched_bots
|
||||
WHERE bot_id = $1 AND backfill_pts IS NOT NULL
|
||||
"""
|
||||
|
||||
_SAVE_BACKLOG_CURSOR = """
|
||||
UPDATE watched_bots SET
|
||||
backfill_pts = $2, backfill_qts = $3, backfill_date = $4, updated_at = now()
|
||||
WHERE bot_id = $1
|
||||
"""
|
||||
|
||||
_MARK_BACKFILLED = """
|
||||
UPDATE watched_bots SET
|
||||
backfilled_at = now(),
|
||||
backfill_pts = NULL,
|
||||
backfill_qts = NULL,
|
||||
backfill_date = NULL,
|
||||
updated_at = now()
|
||||
WHERE bot_id = $1
|
||||
"""
|
||||
|
||||
_SET_STATUS = """
|
||||
UPDATE watched_bots SET
|
||||
status = $2,
|
||||
@@ -116,9 +138,20 @@ async def needs_backfill(pool: asyncpg.Pool, bot_id: int) -> bool:
|
||||
)
|
||||
|
||||
|
||||
async def backlog_cursor(
|
||||
pool: asyncpg.Pool, bot_id: int
|
||||
) -> tuple[int, int, int] | None:
|
||||
row = await pool.fetchrow(_BACKLOG_CURSOR, bot_id)
|
||||
if row is None:
|
||||
return None
|
||||
return row["backfill_pts"], row["backfill_qts"], row["backfill_date"]
|
||||
|
||||
|
||||
async def save_backlog_cursor(
|
||||
pool: asyncpg.Pool, bot_id: int, pts: int, qts: int, date: int
|
||||
) -> None:
|
||||
await pool.execute(_SAVE_BACKLOG_CURSOR, bot_id, pts, qts, date)
|
||||
|
||||
|
||||
async def mark_backfilled(pool: asyncpg.Pool, bot_id: int) -> None:
|
||||
await pool.execute(
|
||||
"UPDATE watched_bots SET backfilled_at = now(), updated_at = now() "
|
||||
"WHERE bot_id = $1",
|
||||
bot_id,
|
||||
)
|
||||
await pool.execute(_MARK_BACKFILLED, bot_id)
|
||||
Reference in new issue
Block a user