-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added silk_request_garbage_collect command for out-of-band garbage co…
…llection. (#541) * Added silk_request_garbage_collect command for out-of-band garbage collection. To avoid deadlock issues as mentioned e.g. in #265 #294 #371 Based on #265 (comment) * Okay, to please @codecov :) Co-authored-by: Nikolaus Schlemm <[email protected]> Co-authored-by: Albert Wang <[email protected]>
- Loading branch information
1 parent
1eaf3d4
commit c59acba
Showing
5 changed files
with
66 additions
and
1 deletion.
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
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,22 @@ | ||
from django.core import management | ||
from django.test import TestCase | ||
|
||
from silk import models | ||
from silk.config import SilkyConfig | ||
|
||
from .factories import RequestMinFactory | ||
|
||
|
||
class TestViewClearDB(TestCase): | ||
def test_garbage_collect_command(self): | ||
SilkyConfig().SILKY_MAX_RECORDED_REQUESTS = 2 | ||
RequestMinFactory.create_batch(3) | ||
self.assertEqual(models.Request.objects.count(), 3) | ||
management.call_command("silk_request_garbage_collect") | ||
self.assertEqual(models.Request.objects.count(), 2) | ||
management.call_command("silk_request_garbage_collect", max_requests=1) | ||
self.assertEqual(models.Request.objects.count(), 1) | ||
management.call_command( | ||
"silk_request_garbage_collect", max_requests=0, verbosity=2 | ||
) | ||
self.assertEqual(models.Request.objects.count(), 0) |
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,29 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
import silk.models | ||
from silk.config import SilkyConfig | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Triggers silk's request garbage collect." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"-m", | ||
"--max-requests", | ||
default=SilkyConfig().SILKY_MAX_RECORDED_REQUESTS, | ||
type=int, | ||
help="Maximum number of requests to keep after garbage collection.", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
if "max_requests" in options: | ||
max_requests = options["max_requests"] | ||
SilkyConfig().SILKY_MAX_RECORDED_REQUESTS = max_requests | ||
if options["verbosity"] >= 2: | ||
max_requests = SilkyConfig().SILKY_MAX_RECORDED_REQUESTS | ||
request_count = silk.models.Request.objects.count() | ||
self.stdout.write( | ||
f"Keeping up to {max_requests} of {request_count} requests." | ||
) | ||
silk.models.Request.garbage_collect(force=True) |