diff --git a/modules/videos/channel/api.py b/modules/videos/channel/api.py index 9f205d46..1cea35c9 100644 --- a/modules/videos/channel/api.py +++ b/modules/videos/channel/api.py @@ -57,8 +57,7 @@ def channel_post(request: Request, body: schema.ChannelPostRequest): channel = lib.create_channel(session, data=body, return_dict=False) # Refresh the videos asynchronously - if not PYTEST: - asyncio.ensure_future(channel.refresh_files()) + channel.refresh_files(channel.id) Events.send_created(f'Created Channel: {channel.name}') @@ -105,7 +104,7 @@ def channel_refresh(request: Request, channel_id: int): if not channel: raise UnknownChannel() - asyncio.ensure_future(channel.refresh_files()) + channel.refresh_files(channel.id) directory = get_relative_to_media_directory(channel.directory) Events.send_directory_refresh(f'Refreshing: {directory}') return response.empty() diff --git a/modules/videos/channel/test/test_lib.py b/modules/videos/channel/test/test_lib.py index 2cabe1a8..d8e15dfb 100644 --- a/modules/videos/channel/test/test_lib.py +++ b/modules/videos/channel/test/test_lib.py @@ -150,7 +150,8 @@ async def test_tag_channel_existing(async_client, test_session, test_directory, @pytest.mark.asyncio async def test_tag_channel_without_directory_saves_config( - test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches + test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches, + async_client ): """Tagging channel without providing directory should still save config.""" channel_directory = test_directory / 'videos' / 'Channel Name' @@ -175,7 +176,7 @@ async def test_tag_channel_without_directory_saves_config( @pytest.mark.asyncio async def test_untag_channel_without_directory_saves_config( - test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches + test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches, ): """Untagging channel without providing directory should still save config.""" channel_directory = test_directory / 'videos' / 'Channel Name' diff --git a/modules/videos/conftest.py b/modules/videos/conftest.py index b45699ee..27eec02c 100644 --- a/modules/videos/conftest.py +++ b/modules/videos/conftest.py @@ -207,7 +207,7 @@ def video_download_manager(test_download_manager) -> DownloadManager: @pytest.fixture -def test_channels_config(test_directory): +def test_channels_config(test_directory, async_client): (test_directory / 'config').mkdir(exist_ok=True) config_path = test_directory / 'config/channels.yaml' with set_test_channels_config() as config: diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 9a1b26c3..f560d2b2 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -281,8 +281,7 @@ class ChannelDownloader(Downloader, ABC): channel = get_channel(session, channel_id=channel_id, return_dict=False) if not channel.videos or not channel.refreshed: logger.warning(f'Refreshing videos in {channel.directory} because {channel} has no video records!') - await channel.refresh_files(send_events=False) - session.commit() # Commit the refresh. + channel.refresh_files(channel.id, send_events=False) session.refresh(channel) @staticmethod diff --git a/modules/videos/models.py b/modules/videos/models.py index 1f3283f7..cc7b2b51 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -889,30 +889,25 @@ class Channel(ModelHelper, Base): self.tag_id = self.tag.id if self.tag else None return self.tag - async def refresh_files(self, send_events: bool = True): + @classmethod + def refresh_files(cls, id_: int, send_events: bool = True): """Refresh all files within this Channel's directory. Mark this channel as refreshed.""" - logger.debug(f'{self}.refresh_files') - # Get this Channel's ID for later. Refresh may take a long time. - self_id = self.id + # Get this Channel's info for later. Refresh may take a long time. + with get_db_session() as session: + directory = cls.find_by_id(session, id_).directory - # Refresh all files within this channel's directory first. - await refresh_files([self.directory], send_events=send_events) + # Perform info_json in background task. Channel will be marked as refreshed after this completes. + async def _(): + # Refresh all files within this channel's directory first. + from modules.videos.common import update_view_counts_and_censored + await refresh_files([directory], send_events=send_events) + # Update view count second. + await update_view_counts_and_censored(id_) + with get_db_session(commit=True) as session_: + channel_ = cls.find_by_id(session_, id_) + channel_.refreshed = True - # Apply any info_json (update view counts) second. - from modules.videos.common import update_view_counts_and_censored - if PYTEST: - await update_view_counts_and_censored(self_id) - self.refreshed = True - else: - - # Perform info_json in background task. Channel will be marked as refreshed after this completes. - async def _(): - await update_view_counts_and_censored(self_id) - with get_db_session(commit=True) as session: - channel: Channel = session.query(Channel).filter(Channel.id == self_id).one() - channel.refreshed = True - - background_task(_()) + background_task(_()) @staticmethod def get_by_id(session: Session, id_: int) -> Optional['Channel']: