launch a thread to check querysets in admin

main
eric 2023-11-25 16:55:41 -05:00
parent dfd2d8fdf2
commit 40313c0834
2 changed files with 14 additions and 5 deletions

View File

@ -5,7 +5,7 @@ from django.utils.safestring import mark_safe
# Register your models here.
from . import models
from .check import check_link
from .check import start_check_links
@admin.register(models.Check)
@ -42,8 +42,8 @@ class LinkAdmin(admin.ModelAdmin):
@admin.action(description="Recheck the links")
def recheck(self, request, queryset):
for link in queryset:
check_link(link)
msg = start_check_links(queryset)
self.message_user(request, msg)
@admin.display(description="URL")
def link_display(self, obj):

View File

@ -3,6 +3,7 @@
import logging
import re
import threading
import time
from urllib.parse import urlparse
@ -128,7 +129,7 @@ def type_for_url(url, response=None):
return code, f'other; {ct}'
def check_link(link):
''' given a Link object, check it's URL, put the result in a Check object '''
''' given a Link object, check its URL, put the result in a Check object '''
check = Check(link=link)
code, ct = type_for_url(link.url)
check.return_code = code
@ -138,4 +139,12 @@ def check_link(link):
check.link.save()
return check
def check_links(queryset):
for link in queryset:
check = check_link(link)
def start_check_links(queryset):
checker = threading.Thread(target=check_links, args=(queryset,), daemon=True)
checker.start()
return 'checker has started checking'