diff --git a/modules/zim/downloader.py b/modules/zim/downloader.py index ce896495..ff515a04 100644 --- a/modules/zim/downloader.py +++ b/modules/zim/downloader.py @@ -106,13 +106,9 @@ class KiwixZimDownloader(Downloader): logger.warning(f'Not validating {output_path} because zimcheck is not installed') # Add the new Zim to the FileGroups, model it. + # The model_zim() function triggers a Kiwix restart via switch when a new Zim is created. await upsert_file(output_path) - # Restart Kiwix serve, so it finds the new zim file. - return_code = await lib.restart_kiwix() - if return_code != 0: - logger.error(f'Failed to restart kiwix') - logger.info(f'Successfully downloaded Zim {download.url} to {output_path}') # Location is just the generic Kiwix viewer. diff --git a/modules/zim/lib.py b/modules/zim/lib.py index 5ce169ca..dc848fb4 100644 --- a/modules/zim/lib.py +++ b/modules/zim/lib.py @@ -18,6 +18,7 @@ from wrolpi import flags from wrolpi.cmd import run_command from wrolpi.common import register_modeler, logger, extract_html_text, extract_headlines, get_media_directory, walk, \ register_refresh_cleanup, background_task, get_wrolpi_config, unique_by_predicate, aiohttp_post +from wrolpi.switches import register_switch_handler from wrolpi.dates import Seconds from wrolpi.events import Events from wrolpi.db import get_db_session, get_db_curs @@ -41,11 +42,18 @@ __all__ = [ 'get_zim', 'get_zims', 'model_zim', + 'restart_kiwix_handler', 'zim_download_url_to_name', 'zim_modeler', ] +@register_switch_handler('restart_kiwix') +async def restart_kiwix_handler(): + """Switch handler that restarts the Kiwix service after zim files are added.""" + await restart_kiwix() + + def model_zim(file_group: FileGroup, session: Session) -> Zim: """Create a Zim record for the provided FileGroup.""" # Check if a Zim already exists for this FileGroup @@ -54,6 +62,8 @@ def model_zim(file_group: FileGroup, session: Session) -> Zim: # Create new Zim if it doesn't exist zim = Zim(file_group=file_group, path=file_group.primary_path) session.add(zim) + # Trigger Kiwix restart so the new zim file is available for browsing. + restart_kiwix_handler.activate_switch() file_group.title = file_group.primary_path.name file_group.a_text = split_file_name_words(file_group.primary_path.name) file_group.indexed = True @@ -560,7 +570,8 @@ async def remove_outdated_zim_files(path: pathlib.Path = None) -> int: # Refresh synchronously to clean up DB before returning. await file_worker.refresh_sync(list(outdated)) - await restart_kiwix() + # Trigger Kiwix restart so the deleted zim files are no longer served. + restart_kiwix_handler.activate_switch() return deleted_count diff --git a/modules/zim/test/test_restart_kiwix.py b/modules/zim/test/test_restart_kiwix.py new file mode 100644 index 00000000..f9b68065 --- /dev/null +++ b/modules/zim/test/test_restart_kiwix.py @@ -0,0 +1,104 @@ +"""Tests for the restart_kiwix switch handler triggered by model_zim().""" +import pytest + +from modules.zim import lib +from modules.zim.models import Zim +from wrolpi.api_utils import api_app +from wrolpi.files.models import FileGroup + + +@pytest.mark.asyncio +async def test_model_zim_activates_restart_switch(async_client, test_session, zim_path_factory): + """model_zim() should activate the restart_kiwix switch when creating a new Zim.""" + # Create a zim file and its FileGroup + zim_path = zim_path_factory('test_restart.zim') + file_group = FileGroup.from_paths(test_session, zim_path) + test_session.commit() + + # Verify no switches are pending + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' not in switches + + # Call model_zim which should activate the switch + lib.model_zim(file_group, test_session) + test_session.commit() + + # Verify the switch was activated + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' in switches + + +@pytest.mark.asyncio +async def test_existing_zim_does_not_trigger_restart(async_client, test_session, zim_factory, await_switches): + """model_zim() should NOT activate restart switch for existing Zims.""" + # Create a zim using the factory (this calls model_zim internally via Zim.from_paths) + zim = zim_factory('existing.zim') + test_session.commit() + + # Clear any pending switches + await await_switches() + + # Verify no switches are pending + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' not in switches + + # Call model_zim again on the same FileGroup - should NOT trigger restart + lib.model_zim(zim.file_group, test_session) + test_session.commit() + + # Verify the switch was NOT activated (Zim already exists) + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' not in switches + + +@pytest.mark.asyncio +async def test_multiple_zims_trigger_single_restart(async_client, test_session, zim_path_factory): + """Multiple new Zims should result in only one restart (debouncing via switch).""" + # Create multiple zim files + paths = [zim_path_factory(f'test_debounce_{i}.zim') for i in range(3)] + + # Create FileGroups and model each one + for path in paths: + file_group = FileGroup.from_paths(test_session, path) + test_session.commit() + lib.model_zim(file_group, test_session) + test_session.commit() + + # Verify switch was activated (only once, due to debouncing) + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' in switches + # The switch dict contains only one entry for restart_kiwix, proving debouncing works + assert list(switches.keys()).count('restart_kiwix') == 1 + + +@pytest.mark.asyncio +async def test_remove_outdated_zim_files_triggers_restart(async_client, test_session, test_directory, zim_path_factory, + await_switches): + """remove_outdated_zim_files() should trigger the restart_kiwix switch.""" + # Create outdated and current zim files + zim_dir = test_directory / 'zims' + zim_dir.mkdir(exist_ok=True) + + # Create an "outdated" zim and a "current" zim (based on date in filename) + outdated_path = zim_dir / 'wikipedia_en_all_maxi_2020-01.zim' + current_path = zim_dir / 'wikipedia_en_all_maxi_2020-02.zim' + + # Copy test zim bytes to both paths + test_zim_bytes = zim_path_factory().read_bytes() + outdated_path.write_bytes(test_zim_bytes) + current_path.write_bytes(test_zim_bytes) + + # Clear any pending switches first + await await_switches() + + # Call remove_outdated_zim_files + deleted_count = await lib.remove_outdated_zim_files(zim_dir) + + # Verify the outdated file was deleted + assert deleted_count == 1 + assert not outdated_path.exists() + assert current_path.exists() + + # Verify the switch was activated + switches = dict(api_app.shared_ctx.switches) if api_app.shared_ctx.switches else {} + assert 'restart_kiwix' in switches