Add job implementation to generate missing thumbnails

Signed-off-by: trivernis <trivernis@protonmail.com>
feature/jobs
trivernis 3 years ago
parent 22f28a79d1
commit a87c341867
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -4,7 +4,7 @@ use mediarepo_core::error::RepoResult;
use mediarepo_core::mediarepo_api::types::jobs::{JobType, RunJobRequest}; use mediarepo_core::mediarepo_api::types::jobs::{JobType, RunJobRequest};
use mediarepo_core::type_keys::{RepoPathKey, SettingsKey, SizeMetadataKey}; use mediarepo_core::type_keys::{RepoPathKey, SettingsKey, SizeMetadataKey};
use mediarepo_logic::dao::DaoProvider; use mediarepo_logic::dao::DaoProvider;
use mediarepo_worker::jobs::{CalculateSizesJob, VacuumJob}; use mediarepo_worker::jobs::{CalculateSizesJob, GenerateMissingThumbsJob, VacuumJob};
use crate::utils::{get_job_dispatcher_from_context, get_repo_from_context}; use crate::utils::{get_job_dispatcher_from_context, get_repo_from_context};
@ -41,7 +41,11 @@ impl JobsNamespace {
JobType::Vacuum => { JobType::Vacuum => {
dispatcher.dispatch(VacuumJob::default()).await; dispatcher.dispatch(VacuumJob::default()).await;
} }
JobType::GenerateThumbnails => job_dao.generate_missing_thumbnails().await?, JobType::GenerateThumbnails => {
dispatcher
.dispatch(GenerateMissingThumbsJob::default())
.await;
}
} }
Ok(Response::empty()) Ok(Response::empty())

@ -0,0 +1,46 @@
use crate::jobs::Job;
use crate::status_utils::SimpleProgress;
use async_trait::async_trait;
use mediarepo_core::error::RepoResult;
use mediarepo_core::thumbnailer::ThumbnailSize;
use mediarepo_logic::dao::repo::Repo;
use mediarepo_logic::dao::DaoProvider;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Clone, Default)]
pub struct GenerateMissingThumbsJob {
state: Arc<RwLock<SimpleProgress>>,
}
#[async_trait]
impl Job for GenerateMissingThumbsJob {
type JobState = SimpleProgress;
fn state(&self) -> Arc<RwLock<Self::JobState>> {
self.state.clone()
}
async fn run(&self, repo: Arc<Repo>) -> RepoResult<()> {
let file_dao = repo.file();
let all_files = file_dao.all().await?;
{
let mut progress = self.state.write().await;
progress.set_total(all_files.len() as u64);
}
for file in all_files {
if file_dao.thumbnails(file.encoded_cd()).await?.is_empty() {
let _ = file_dao
.create_thumbnails(&file, vec![ThumbnailSize::Medium])
.await;
}
{
let mut progress = self.state.write().await;
progress.tick();
}
}
Ok(())
}
}

@ -1,7 +1,9 @@
mod calculate_sizes; mod calculate_sizes;
mod generate_missing_thumbnails;
mod vacuum; mod vacuum;
pub use calculate_sizes::*; pub use calculate_sizes::*;
pub use generate_missing_thumbnails::*;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
pub use vacuum::*; pub use vacuum::*;

@ -17,6 +17,11 @@ impl SimpleProgress {
Self { total, current: 0 } Self { total, current: 0 }
} }
/// Sets the total count
pub fn set_total(&mut self, total: u64) {
self.total = total;
}
/// Increments the current progress by 1 /// Increments the current progress by 1
pub fn tick(&mut self) { pub fn tick(&mut self) {
self.current += 1; self.current += 1;

Loading…
Cancel
Save