diff --git a/main.py b/main.py index 7189918e..4f023279 100755 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ create_empty_config_files, TRACE_LEVEL from wrolpi.contexts import attach_shared_contexts, reset_shared_contexts, initialize_configs_contexts from wrolpi.dates import Seconds -from wrolpi.downloader import import_downloads_config, download_manager +from wrolpi.downloader import import_downloads_config, download_manager, get_download_manager_config from wrolpi.errors import WROLModeEnabled from wrolpi.vars import PROJECT_DIR, DOCKERIZED, INTERNET_SERVER from wrolpi.version import get_version_string @@ -257,9 +257,6 @@ async def start_single_tasks(app: Sanic): # Set all downloads to new. download_manager.retry_downloads() - if get_wrolpi_config().download_on_startup and download_manager.can_download: - await download_manager.enable() - # Hotspot/throttle are not supported in Docker containers. if not DOCKERIZED: if get_wrolpi_config().hotspot_on_startup: @@ -280,6 +277,15 @@ async def start_single_tasks(app: Sanic): else: logger.info('CPU throttle on startup is disabled') + if get_wrolpi_config().download_on_startup and not wrol_mode_enabled(): + # Only start downloading when prerequisites have been met. + try: + async with flags.have_internet.wait_for(timeout=30): + if get_download_manager_config().successful_import: + await download_manager.enable() + except TimeoutError as e: + logger.error('Failed to enable download', exc_info=e) + logger.debug(f'start_single_tasks done') diff --git a/modules/videos/lib.py b/modules/videos/lib.py index 17c9aa3c..89fd846f 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -19,7 +19,7 @@ escape_file_name, get_media_directory, background_task, Base, get_wrolpi_config from wrolpi.dates import Seconds, from_timestamp from wrolpi.db import get_db_curs, get_db_session -from wrolpi.downloader import Download +from wrolpi.downloader import Download, download_manager from wrolpi.errors import UnknownDirectory from wrolpi.events import Events from wrolpi.files.lib import split_path_stem_and_suffix @@ -297,8 +297,9 @@ def import_config(self, file: pathlib.Path = None, send_events=False): if not channel.source_id and channel.url and flags.have_internet.is_set(): # If we can download from a channel, we must have its source_id. - logger.info(f'Fetching channel source id for {channel}') - background_task(fetch_channel_source_id(channel.id)) + if download_manager.can_download: + logger.info(f'Fetching channel source id for {channel}') + background_task(fetch_channel_source_id(channel.id)) channel_import_logger.debug(f'Updated {repr(channel.name)}' f' url={channel.url}'