add item checker designed to run in cron

main
eric 2023-04-20 17:28:52 -04:00
parent 1db503c225
commit c50e9cd8be
2 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,48 @@
import datetime
import logging
from django.core.management.base import BaseCommand
logger = logging.getLogger(__name__)
from doab_check.check import check_link
from doab_check.models import Item
def mo_hr_now():
now = datetime.datetime.now()
return (now.day - 1) * 24 + now.hour
class Command(BaseCommand):
help = "check items in based on primary key"
def add_arguments(self, parser):
parser.add_argument('--max', nargs='?', type=int, default=120, help="max checks")
def handle(self, **options):
max_pk = Item.objects.latest('pk').pk
# round up to nearest 1000
top = max_pk - (max_pk % 1000) + 1000
start_time = datetime.datetime.now()
max = options['max']
n_checked = l_checked = 0
prime = 349
this_hr = mo_hr_now()
logger.info(f'this_hr: {this_hr} top: {top}')
for i in range(0, max):
j = (i * prime + this_hr) % top
try:
item = Item.objects.get(pk=j)
for link in item.related.filter(status=1):
check_link(link.link)
l_checked += 1
n_checked += 1
if n_checked >= max:
break
except Item.DoesNotExist:
continue
self.stdout.write(f'checked {n_checked} links')
end_time = datetime.datetime.now()
logger.info(f'checked {l_checked} links in {end_time - start_time}')
self.stdout.write(f'checked {l_checked} links for {n_checked} items in {end_time - start_time}')

View File

@ -14,6 +14,7 @@ class Command(BaseCommand):
parser.add_argument('--max', nargs='?', type=int, default=1000, help="max checks")
def handle(self, **options):
start_time = datetime.datetime.now()
max = options['max']
n_checked = 0
for link in Link.objects.all().order_by('?'):
@ -22,4 +23,7 @@ class Command(BaseCommand):
if n_checked >= max:
break
self.stdout.write(f'checked {n_checked} links')
logger.info(f'checked {n_checked} links')
end_time = datetime.datetime.now()
logger.info(f'checked {n_checked} links in {end_time - start_time}')
self.stdout.write(f'checked {n_checked} links in {end_time - start_time}')