blob: 86283b7d25f0196a4b3af3e5e6bd4f8b1f791967 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
"""Delete old received Webmentions in terminal unsuccessful states."""
from datetime import UTC, datetime, timedelta
import sqlalchemy as sa
from flask import current_app
from ..db_helpers import rows_affected
from ..extensions import db
from ..models import ReceivedStatus, ReceivedWebmention
def purge_old_received_webmentions() -> int:
"""Delete old failed/deleted received rows, retaining verified and in-flight ones.
The status/time predicate is checked by the database in one DELETE, so a
row recently requeued for verification is not purged based on a stale read.
Outgoing sources and Webmentions are intentionally unaffected.
"""
cutoff = datetime.now(UTC) - timedelta(
days=current_app.config["RECEIVED_RETENTION_DAYS"]
)
result = db.session.execute(
sa.delete(ReceivedWebmention).where(
ReceivedWebmention.status.in_(
(ReceivedStatus.FAILED, ReceivedStatus.DELETED)
),
ReceivedWebmention.updated_at < cutoff,
)
)
db.session.commit()
count = rows_affected(result)
if count:
current_app.logger.info("Purged %d old failed/deleted Webmentions", count)
return count
|