An open source, non-profit web search engine
Fixes for running server
Daoud Clarke2bd6bd7d7a8e
diff --git a/mwmbl/crawl.py b/mwmbl/crawl.pyindex 9b994ec..72acdf0 100644--- a/mwmbl/crawl.py+++ b/mwmbl/crawl.py@@ -11,7 +11,7 @@ import requestsfrom django.conf import settingsfrom redis import Redis-from mwmbl.crawler.env_vars import CRAWLER_WORKERS, CRAWL_DELAY_SECONDS, MWMBL_API_KEY, REDIS_URL+from mwmbl.crawler.env_vars import CRAWLER_WORKERS, CRAWL_DELAY_SECONDS, MWMBL_API_KEYfrom mwmbl.rankeval.evaluation.remote_index import RemoteIndexfrom mwmbl.redis_url_queue import RedisURLQueuefrom mwmbl.tinysearchengine.indexer import TinyIndex, Document@@ -41,7 +41,7 @@ BATCH_QUEUE_KEY = "batch-queue"redis = Redis.from_url(- REDIS_URL,+ settings.REDIS_URL,decode_responses=True,health_check_interval=30,)@@ -157,7 +157,6 @@ def process_batch():"user_id_hash": user_id,"timestamp": js_timestamp,"items": results,- "crawler_version": CRAWLER_VERSION})record_urls_in_database([batch], url_queue)@@ -226,7 +225,7 @@ def run_indexing():if new_high_score:result_items = [Result(url=doc.url, title=doc.title, extract=doc.extract,score=doc.score, term=doc.term, state=doc.state) for doc in new_items]- results = Results(api_key=MWMBL_API_KEY, results=result_items)+ results = Results(api_key=MWMBL_API_KEY, results=result_items, crawler_version=CRAWLER_VERSION)logger.info(f"Posting {len(result_items)} results")response = requests.post("https://mwmbl.org/api/v1/crawler/results", json=results.dict()diff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.pyindex a50330e..959e49b 100644--- a/mwmbl/crawler/app.py+++ b/mwmbl/crawler/app.py@@ -10,12 +10,11 @@ from uuid import uuid4import boto3import requestsfrom django.conf import settings-from ninja import NinjaAPI, Schema, HTTPException+from ninja import NinjaAPI, Schema+from ninja.errors import HttpErrorfrom redis import Redisfrom mwmbl.crawler.batch import Batch, NewBatchRequest, HashedBatch, Results, PostResultsResponse, Error-from mwmbl.crawler.env_vars import REDIS_URL-from mwmbl.crawl import CRAWLER_VERSIONfrom mwmbl.crawler.stats import MwmblStats, StatsManagerfrom mwmbl.database import Databasefrom mwmbl.indexer.batch_cache import BatchCache@@ -37,7 +36,7 @@ from mwmbl.settings import (DATE_REGEX)from mwmbl.tinysearchengine.indexer import Document-stats_manager = StatsManager(Redis.from_url(REDIS_URL, decode_responses=True))+stats_manager = StatsManager(Redis.from_url(os.environ.get("REDIS_URL", "redis://127.0.0.1:6379"), decode_responses=True))def get_bucket(name):@@ -87,12 +86,7 @@ def create_router(batch_cache: BatchCache, queued_batches: RedisURLQueue, versio# Using an approach from https://stackoverflow.com/a/30476450now = datetime.now(timezone.utc)epoch_time = (now - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()- hashed_batch = HashedBatch(- user_id_hash=user_id_hash,- timestamp=epoch_time,- items=batch.items,- crawler_version=CRAWLER_VERSION- )+ hashed_batch = HashedBatch(user_id_hash=user_id_hash, timestamp=epoch_time, items=batch.items)stats_manager.record_batch(hashed_batch)@@ -209,7 +203,7 @@ def _get_user_id_hash(batch: Union[Batch, NewBatchRequest]):def check_public_user_id(public_user_id):if len(public_user_id) != PUBLIC_USER_ID_LENGTH:- raise HTTPException(400, f"Incorrect public user ID length, should be {PUBLIC_USER_ID_LENGTH}")+ raise HttpError(400, f"Incorrect public user ID length, should be {PUBLIC_USER_ID_LENGTH}")def get_batch_url(batch_id, date_str, public_user_id):@@ -242,7 +236,7 @@ def get_batches_for_prefix(prefix):def check_date_str(date_str):if not DATE_REGEX.match(date_str):- raise HTTPException(400, f"Incorrect date format, should be YYYY-MM-DD")+ raise HttpError(400, f"Incorrect date format, should be YYYY-MM-DD")def get_subfolders(prefix):diff --git a/mwmbl/crawler/batch.py b/mwmbl/crawler/batch.pyindex 303338e..5dc8907 100644--- a/mwmbl/crawler/batch.py+++ b/mwmbl/crawler/batch.py@@ -58,7 +58,6 @@ class HashedBatch(Schema):user_id_hash: strtimestamp: floatitems: list[Item]- crawler_version: Optional[str] = Noneclass Result(Schema):@@ -73,6 +72,7 @@ class Result(Schema):class Results(Schema):api_key: strresults: list[Result]+ crawler_version: Optional[str] = Noneclass PostResultsResponse(Schema):diff --git a/mwmbl/crawler/env_vars.py b/mwmbl/crawler/env_vars.pyindex f0c0b4f..338464f 100644--- a/mwmbl/crawler/env_vars.py+++ b/mwmbl/crawler/env_vars.py@@ -12,9 +12,6 @@ CRAWL_THREADS = int(os.environ.get("CRAWL_THREADS", "20"))# Rate limiting configurationCRAWL_DELAY_SECONDS = float(os.environ.get("CRAWL_DELAY_SECONDS", "0.0"))-# Redis configuration-REDIS_URL = os.environ.get("REDIS_URL", "redis://127.0.0.1:6379")-# API configurationMWMBL_API_KEY = os.environ.get("MWMBL_API_KEY", "")assert MWMBL_API_KEY.strip(), "An environment variable MWMBL_API_KEY must be set to run the crawler"diff --git a/mwmbl/settings_common.py b/mwmbl/settings_common.pyindex 77fb9f5..c33b4ff 100644--- a/mwmbl/settings_common.py+++ b/mwmbl/settings_common.py@@ -220,3 +220,6 @@ def strip_query_string(event):# Django ninja-jwt settings - custom auth ruleUSER_AUTHENTICATION_RULE = require_email_confirmation++# Redis configuration+REDIS_URL = os.environ.get("REDIS_URL", "redis://127.0.0.1:6379")