style: adjust code formatting and import order
This commit is contained in:
parent
94b0fe1039
commit
36869be0ed
@ -22,9 +22,7 @@ impl AppConfig {
|
||||
fn load_config() -> Config {
|
||||
let config = match fs::read_to_string("./config.toml") {
|
||||
Ok(toml_string) => match toml::from_str::<ConfigTable>(&toml_string) {
|
||||
Ok(config_table) => {
|
||||
config_table.config
|
||||
}
|
||||
Ok(config_table) => config_table.config,
|
||||
Err(_) => panic!("{}", SystemEntry::InvalidConfig)
|
||||
}
|
||||
Err(_) => panic!("{}", SystemEntry::ConfigNotFound)
|
||||
@ -41,7 +39,7 @@ impl AppConfig {
|
||||
pub fn fetch_blocking() -> Config {
|
||||
// Initialization has been ensured
|
||||
let lock = SYNC_CONFIG.get().unwrap();
|
||||
// There is no lock acquired multiple times, so this is safe
|
||||
// In extreme cases, a serious error occurs in the system
|
||||
lock.read().unwrap().clone()
|
||||
}
|
||||
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
use std::ops::Deref;
|
||||
use crate::interface::database_ops::DatabaseOpsTrait;
|
||||
use crate::platform::constants::*;
|
||||
use crate::platform::database_ops::DatabaseOps;
|
||||
use crate::utils::log_entry::database::DatabaseEntry;
|
||||
use crate::utils::log_entry::system::SystemEntry;
|
||||
use sqlx::SqlitePool;
|
||||
use std::ops::Deref;
|
||||
use std::sync::OnceLock;
|
||||
use tracing::{error, info, trace};
|
||||
|
||||
@ -18,18 +18,16 @@ pub struct DatabaseManager {
|
||||
impl DatabaseManager {
|
||||
pub async fn initialization() {
|
||||
info!("{}", SystemEntry::Initializing);
|
||||
if let Err(err) = DatabaseOps::lock_database().await {
|
||||
panic!("{}", err);
|
||||
}
|
||||
DatabaseOps::lock_database().await.unwrap();
|
||||
if !DatabaseOps::exist_database().await {
|
||||
if let Err(err) = DatabaseOps::create_database().await {
|
||||
panic!("{}", err);
|
||||
}
|
||||
DatabaseOps::create_database().await.unwrap();
|
||||
}
|
||||
let instance = match SqlitePool::connect(DATABASE_URL).await {
|
||||
Ok(pool) => {
|
||||
info!("{}", DatabaseEntry::DatabaseConnectSuccess);
|
||||
DatabaseManager { ops: DatabaseOps::new(pool) }
|
||||
DatabaseManager {
|
||||
ops: DatabaseOps::new(pool),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
trace!(?err);
|
||||
@ -37,19 +35,20 @@ impl DatabaseManager {
|
||||
}
|
||||
};
|
||||
if !instance.exist_table("BackupTasks").await {
|
||||
if let Err(err) = instance.create_backup_task_table().await {
|
||||
error!("{}", err);
|
||||
}
|
||||
instance.create_backup_task_table().await.unwrap();
|
||||
}
|
||||
DATABASE_MANAGER.set(instance).unwrap();
|
||||
info!("{}", SystemEntry::InitializeComplete);
|
||||
}
|
||||
|
||||
pub async fn terminate() {
|
||||
let instance = DatabaseManager::instance();
|
||||
instance.close_connection().await;
|
||||
let _ = DatabaseOps::unlock_database().await;
|
||||
}
|
||||
|
||||
pub fn instance() -> &'static DatabaseManager {
|
||||
// Initialization has been ensured
|
||||
DATABASE_MANAGER.get().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +1,41 @@
|
||||
use std::sync::OnceLock;
|
||||
use dashmap::DashMap;
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
use crate::model::backup_task::BackupTask;
|
||||
|
||||
pub static ENGINE: OnceLock<RwLock<Engine>> = OnceLock::new();
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Engine {}
|
||||
pub struct Engine {
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub async fn initialize() {
|
||||
ENGINE.set(RwLock::new(Engine {})).unwrap();
|
||||
}
|
||||
|
||||
pub async fn terminate() {}
|
||||
pub async fn run() {
|
||||
|
||||
}
|
||||
|
||||
pub async fn terminate() {
|
||||
|
||||
}
|
||||
|
||||
pub async fn create_task() {
|
||||
|
||||
}
|
||||
|
||||
pub async fn start_task() {
|
||||
|
||||
}
|
||||
|
||||
pub async fn suspend_task() {
|
||||
|
||||
}
|
||||
|
||||
pub async fn resume_task() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
use futures::future;
|
||||
use crate::core::event_system::actor_dispatcher::ActorDispatcher;
|
||||
use crate::core::event_system::actor_ref::ActorRef;
|
||||
use crate::interface::event_system::actor::Actor;
|
||||
@ -6,6 +5,7 @@ use crate::interface::event_system::dispatcher::Dispatcher;
|
||||
use crate::interface::event_system::event::Event;
|
||||
use crate::interface::event_system::event_handler::EventHandler;
|
||||
use crate::interface::ThreadSafe;
|
||||
use futures::future;
|
||||
|
||||
pub struct ListenerGroup<E: Event> {
|
||||
dispatchers: Vec<Box<dyn Dispatcher<E> + ThreadSafe>>,
|
||||
@ -29,7 +29,8 @@ impl<E: Event> ListenerGroup<E> {
|
||||
}
|
||||
|
||||
pub async fn broadcast(&self, event: E) {
|
||||
let futures = self.dispatchers
|
||||
let futures = self
|
||||
.dispatchers
|
||||
.iter()
|
||||
.map(|dispatcher| dispatcher.dispatch(event.clone()));
|
||||
future::join_all(futures).await;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
mod listener_group;
|
||||
mod actor_ref;
|
||||
mod event_bus;
|
||||
mod actor_dispatcher;
|
||||
pub mod actor_dispatcher;
|
||||
pub mod actor_ref;
|
||||
pub mod event_bus;
|
||||
pub mod listener_group;
|
||||
|
||||
@ -23,6 +23,7 @@ impl IOManager {
|
||||
}
|
||||
|
||||
pub fn instance() -> &'static IOManager {
|
||||
// Initialization has been ensured
|
||||
IO_MANAGER.get().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
pub mod app_config;
|
||||
pub mod database_manager;
|
||||
pub mod engine;
|
||||
pub mod system;
|
||||
pub mod event_system;
|
||||
pub mod io_manager;
|
||||
mod progress_tracker;
|
||||
pub mod progress_tracker;
|
||||
pub mod system;
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
use crate::utils::logging::Logging;
|
||||
use tracing::info;
|
||||
use crate::core::app_config::AppConfig;
|
||||
use crate::core::database_manager::DatabaseManager;
|
||||
use crate::core::engine::Engine;
|
||||
use crate::core::io_manager::IOManager;
|
||||
use crate::platform::elevate::elevate;
|
||||
use crate::utils::log_entry::system::SystemEntry;
|
||||
use crate::utils::logging::Logging;
|
||||
use crate::utils::privilege::elevate;
|
||||
use privilege::user::privileged;
|
||||
use tracing::info;
|
||||
|
||||
pub struct System {}
|
||||
|
||||
@ -10,19 +15,26 @@ impl System {
|
||||
pub async fn initialize() {
|
||||
Logging::initialize().await;
|
||||
info!("{}", SystemEntry::Initializing);
|
||||
if !privileged() {
|
||||
info!("{}", SystemEntry::ReRunAsAdmin);
|
||||
elevate().map_err(SystemEntry::RunAsAdminFailed).unwrap();
|
||||
}
|
||||
AppConfig::initialization().await;
|
||||
Engine::initialize().await;
|
||||
IOManager::initialize().await;
|
||||
DatabaseManager::initialization().await;
|
||||
info!("{}", SystemEntry::InitializeComplete);
|
||||
}
|
||||
|
||||
pub async fn run() {
|
||||
Engine::run().await;
|
||||
info!("{}", SystemEntry::Online);
|
||||
|
||||
}
|
||||
|
||||
pub async fn terminate() {
|
||||
info!("{}", SystemEntry::Terminating);
|
||||
Engine::terminate().await;
|
||||
DatabaseManager::terminate().await;
|
||||
info!("{}", SystemEntry::TerminateComplete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +42,11 @@ pub trait DatabaseOpsTrait {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn close_connection(&self) {
|
||||
let pool = self.get_pool();
|
||||
pool.close().await
|
||||
}
|
||||
|
||||
async fn exist_table(&self, table_name: &str) -> bool {
|
||||
let pool = self.get_pool();
|
||||
sqlx::query_scalar::<_, bool>(
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use crate::interface::event_system::event::Event;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Dispatcher<E: Event> {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
pub mod actor;
|
||||
pub mod dispatcher;
|
||||
pub mod event;
|
||||
pub mod event_handler;
|
||||
pub mod actor;
|
||||
pub mod dispatcher;
|
||||
@ -1,10 +1,13 @@
|
||||
use crate::core::event_system::event_bus::EventBus;
|
||||
use crate::model::event::io_event::{IOEvent, IOType};
|
||||
use async_trait::async_trait;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::fs;
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio_stream::StreamExt;
|
||||
use tokio_stream::wrappers::ReadDirStream;
|
||||
use tokio_stream::StreamExt;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[async_trait]
|
||||
pub trait FileSystemTrait {
|
||||
@ -24,31 +27,64 @@ pub trait FileSystemTrait {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn create_directory(&self, path: PathBuf) -> anyhow::Result<()> {
|
||||
async fn create_directory(&self, task_id: Uuid, path: PathBuf) -> anyhow::Result<()> {
|
||||
let semaphore = self.semaphore();
|
||||
let _permit = semaphore.acquire_owned().await?;
|
||||
fs::create_dir_all(path).await?;
|
||||
fs::create_dir_all(&path).await?;
|
||||
let io_event = IOEvent {
|
||||
task_id,
|
||||
io_type: IOType::CreateDirectory,
|
||||
source: None,
|
||||
destination,
|
||||
};
|
||||
EventBus::publish(io_event).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn copy_file(&self, source: PathBuf, destination: PathBuf) -> anyhow::Result<()> {
|
||||
async fn copy_file(
|
||||
&self,
|
||||
task_id: Uuid,
|
||||
source: PathBuf,
|
||||
destination: PathBuf,
|
||||
) -> anyhow::Result<()> {
|
||||
let semaphore = self.semaphore();
|
||||
let _permit = semaphore.acquire_owned().await?;
|
||||
fs::copy(source, destination).await?;
|
||||
fs::copy(&source, &destination).await?;
|
||||
let io_event = IOEvent {
|
||||
task_id,
|
||||
io_type: IOType::CopyFile,
|
||||
source: Some(source),
|
||||
destination,
|
||||
};
|
||||
EventBus::publish(io_event).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_directory(&self, path: PathBuf) -> anyhow::Result<()> {
|
||||
async fn delete_directory(&self, task_id: Uuid, path: PathBuf) -> anyhow::Result<()> {
|
||||
let semaphore = self.semaphore();
|
||||
let _permit = semaphore.acquire_owned().await?;
|
||||
fs::remove_dir_all(path).await?;
|
||||
fs::remove_dir_all(&path).await?;
|
||||
let io_event = IOEvent {
|
||||
task_id,
|
||||
io_type: IOType::DeleteDirectory,
|
||||
source: None,
|
||||
destination,
|
||||
};
|
||||
EventBus::publish(io_event).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_file(&self, path: PathBuf) -> anyhow::Result<()> {
|
||||
async fn delete_file(&self, task_id: Uuid, path: PathBuf) -> anyhow::Result<()> {
|
||||
let semaphore = self.semaphore();
|
||||
let _permit = semaphore.acquire_owned().await?;
|
||||
fs::remove_file(path).await?;
|
||||
let io_event = IOEvent {
|
||||
task_id,
|
||||
io_type: IOType::DeleteFile,
|
||||
source: None,
|
||||
destination,
|
||||
};
|
||||
EventBus::publish(io_event).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,13 @@ use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BackupState {
|
||||
Running,
|
||||
Suspended,
|
||||
Stopped,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum BackupType {
|
||||
Full,
|
||||
@ -34,8 +41,10 @@ pub struct BackupOptions {
|
||||
advanced_file_attr: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BackupTask {
|
||||
pub uuid: Uuid,
|
||||
pub state: BackupState,
|
||||
pub source_path: PathBuf,
|
||||
pub destination_path: PathBuf,
|
||||
pub backup_type: BackupType,
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub enum DiffType {
|
||||
Created,
|
||||
Modified,
|
||||
Deleted,
|
||||
}
|
||||
|
||||
pub struct DiffEntry {
|
||||
pub diff_type: DiffType,
|
||||
pub source: Option<PathBuf>,
|
||||
pub destination: Option<PathBuf>,
|
||||
}
|
||||
@ -9,6 +9,8 @@ pub enum IOType {
|
||||
DeleteDirectory,
|
||||
CopyFile,
|
||||
DeleteFile,
|
||||
GetAttributes,
|
||||
CalculateHash,
|
||||
ChangeAttributes,
|
||||
ChangeAccessControlList,
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
pub mod io_event;
|
||||
pub mod io_event;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
pub mod backup_task;
|
||||
pub mod config;
|
||||
pub mod diff_entry;
|
||||
pub mod event;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use crate::interface::database_ops::DatabaseOpsTrait;
|
||||
use async_trait::async_trait;
|
||||
use sqlx::SqlitePool;
|
||||
use crate::interface::database_ops::DatabaseOpsTrait;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DatabaseOps {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::Semaphore;
|
||||
use crate::interface::file_system::FileSystemTrait;
|
||||
use async_trait::async_trait;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Semaphore;
|
||||
|
||||
pub struct FileSystem {
|
||||
semaphore: Arc<Semaphore>,
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use sqlx::SqlitePool;
|
||||
use crate::interface::database_ops::DatabaseOpsTrait;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DatabaseOps {
|
||||
|
||||
@ -1,17 +1,11 @@
|
||||
use crate::interface::file_system::FileSystemTrait;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Semaphore;
|
||||
use crate::interface::file_system::FileSystemTrait;
|
||||
|
||||
pub struct FileSystem {
|
||||
semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl FileSystem {
|
||||
pub fn test(&self) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl FileSystemTrait for FileSystem {
|
||||
fn new(semaphore: Arc<Semaphore>) -> Self {
|
||||
FileSystem { semaphore }
|
||||
|
||||
@ -2,6 +2,10 @@ use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SystemEntry {
|
||||
#[error("Rerun as administrator")]
|
||||
ReRunAsAdmin,
|
||||
#[error("Unable to run as administrator")]
|
||||
RunAsAdminFailed,
|
||||
#[error("Online now")]
|
||||
Online,
|
||||
#[error("Initializing")]
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
pub use crate::platform::elevate::elevate;
|
||||
pub use privilege::user::privileged;
|
||||
Loading…
x
Reference in New Issue
Block a user