Always refreshing channel in background task.

This commit is contained in:
Roland 2025-12-26 13:43:51 -07:00
parent e82cd8a19f
commit 3733eb4efd
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 23 additions and 29 deletions

View file

@ -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()

View file

@ -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'

View file

@ -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:

View file

@ -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

View file

@ -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']: