-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cron to cleanup dangling single tests and batch periodic test reports #1579
Open
aequitas
wants to merge
3
commits into
main
Choose a base branch
from
db_cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
checks/migrations/0016_webtestappsecpriv_timestamp_webtesttls_timestamp.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.16 on 2024-12-03 22:43 | ||
|
||
import datetime | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("checks", "0015_add_rpki_scoring"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="webtestappsecpriv", | ||
name="timestamp", | ||
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(1, 1, 1, 0, 0)), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name="webtesttls", | ||
name="timestamp", | ||
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(1, 1, 1, 0, 0)), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
|
||
# perform cleanup maintenance on database: | ||
# | ||
# - remove dangling subtests (probe results with no report) caused by periodic test or aborted single tests | ||
# - remove test reports for batch periodic tests | ||
|
||
set -e | ||
|
||
if [ ! "$CRON_DAILY_DATABASE_CLEANUP" = "True" ];then | ||
exit 0 | ||
fi | ||
|
||
docker ps --filter label=com.docker.compose.service=app --quiet | xargs -I% --no-run-if-empty docker exec % ./manage.py database_cleanup -v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.core.management.base import BaseCommand | ||
from checks.models import BatchRequest, DomainTestIpv6, DomainTestDnssec, WebTestTls, WebTestAppsecpriv, WebTestRpki | ||
import logging | ||
import datetime | ||
from django.conf import settings | ||
from django.utils import timezone | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
BATCH_PERIODIC_TESTS_PREFIX = "batch periodic tests" | ||
|
||
TEST_REPORT_PROBE_MODELS = [DomainTestIpv6, DomainTestDnssec, WebTestTls, WebTestAppsecpriv, WebTestRpki] | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Removes batch periodic test scan results and dangling probe results from database" | ||
|
||
def info(self, text): | ||
if self.v_level: | ||
self.stdout.write(f"{text}") | ||
|
||
def debug(self, text): | ||
if self.v_level > 1: | ||
self.stdout.write(f"{text}") | ||
|
||
def handle(self, *args, **options): | ||
logging.basicConfig(level=logging.INFO if options["verbosity"] > 0 else logging.ERROR) | ||
|
||
count, _ = BatchRequest.objects.filter(name__startswith=BATCH_PERIODIC_TESTS_PREFIX).delete() | ||
log.info("Deleted %s BatchRequest objects from batch periodic tests.", count) | ||
|
||
timestamp_recent_probes = timezone.make_aware(datetime.datetime.now()) - datetime.timedelta( | ||
seconds=int(settings.CACHE_TTL) | ||
) | ||
|
||
for model in TEST_REPORT_PROBE_MODELS: | ||
# >>> print(DomainTestIpv6.objects.filter(domaintestreport__isnull=True).values_list('id').query) | ||
# SELECT "checks_domaintestipv6"."id" FROM "checks_domaintestipv6" LEFT OUTER JOIN "checks_domaintestreport" | ||
# ON ("checks_domaintestipv6"."id" = "checks_domaintestreport"."ipv6_id") | ||
# WHERE "checks_domaintestreport"."id" IS NULL | ||
|
||
# find all test probe results that have no report associated, but not too recent because | ||
# those might be unfinished tests | ||
count, _ = model.objects.filter( | ||
domaintestreport__isnull=True, timestamp__lt=timestamp_recent_probes | ||
).delete() | ||
log.info("Deleted %s probes that don't have an associated report.", count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django.core.management import call_command | ||
from checks.models import DomainTestIpv6, DomainTestReport | ||
import datetime | ||
import pytest | ||
|
||
|
||
def test_cleanup_aborted_or_periodic_test_results(db): | ||
"""Make sure that test results with a report are deleted on cleanup, but not if they are recent.""" | ||
ipv6_no_report = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_no_report.save() | ||
ipv6_no_report.timestamp = datetime.datetime.now() - datetime.timedelta(seconds=200) | ||
ipv6_no_report.save() | ||
|
||
ipv6_report = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_report.save() | ||
ipv6_report.timestamp = datetime.datetime.now() - datetime.timedelta(seconds=200) | ||
ipv6_report.save() | ||
|
||
ipv6_no_report_recent = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_no_report_recent.save() | ||
|
||
ipv6_report_recent = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_report_recent.save() | ||
|
||
report = DomainTestReport(domain="example.com", ipv6=ipv6_report) | ||
report.save() | ||
|
||
# run cleanup | ||
call_command("database_cleanup") | ||
|
||
with pytest.raises(DomainTestIpv6.DoesNotExist): | ||
ipv6_no_report.refresh_from_db() | ||
|
||
ipv6_report.refresh_from_db() | ||
assert ipv6_report | ||
|
||
ipv6_no_report_recent.refresh_from_db() | ||
assert ipv6_no_report_recent | ||
|
||
ipv6_report_recent.refresh_from_db() | ||
assert ipv6_report_recent |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will auto_now_add also apply to existing rows at the time of migration? Or will it only do that when adding a new object through ORM? Otherwise, we will be expiring currently existing reports early.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should set the date to epoch(0) for existing records. I will test it out to make sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but that's bad, right? If the cleanup runs right after deployment, it'll consider every report old, and start cleaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cleanup does not touch single test reports, only probe/test results. It will delete results that don't have a report associated and are not recent enough to be currently running. The timestamp is added to these 2 models because they don't have it yet while other tests in the report do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻