diff --git a/backend/startup.py b/backend/startup.py index 4ad314ed5..0ae957b0b 100644 --- a/backend/startup.py +++ b/backend/startup.py @@ -4,7 +4,7 @@ import asyncio import sentry_sdk from opentelemetry import trace -from rq.job import Job +from rq.exceptions import DuplicateJobError from config import ( ENABLE_SCHEDULED_CONVERT_IMAGES_TO_WEBP, @@ -35,6 +35,7 @@ from tasks.manual.recompute_save_content_hashes import ( recompute_save_content_hashes_task, ) from tasks.scheduled.convert_images_to_webp import convert_images_to_webp_task +from tasks.tasks import run_task_by_name from utils import get_version from utils.cache import conditionally_set_cache from utils.context import initialize_context @@ -44,6 +45,11 @@ tracer = trace.get_tracer(__name__) RECOMPUTE_SAVE_HASHES_JOB_ID = "recompute_save_content_hashes_bootstrap" CONVERT_IMAGES_TO_WEBP_JOB_ID = "convert_images_to_webp_bootstrap" +# The names these backfills are registered under, which is what the payload +# carries rather than the task itself. +RECOMPUTE_SAVE_HASHES_TASK = "recompute_save_content_hashes" +CONVERT_IMAGES_TO_WEBP_TASK = "convert_images_to_webp" + def _enqueue_recompute_save_hashes_if_needed() -> None: """Backfill content_hash for saves uploaded before the path-resolution @@ -66,16 +72,13 @@ def _enqueue_recompute_save_hashes_if_needed() -> None: return try: - if Job.exists(RECOMPUTE_SAVE_HASHES_JOB_ID, low_prio_queue.connection): - log.info( - "recompute_save_content_hashes already queued or running from a " - "previous restart; skipping enqueue" - ) - return - + # A fixed id with unique=True settles it in one round trip, so two + # instances starting together cannot both get past the check. low_prio_queue.enqueue( - recompute_save_content_hashes_task.run, + run_task_by_name, + kwargs={"name": RECOMPUTE_SAVE_HASHES_TASK}, job_id=RECOMPUTE_SAVE_HASHES_JOB_ID, + unique=True, job_timeout=TASK_TIMEOUT, meta={ "task_name": recompute_save_content_hashes_task.title, @@ -86,6 +89,11 @@ def _enqueue_recompute_save_hashes_if_needed() -> None: f"Enqueued recompute_save_content_hashes ({missing} saves with NULL content_hash); " "running on low-priority worker" ) + except DuplicateJobError: + log.info( + "recompute_save_content_hashes already queued or running from a " + "previous restart; skipping enqueue" + ) except Exception: log.exception( "Failed to enqueue recompute_save_content_hashes; admins can run it manually" @@ -101,16 +109,11 @@ def _enqueue_convert_images_to_webp() -> None: enabling. Without a backfill, existing covers have no .webp sibling and every request 404s until the cron eventually runs.""" try: - if Job.exists(CONVERT_IMAGES_TO_WEBP_JOB_ID, low_prio_queue.connection): - log.info( - "convert_images_to_webp already queued or running from a " - "previous restart; skipping enqueue" - ) - return - low_prio_queue.enqueue( - convert_images_to_webp_task.run, + run_task_by_name, + kwargs={"name": CONVERT_IMAGES_TO_WEBP_TASK}, job_id=CONVERT_IMAGES_TO_WEBP_JOB_ID, + unique=True, job_timeout=TASK_TIMEOUT, meta={ "task_name": convert_images_to_webp_task.title, @@ -118,6 +121,11 @@ def _enqueue_convert_images_to_webp() -> None: }, ) log.info("Enqueued convert_images_to_webp backfill on low-priority worker") + except DuplicateJobError: + log.info( + "convert_images_to_webp already queued or running from a previous " + "restart; skipping enqueue" + ) except Exception: log.exception( "Failed to enqueue convert_images_to_webp; admins can run it manually" diff --git a/backend/tests/test_startup.py b/backend/tests/test_startup.py index 273cd1655..34d5c908b 100644 --- a/backend/tests/test_startup.py +++ b/backend/tests/test_startup.py @@ -1,8 +1,11 @@ """Tests for startup-time auto-enqueue of the recompute task.""" import startup +from rq.exceptions import DuplicateJobError from rq.job import JOB_ID_PATTERN +from tasks.registry import get_task + def test_enqueue_recompute_skips_when_no_missing_hashes(mocker): """Saves all have content_hash -> no enqueue.""" @@ -21,15 +24,17 @@ def test_enqueue_recompute_fires_when_missing_hashes_present(mocker): mocker.patch.object( startup.db_save_handler, "count_saves_missing_content_hash", return_value=42 ) - mocker.patch.object(startup.Job, "exists", return_value=False) enqueue = mocker.patch.object(startup.low_prio_queue, "enqueue") startup._enqueue_recompute_save_hashes_if_needed() enqueue.assert_called_once() args, kwargs = enqueue.call_args - # First positional arg is the bound task.run method - assert args[0].__self__ is startup.recompute_save_content_hashes_task + # Enqueued by name, so the payload survives the code moving underneath it + assert args[0] is startup.run_task_by_name + assert kwargs["kwargs"] == {"name": startup.RECOMPUTE_SAVE_HASHES_TASK} + # RQ settles the duplicate check and the enqueue in one round trip + assert kwargs["unique"] is True # Sanity-check the meta payload routes correctly in the task list UI assert kwargs["meta"]["task_name"] == ( startup.recompute_save_content_hashes_task.title @@ -50,17 +55,26 @@ def test_recompute_job_id_is_valid_rq_id(): assert JOB_ID_PATTERN.fullmatch(startup.RECOMPUTE_SAVE_HASHES_JOB_ID) -def test_enqueue_recompute_skips_when_already_queued(mocker): - """An in-flight job from a previous restart -> skip enqueue, don't double up.""" +def test_enqueue_recompute_tolerates_an_in_flight_job(mocker): + """An in-flight job from a previous restart -> RQ refuses, startup carries on.""" mocker.patch.object( startup.db_save_handler, "count_saves_missing_content_hash", return_value=10 ) - mocker.patch.object(startup.Job, "exists", return_value=True) - enqueue = mocker.patch.object(startup.low_prio_queue, "enqueue") + mocker.patch.object( + startup.low_prio_queue, "enqueue", side_effect=DuplicateJobError("exists") + ) startup._enqueue_recompute_save_hashes_if_needed() - enqueue.assert_not_called() + +def test_both_backfills_name_a_registered_task(mocker): + """The name in the payload is all the runner gets, so it has to resolve.""" + assert get_task(startup.RECOMPUTE_SAVE_HASHES_TASK) is ( + startup.recompute_save_content_hashes_task + ) + assert get_task(startup.CONVERT_IMAGES_TO_WEBP_TASK) is ( + startup.convert_images_to_webp_task + ) def test_enqueue_recompute_swallows_count_error(mocker): @@ -82,7 +96,6 @@ def test_enqueue_recompute_swallows_enqueue_error(mocker): mocker.patch.object( startup.db_save_handler, "count_saves_missing_content_hash", return_value=5 ) - mocker.patch.object(startup.Job, "exists", return_value=False) mocker.patch.object( startup.low_prio_queue, "enqueue", side_effect=RuntimeError("redis gone") ) @@ -92,14 +105,15 @@ def test_enqueue_recompute_swallows_enqueue_error(mocker): def test_enqueue_convert_webp_fires_when_not_queued(mocker): """No in-flight bootstrap job -> enqueue the backfill exactly once.""" - mocker.patch.object(startup.Job, "exists", return_value=False) enqueue = mocker.patch.object(startup.low_prio_queue, "enqueue") startup._enqueue_convert_images_to_webp() enqueue.assert_called_once() args, kwargs = enqueue.call_args - assert args[0].__self__ is startup.convert_images_to_webp_task + assert args[0] is startup.run_task_by_name + assert kwargs["kwargs"] == {"name": startup.CONVERT_IMAGES_TO_WEBP_TASK} + assert kwargs["unique"] is True assert kwargs["meta"]["task_name"] == startup.convert_images_to_webp_task.title assert kwargs["meta"]["task_type"] == ( startup.convert_images_to_webp_task.task_type.value @@ -114,19 +128,17 @@ def test_convert_webp_job_id_is_valid_rq_id(): assert JOB_ID_PATTERN.fullmatch(startup.CONVERT_IMAGES_TO_WEBP_JOB_ID) -def test_enqueue_convert_webp_skips_when_already_queued(mocker): - """An in-flight job from a previous restart -> skip enqueue, don't double up.""" - mocker.patch.object(startup.Job, "exists", return_value=True) - enqueue = mocker.patch.object(startup.low_prio_queue, "enqueue") +def test_enqueue_convert_webp_tolerates_an_in_flight_job(mocker): + """An in-flight job from a previous restart -> RQ refuses, startup carries on.""" + mocker.patch.object( + startup.low_prio_queue, "enqueue", side_effect=DuplicateJobError("exists") + ) startup._enqueue_convert_images_to_webp() - enqueue.assert_not_called() - def test_enqueue_convert_webp_swallows_enqueue_error(mocker): """A failed enqueue must not crash startup.""" - mocker.patch.object(startup.Job, "exists", return_value=False) mocker.patch.object( startup.low_prio_queue, "enqueue", side_effect=RuntimeError("redis gone") )