Skip to content

Commit ef7ead9

Browse files
committed
feat: add delete history command
1 parent f07346f commit ef7ead9

6 files changed

Lines changed: 54 additions & 0 deletions

File tree

.env.example.prod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ ALLOWED_ORIGINS=csv,includehttp
3737
# into swagger
3838
# INJECT_SWAGGER_X_API_KEY_HEADER_AUTH=true
3939

40+
# History older than this many rounds will get dropped. There are 960 round
41+
# each day so 30 * 960 = 28800 means there will be 30 days of history kept.
42+
# You can comment this out to never delete history. In this case it is suggested
43+
# to also comment out the delte_history service from docker compose.
44+
HISTORY_KEEP_ROUNDS = 28800
45+
4046
# project related
4147

4248
# CONFIG_MODULE: one of: flare, songbird, coston2, coston

docker-compose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,14 @@ services:
5151
django:
5252
condition: service_healthy
5353

54+
delete-history:
55+
<<: *django
56+
command: python manage.py delete_history
57+
depends_on:
58+
db:
59+
condition: service_healthy
60+
django:
61+
condition: service_healthy
62+
5463
volumes:
5564
pg_data:

fsp/management/__init__.py

Whitespace-only changes.

fsp/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import time
2+
3+
from django.conf import settings
4+
from django.core.management.base import BaseCommand
5+
from django.db import transaction
6+
7+
from fdc.models import AttestationResult
8+
from fsp.epoch import VotingEpoch
9+
from fsp.models import ProtocolMessageRelayed
10+
from ftso.models import FeedResult, RandomResult
11+
12+
13+
class Command(BaseCommand):
14+
def handle(self, *args, **options):
15+
history_rounds: int | None = settings.HISTORY_KEEP_ROUNDS
16+
if history_rounds is None:
17+
return
18+
19+
while True:
20+
time.sleep(600)
21+
22+
voting_epoch_now = VotingEpoch.now().n
23+
voting_round = VotingEpoch(voting_epoch_now - history_rounds)
24+
25+
with transaction.atomic():
26+
AttestationResult.objects.filter(
27+
voting_round_id__lt=voting_round
28+
).delete()
29+
FeedResult.objects.filter(voting_round_id__lt=voting_round).delete()
30+
RandomResult.objects.filter(voting_round_id__lt=voting_round).delete()
31+
ProtocolMessageRelayed.objects.filter(
32+
voting_round_id__lt=voting_round
33+
).delete()

project/settings/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,10 @@
170170

171171
# END OF DEPENDENCY SETTINGS
172172

173+
_HISTORY_KEEP_ROUNDS = os.environ.get("HISTORY_KEEP_ROUNDS", "")
174+
try:
175+
HISTORY_KEEP_ROUNDS = int(_HISTORY_KEEP_ROUNDS)
176+
except ValueError:
177+
HISTORY_KEEP_ROUNDS = None
178+
173179
CONFIG_MODULE = os.environ.get("CONFIG_MODULE", None)

0 commit comments

Comments
 (0)