Automatically restarting kiwix container when new zim is downloaded using new Controller.

This commit is contained in:
Roland 2025-12-19 17:00:53 -07:00
parent d703bdc268
commit 7183cf5e85
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 23 additions and 25 deletions

View file

@ -17,7 +17,9 @@ from modules.zim.models import Zim, Zims, TagZimEntry, ZimSubscription
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
register_refresh_cleanup, background_task, get_wrolpi_config, unique_by_predicate, aiohttp_post
from wrolpi.dates import Seconds
from wrolpi.events import Events
from wrolpi.db import get_db_session, get_db_curs
from wrolpi.downloader import DownloadFrequency
from wrolpi.files.lib import refresh_files, split_file_name_words
@ -26,6 +28,8 @@ from wrolpi.vars import PYTEST, DOCKERIZED
logger = logger.getChild(__name__)
CONTROLLER_SERVICE = 'http://controller:8087'
__all__ = [
'flag_outdated_zim_files',
'get_all_entries_tags',
@ -573,9 +577,23 @@ async def restart_kiwix():
logger.warning('Unable to restart Kiwix serve because we are testing')
return
if DOCKERIZED:
logger.warning('Setting Kiwix restart because this is in a docker container')
flags.kiwix_restart.set()
return
logger.info('Restarting Kiwix via controller API')
try:
async with aiohttp_post(f'{CONTROLLER_SERVICE}/api/services/zim/restart',
timeout=Seconds.minute) as response:
if response.status == 200:
logger.info('Successfully restarted Kiwix container')
return 0
else:
logger.error(f'Failed to restart Kiwix: {response.status}')
Events.send_user_notify(
'Failed to restart Kiwix automatically. Please restart manually: docker compose restart zim')
return 1
except Exception as e:
logger.error(f'Failed to restart Kiwix: {e}')
Events.send_user_notify(
'Failed to restart Kiwix automatically. Please restart manually: docker compose restart zim')
return 1
logger.info('Restarting Kiwix serve')