Add job for checking the integrity

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

@ -4,7 +4,10 @@ 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, GenerateMissingThumbsJob, VacuumJob}; use mediarepo_worker::job_dispatcher::JobDispatcher;
use mediarepo_worker::jobs::{
CalculateSizesJob, CheckIntegrityJob, GenerateMissingThumbsJob, Job, 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};
@ -37,20 +40,19 @@ impl JobsNamespace {
match run_request.job_type { match run_request.job_type {
JobType::MigrateContentDescriptors => job_dao.migrate_content_descriptors().await?, JobType::MigrateContentDescriptors => job_dao.migrate_content_descriptors().await?,
JobType::CalculateSizes => calculate_all_sizes(ctx).await?, JobType::CalculateSizes => calculate_all_sizes(ctx).await?,
JobType::CheckIntegrity => job_dao.check_integrity().await?, JobType::CheckIntegrity => {
dispatch_job(&dispatcher, CheckIntegrityJob::default(), run_request.sync).await?
}
JobType::Vacuum => { JobType::Vacuum => {
let mut handle = dispatcher.dispatch(VacuumJob::default()).await; dispatch_job(&dispatcher, VacuumJob::default(), run_request.sync).await?
if run_request.sync {
handle.try_result().await?;
}
} }
JobType::GenerateThumbnails => { JobType::GenerateThumbnails => {
let mut handle = dispatcher dispatch_job(
.dispatch(GenerateMissingThumbsJob::default()) &dispatcher,
.await; GenerateMissingThumbsJob::default(),
if run_request.sync { run_request.sync,
handle.try_result().await?; )
} .await?
} }
} }
@ -58,6 +60,18 @@ impl JobsNamespace {
} }
} }
async fn dispatch_job<J: 'static + Job>(
dispatcher: &JobDispatcher,
job: J,
sync: bool,
) -> RepoResult<()> {
let mut handle = dispatcher.dispatch(job).await;
if sync {
handle.try_result().await?;
}
Ok(())
}
async fn calculate_all_sizes(ctx: &Context) -> RepoResult<()> { async fn calculate_all_sizes(ctx: &Context) -> RepoResult<()> {
let (repo_path, settings) = { let (repo_path, settings) = {
let data = ctx.data.read().await; let data = ctx.data.read().await;

@ -0,0 +1,32 @@
use crate::jobs::Job;
use crate::status_utils::SimpleProgress;
use async_trait::async_trait;
use mediarepo_core::error::RepoResult;
use mediarepo_logic::dao::repo::Repo;
use mediarepo_logic::dao::DaoProvider;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Clone, Default)]
pub struct CheckIntegrityJob {
progress: Arc<RwLock<SimpleProgress>>,
}
#[async_trait]
impl Job for CheckIntegrityJob {
type JobStatus = SimpleProgress;
type Result = ();
fn status(&self) -> Arc<RwLock<Self::JobStatus>> {
self.progress.clone()
}
async fn run(&self, repo: Arc<Repo>) -> RepoResult<Self::Result> {
repo.job().check_integrity().await?;
{
let mut progress = self.progress.write().await;
progress.set_total(100);
}
Ok(())
}
}

@ -1,8 +1,10 @@
mod calculate_sizes; mod calculate_sizes;
mod check_integrity;
mod generate_missing_thumbnails; mod generate_missing_thumbnails;
mod vacuum; mod vacuum;
pub use calculate_sizes::*; pub use calculate_sizes::*;
pub use check_integrity::*;
pub use generate_missing_thumbnails::*; pub use generate_missing_thumbnails::*;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;

@ -1,5 +1,5 @@
use crate::job_dispatcher::JobDispatcher; use crate::job_dispatcher::JobDispatcher;
use crate::jobs::VacuumJob; use crate::jobs::{CheckIntegrityJob, VacuumJob};
use mediarepo_core::error::RepoError; use mediarepo_core::error::RepoError;
use mediarepo_core::tokio_graceful_shutdown::Toplevel; use mediarepo_core::tokio_graceful_shutdown::Toplevel;
use mediarepo_logic::dao::repo::Repo; use mediarepo_logic::dao::repo::Repo;
@ -21,6 +21,12 @@ pub async fn start(top_level: Toplevel, repo: Repo) -> (Toplevel, JobDispatcher)
dispatcher dispatcher
.dispatch_periodically(VacuumJob::default(), Duration::from_secs(60 * 30)) .dispatch_periodically(VacuumJob::default(), Duration::from_secs(60 * 30))
.await; .await;
dispatcher
.dispatch_periodically(
CheckIntegrityJob::default(),
Duration::from_secs(60 * 60 * 24),
)
.await;
Ok(()) Ok(())
}); });

Loading…
Cancel
Save