diff --git a/Cargo.lock b/Cargo.lock index 988f725..39455ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,18 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b79b82693f705137f8fb9b37871d99e4f9a7df12b917eed79c3d3954830a60b" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -26,6 +38,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "autocfg" version = "1.2.0" @@ -136,6 +154,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "crypto-common" version = "0.1.6" @@ -206,6 +239,18 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" +[[package]] +name = "filetime" +version = "0.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "windows-sys 0.52.0", +] + [[package]] name = "fnv" version = "1.0.7" @@ -236,6 +281,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fsevent-sys" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" +dependencies = [ + "libc", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -251,6 +305,23 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -270,9 +341,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", + "futures-macro", "futures-task", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -293,9 +366,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ "bytes", "fnv", @@ -315,6 +388,10 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "hermit-abi" @@ -433,6 +510,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "inotify" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff" +dependencies = [ + "bitflags 1.3.2", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" +dependencies = [ + "libc", +] + [[package]] name = "instant" version = "0.1.12" @@ -474,6 +571,26 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kqueue" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7447f1ca1b7b563588a205fe93dea8df60fd981423a768bc1c0ded35ed147d0c" +dependencies = [ + "kqueue-sys", + "libc", +] + +[[package]] +name = "kqueue-sys" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -540,6 +657,7 @@ dependencies = [ "indicatif", "log", "md-5", + "notify", "pretty_env_logger", "reqwest", "serde", @@ -549,6 +667,7 @@ dependencies = [ "tempfile", "thiserror", "tokio", + "tokio-util", "toml", ] @@ -568,6 +687,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" dependencies = [ "libc", + "log", "wasi", "windows-sys 0.48.0", ] @@ -590,6 +710,26 @@ dependencies = [ "tempfile", ] +[[package]] +name = "notify" +version = "6.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d" +dependencies = [ + "bitflags 2.5.0", + "crossbeam-channel", + "filetime", + "fsevent-sys", + "inotify", + "kqueue", + "libc", + "log", + "mio", + "serde", + "walkdir", + "windows-sys 0.48.0", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -655,9 +795,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.101" +version = "0.9.102" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" +checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" dependencies = [ "cc", "libc", @@ -696,9 +836,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -858,6 +998,15 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.23" @@ -875,9 +1024,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "security-framework" -version = "2.9.2" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6" dependencies = [ "bitflags 1.3.2", "core-foundation", @@ -888,9 +1037,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.9.1" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef" dependencies = [ "core-foundation-sys", "libc", @@ -1006,9 +1155,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -1100,9 +1249,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.36.0" +version = "1.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" dependencies = [ "backtrace", "bytes", @@ -1146,8 +1295,12 @@ checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" dependencies = [ "bytes", "futures-core", + "futures-io", "futures-sink", + "futures-util", + "hashbrown", "pin-project-lite", + "slab", "tokio", "tracing", ] @@ -1273,6 +1426,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1545,3 +1708,23 @@ dependencies = [ "cfg-if", "windows-sys 0.48.0", ] + +[[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 20d093e..7cc105f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" indicatif = { version = "0.17.8", features = ["tokio"] } log = { version = "0.4.21", features = ["serde"] } md-5 = "0.10.6" +notify = { version = "6.1.1", features = ["serde"] } pretty_env_logger = "0.5.0" reqwest = { version = "0.11.22", features = ["json"] } serde = { version = "1.0.193", features = ["derive"] } @@ -19,4 +20,5 @@ sha2 = "0.10.8" tempfile = "3.10.1" thiserror = "1.0.57" tokio = { version = "1.34.0", features = ["full"] } +tokio-util = { version = "0.7.10", features = ["full"] } toml = "0.8.8" diff --git a/config.toml b/config.toml index 98d85ba..0e2718a 100644 --- a/config.toml +++ b/config.toml @@ -11,8 +11,8 @@ force_update = false # [plugins.itemswapper] [additions] -configPluguinsFrom = "git@github.com:TOwInOK/test.git" -key = "SUPEREKLy" +# configPluguinsFrom = "git@github.com:TOwInOK/test.git" +# key = "SUPEREKLy" path_to_core = "core" # path_to_mods = "" path_to_plugins = "plugins" diff --git a/src/controller/mod.rs b/src/controller/mod.rs index bfd3f75..2b6413a 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -1,9 +1,12 @@ -use std::time::Duration; +use std::{error::Error, time::Duration}; use log::{error, info, trace}; use tokio::{sync::Mutex, time::sleep}; +use tokio_util::sync::CancellationToken; -use crate::{config::Config, downloader::Downloader, errors::error::LockErrors, lock::locker::Lock}; +use crate::{ + config::Config, downloader::Downloader, errors::error::LockErrors, lock::locker::Lock, +}; pub struct Controller { config: Mutex, @@ -11,10 +14,9 @@ pub struct Controller { } impl Controller { - pub async fn init() -> Self { - let mut controller = Self::new().await; - controller.run().await; - controller + pub async fn init() { + let controller = Self::new().await; + controller.watch_config_changes().await; } async fn new() -> Self { @@ -41,7 +43,7 @@ impl Controller { Self { config, lock } } - async fn run(&mut self) { + async fn run(&mut self, token: CancellationToken) { // let sleep_cooldown = self.config.lock().await.additions.time_to_await; let cooldown = 100; loop { @@ -49,53 +51,67 @@ impl Controller { self.start().await; // Sleep for 5 minutes - sleep(Duration::from_secs(cooldown)).await; + tokio::select! { + _ = sleep(Duration::from_millis(cooldown)) => {}, + _ = token.cancelled() => break, + }; } } - async fn start(&mut self) { - let config = self.config.get_mut(); - let lock = self.lock.get_mut(); + /// Check zombies entities. + /// Start download fn. + async fn start(&self) { + let mut config = self.config.lock().await; + let mut lock = self.lock.lock().await; + info!("Start removing useless things"); - remove_zombies(lock, config) - .await - .unwrap_or_else(|e| error!("{e}")); + if let Err(e) = remove_zombies(&mut lock, &mut config).await { + error!("{:?}", e); + } + info!("Init downloader"); - Downloader::init(config, lock) + if let Err(e) = Downloader::init(&mut config, &mut lock) .check_and_download() .await - .unwrap_or_else(|e| error!("{e}")); + { + error!("{:?}", e); + } } - pub async fn watch_config_changes(&mut self) { + pub async fn watch_config_changes(&self) { + let token = CancellationToken::new(); + let downloader = tokio::spawn(self.run(token)); + // Load new Config file - let path = self - .config - .lock() - .await - .additions - .path_to_configs - .to_owned(); - let config = Config::load_config(&path).await.unwrap_or_else(|e| { - log::error!("message: {}", e); - log::warn!("Происходит загрузка стандартного конфига"); - Config::default() - }); - log::debug!("{:#?}", config); + let path = self.config.lock().await.additions.path_to_configs.to_owned(); + let config = match Config::load_config(&path).await { + Ok(config) => { + log::debug!("{:#?}", config); + config + } + Err(e) => { + log::error!("message: {}", e); + log::warn!("Происходит загрузка стандартного конфига"); + Config::default() + } + }; // Load new lock let mut lock = Lock::default(); if let Err(e) = lock.load(&config.additions.path_to_configs).await { - error!("{e}"); + error!("{:?}", e); lock.create(&config.additions.path_to_configs) .await .unwrap(); lock.load(&config.additions.path_to_configs) .await - .unwrap_or_else(|e| error!("{e}")); + .unwrap_or_else(|e| error!("{:?}", e)); } - self.lock = lock.into(); - self.config = config.into(); + + *self.lock.lock().await = lock; + *self.config.lock().await = config; + + watcher(token).await; } } @@ -106,3 +122,9 @@ async fn remove_zombies(lock: &mut Lock, config: &Config) -> Result<(), LockErro lock.remove_if_not_exist_plugin(config).await?; lock.remove_if_not_exist_core(config).await } + + +async fn watcher(token: CancellationToken) { + token.cancel(); + info!("Token Stopped {:#?}", token) +} \ No newline at end of file diff --git a/src/downloader/core.rs b/src/downloader/core.rs index 04e22d9..6a233b1 100644 --- a/src/downloader/core.rs +++ b/src/downloader/core.rs @@ -23,9 +23,9 @@ async fn get_core_link(core: &mut Core) -> Result<(String, ChooseHash), Download Provider::Paper => Paper::get_link(core).await, Provider::Folia => Folia::get_link(core).await, Provider::Purpur => Purpur::get_link(core).await, - Provider::Fabric => todo!(), - Provider::Forge => todo!(), - Provider::NeoForge => todo!(), + // Provider::Fabric => todo!(), + // Provider::Forge => todo!(), + // Provider::NeoForge => todo!(), Provider::Waterfall => Waterfall::get_link(core).await, Provider::Velocity => Velocity::get_link(core).await, } diff --git a/src/main.rs b/src/main.rs index 3efb34b..78431c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,5 @@ async fn main() { .filter_level(log::LevelFilter::Trace) .init(); - controller::Controller::init() - .await - .watch_config_changes() - .await; + let _ = controller::Controller::init().await; }