Add simple synchronous job API

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent f74bac100b
commit f374488762

@ -1,6 +1,6 @@
[package]
name = "mediarepo-api"
version = "0.24.1"
version = "0.24.2"
edition = "2018"
license = "gpl-3"

@ -0,0 +1,40 @@
use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi;
use crate::types::jobs::{JobType, RunJobRequest};
use bromine::context::{Context, PoolGuard, PooledContext};
use std::time::Duration;
#[derive(Clone)]
pub struct JobApi {
ctx: PooledContext,
}
impl IPCApi for JobApi {
fn namespace() -> &'static str {
"jobs"
}
fn ctx(&self) -> PoolGuard<Context> {
self.ctx.acquire()
}
}
impl JobApi {
pub fn new(ctx: PooledContext) -> Self {
Self { ctx }
}
/// Runs a job of the given type and returns when it has finished
#[tracing::instrument(level = "debug", skip(self))]
pub async fn run_job(&self, job_type: JobType) -> ApiResult<()> {
self.emit_and_get(
"run_job",
RunJobRequest {
job_type,
sync: true,
},
Some(Duration::from_secs(300)),
)
.await
}
}

@ -1,11 +1,13 @@
pub mod error;
pub mod file;
pub mod job;
pub mod protocol;
pub mod repo;
pub mod tag;
use crate::client_api::error::{ApiError, ApiResult};
use crate::client_api::file::FileApi;
use crate::client_api::job::JobApi;
use crate::client_api::repo::RepoApi;
use crate::client_api::tag::TagApi;
use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse};
@ -45,6 +47,7 @@ pub struct ApiClient {
pub file: FileApi,
pub tag: TagApi,
pub repo: RepoApi,
pub job: JobApi,
}
impl Clone for ApiClient {
@ -54,6 +57,7 @@ impl Clone for ApiClient {
file: self.file.clone(),
tag: self.tag.clone(),
repo: self.repo.clone(),
job: self.job.clone(),
}
}
}
@ -65,6 +69,7 @@ impl ApiClient {
file: FileApi::new(ctx.clone()),
tag: TagApi::new(ctx.clone()),
repo: RepoApi::new(ctx.clone()),
job: JobApi::new(ctx.clone()),
ctx,
}
}

@ -28,19 +28,22 @@ impl RepoApi {
/// Returns metadata about the repository
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_repo_metadata(&self) -> ApiResult<RepositoryMetadata> {
self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(3))).await
self.emit_and_get("repository_metadata", (), Some(Duration::from_secs(3)))
.await
}
/// Returns the size of a given type
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_size(&self, size_type: SizeType) -> ApiResult<SizeMetadata> {
self.emit_and_get("size_metadata", size_type, Some(Duration::from_secs(30))).await
self.emit_and_get("size_metadata", size_type, Some(Duration::from_secs(30)))
.await
}
/// Returns the state of the frontend that is stored in the repo
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_frontend_state(&self) -> ApiResult<FrontendState> {
self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await
self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5)))
.await
}
/// Sets the state of the frontend

@ -0,0 +1,11 @@
use crate::tauri_plugin::commands::ApiAccess;
use crate::tauri_plugin::error::PluginResult;
use crate::types::jobs::JobType;
#[tauri::command]
pub async fn run_job(api_state: ApiAccess<'_>, job_type: JobType) -> PluginResult<()> {
let api = api_state.api().await?;
api.job.run_job(job_type).await?;
Ok(())
}

@ -2,6 +2,7 @@ use tauri::State;
pub use daemon::*;
pub use file::*;
pub use job::*;
pub use repo::*;
pub use tag::*;
@ -9,6 +10,7 @@ use crate::tauri_plugin::state::{ApiState, AppState, BufferState};
pub mod daemon;
pub mod file;
pub mod job;
pub mod repo;
pub mod tag;

@ -66,7 +66,8 @@ impl<R: Runtime> MediarepoPlugin<R> {
get_files,
get_repo_metadata,
get_size,
get_file_metadata
get_file_metadata,
run_job
]),
}
}

@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RunJobRequest {
pub job_type: JobType,
pub sync: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum JobType {
MigrateContentDescriptors,
CalculateSizes,
CheckIntegrity,
}

@ -1,5 +1,6 @@
pub mod files;
pub mod identifier;
pub mod jobs;
pub mod misc;
pub mod repo;
pub mod tags;

Loading…
Cancel
Save