Skip to content

Commit

Permalink
changed: added rule for automatically creating missing previews
Browse files Browse the repository at this point in the history
  • Loading branch information
sraesch committed Aug 27, 2023
1 parent d0c03cc commit 316ac0a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
79 changes: 64 additions & 15 deletions movies-db-service/movies-db/src/service/preview_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ use tokio::{
sync::{mpsc, RwLock},
};

use crate::{ffmpeg::FFMpeg, MovieDataType, MovieId, MovieStorage, MoviesIndex, ScreenshotInfo};
use crate::{
ffmpeg::FFMpeg, MovieDataType, MovieId, MovieSearchQuery, MovieStorage, MoviesIndex,
ScreenshotInfo,
};

/// The request to generate a preview.
#[derive(Clone, Debug)]
pub struct ScreenshotRequest {
/// The id of the movie to generate the preview for.
pub movie_id: MovieId,
pub ext: String,
}
Expand All @@ -20,6 +24,7 @@ pub struct PreviewGenerator<I: MoviesIndex, S: MovieStorage> {
index: Arc<RwLock<I>>,
storage: Arc<RwLock<S>>,
recv_preview: mpsc::UnboundedReceiver<ScreenshotRequest>,
send_preview: mpsc::UnboundedSender<ScreenshotRequest>,
}

impl<I: MoviesIndex, S: MovieStorage> PreviewGenerator<I, S> {
Expand All @@ -29,26 +34,29 @@ impl<I: MoviesIndex, S: MovieStorage> PreviewGenerator<I, S> {
/// * `ffmpeg` - The ffmpeg instance.
/// * `index` - The movie index.
/// * `storage` - The movie storage.
pub fn new(
ffmpeg: FFMpeg,
index: Arc<RwLock<I>>,
storage: Arc<RwLock<S>>,
) -> (Self, mpsc::UnboundedSender<ScreenshotRequest>) {
pub fn new(ffmpeg: FFMpeg, index: Arc<RwLock<I>>, storage: Arc<RwLock<S>>) -> Self {
let (send_preview, recv_preview) = mpsc::unbounded_channel();

(
Self {
ffmpeg,
index,
storage,
recv_preview,
},
send_preview,
)
Self {
ffmpeg,
index,
storage,
recv_preview,
send_preview: send_preview.clone(),
}
}

/// Returns the sender for preview requests.
pub fn get_preview_request_sender(&self) -> mpsc::UnboundedSender<ScreenshotRequest> {
self.send_preview.clone()
}

/// Runs the preview generator loop.
pub async fn run(&mut self) {
self.trigger_all_missing_previews().await;

info!("Starting preview generator loop...");

while let Some(r) = self.recv_preview.recv().await {
debug!("Generating preview for request '{:?}'", r);

Expand Down Expand Up @@ -155,4 +163,45 @@ impl<I: MoviesIndex, S: MovieStorage> PreviewGenerator<I, S> {

info!("Preview generator loop stopped");
}

async fn trigger_all_missing_previews(&self) {
info!("Triggering all missing previews...");
let index = self.index.read().await;

let preview_request_sender = self.get_preview_request_sender();

let query: MovieSearchQuery = Default::default();
let movie_ids = match index.search_movies(query).await {
Ok(movie_ids) => movie_ids,
Err(err) => {
error!("Failed to search movies");
error!("Error: {}", err);
return;
}
};

for movie_id in movie_ids.iter() {
let movie = match index.get_movie(movie_id).await {
Ok(movie) => movie,
Err(err) => {
error!("Failed to get movie '{}'", movie_id);
error!("Error: {}", err);
continue;
}
};

if let Some(movie_file_info) = movie.movie_file_info {
if movie.screenshot_file_info.is_none() {
info!("Movie '{}' is missing a preview", movie_id);
if let Err(err) = preview_request_sender.send(ScreenshotRequest {
movie_id: movie_id.clone(),
ext: movie_file_info.extension,
}) {
error!("Failed to send preview request for movie '{}'", movie_id);
error!("Error: {}", err);
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions movies-db-service/movies-db/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ where

// create preview generator
let ffmpeg = FFMpeg::new(&self.options.ffmpeg).await?;
let (preview_generator, preview_requests) =
PreviewGenerator::new(ffmpeg, index.clone(), storage.clone());
let preview_generator = PreviewGenerator::new(ffmpeg, index.clone(), storage.clone());
let preview_requests = preview_generator.get_preview_request_sender();

// spawn preview generator
tokio::spawn(async move {
Expand Down

0 comments on commit 316ac0a

Please sign in to comment.