An open source, non-profit web search engine
Update the old index info
Daoud Clarke9719895737ab
diff --git a/mwmbl/background.py b/mwmbl/background.pyindex 86aa05a..da1e7d2 100644--- a/mwmbl/background.py+++ b/mwmbl/background.py@@ -3,17 +3,23 @@ Script that updates data in a background process."""import loggingimport sys+from datetime import datetimefrom logging import getLogger, basicConfigfrom pathlib import Pathfrom time import sleep-from mwmbl import settings+from django.conf import settings+from mwmbl.indexer import index_batches, historicalfrom mwmbl.indexer.batch_cache import BatchCachefrom mwmbl.indexer.paths import BATCH_DIR_NAME, INDEX_NAMEfrom mwmbl.models import OldIndexfrom mwmbl.tinysearchengine.copy_index import copy_pages++NUM_PAGES_TO_COPY = 10++basicConfig(stream=sys.stdout, level=logging.INFO)logger = getLogger(__name__)@@ -39,8 +45,24 @@ def run(data_path: str):def copy_all_indexes(new_index_path):old_indexes = OldIndex.objects.all()+ logger.info(f"Found {len(old_indexes)} old indexes")++ # Check if all indexes are copied+for old_index_info in old_indexes:- copy_pages(old_index_info.index_path, new_index_path, old_index_info.start_page, 10)+ if old_index_info.last_page_copied >= old_index_info.num_pages - 1:+ logger.info(f"All pages copied for index {old_index_info.index_path}")+ continue++ logger.info(f"Copying pages from {old_index_info.index_path} to {new_index_path} starting at page {old_index_info.last_page_copied}")+ end_page = copy_pages(old_index_info.index_path, new_index_path, old_index_info.start_page, NUM_PAGES_TO_COPY)++ # Update the start page+ old_index_info.last_page_copied = end_page+ old_index_info.last_copied_time = datetime.utcnow()+ old_index_info.save()++ logger.info(f"Copied pages from {old_index_info.index_path} to {new_index_path} up to page {end_page}")def copy_indexes_continuously():diff --git a/mwmbl/main.py b/mwmbl/main.pyindex 9867ea6..c287786 100644--- a/mwmbl/main.py+++ b/mwmbl/main.py@@ -1,12 +1,12 @@-import loggingimport os-import sys+from pathlib import Pathimport djangoimport uvicornfrom django.core.management import call_commandfrom redis import Redis+from mwmbl.indexer.paths import INDEX_NAMEfrom mwmbl.indexer.update_urls import update_urls_continuouslyfrom mwmbl.redis_url_queue import RedisURLQueue@@ -29,6 +29,8 @@ def run():update_urls_continuously(settings.DATA_PATH, url_queue)elif mwmbl_app == "update_batches":background.run(settings.DATA_PATH)+ elif mwmbl_app == "copy_indexes":+ background.copy_indexes_continuously()elif mwmbl_app == "server":uvicorn.run("mwmbl.asgi:application", host="0.0.0.0", port=5000)else:diff --git a/mwmbl/redis_url_queue.py b/mwmbl/redis_url_queue.pyindex 7fb0686..fcd47da 100644--- a/mwmbl/redis_url_queue.py+++ b/mwmbl/redis_url_queue.py@@ -75,6 +75,15 @@ class RedisURLQueue:for domain in domains:domain_urls_scores = self.redis.zpopmax(DOMAIN_URLS_KEY.format(domain=domain))+ # Update the domain score if we removed a URL+ new_domain_scores = self.redis.zrangebyscore(+ DOMAIN_URLS_KEY.format(domain=domain), "-inf", "+inf", start=0, num=1, withscores=True)+ if new_domain_scores:+ new_domain_score = new_domain_scores[0][1]+ self.redis.zadd(DOMAIN_SCORE_KEY, {domain: new_domain_score}, gt=True)+ else:+ self.redis.zrem(DOMAIN_SCORE_KEY, domain)+for url, score in domain_urls_scores:urls.append(url)diff --git a/mwmbl/tinysearchengine/copy_index.py b/mwmbl/tinysearchengine/copy_index.pyindex 7afcbbe..f940197 100644--- a/mwmbl/tinysearchengine/copy_index.py+++ b/mwmbl/tinysearchengine/copy_index.py@@ -30,3 +30,5 @@ def copy_pages(old_index_path: str, new_index_path: str, start_page: int, num_palogger.info(f"Copying {len(page_documents)} pages to {new_index_path}")index_pages(new_index_path, page_documents)++ return page_index