From eaca78053cfeb299a8d7495f3664e5fc97a99187 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sat, 12 Aug 2023 13:37:02 +0200 Subject: [PATCH] Fix --- Cargo.lock | 155 ++++++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 2 + src/main.rs | 73 ++++++++++++++++--------- 3 files changed, 193 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a8901b0..2968635 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "async-tempfile" version = "0.4.0" @@ -55,7 +70,7 @@ checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -197,6 +212,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "winapi", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -213,6 +240,17 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "cron" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ff76b51e4c068c52bfd2866e1567bee7c567ae8f24ada09fd4307019e25eab7" +dependencies = [ + "chrono", + "nom", + "once_cell", +] + [[package]] name = "crossbeam-epoch" version = "0.9.15" @@ -297,6 +335,7 @@ dependencies = [ "futures-util", "sentry", "tokio", + "tokio-cron-scheduler", "tokio-util", "tower-http", "tracing", @@ -402,7 +441,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -586,6 +625,29 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "idna" version = "0.4.0" @@ -743,7 +805,7 @@ checksum = "ddece26afd34c31585c74a4db0630c376df271c285d682d1e55012197830b6df" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -767,6 +829,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -823,6 +891,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -833,6 +911,26 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-derive" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -881,7 +979,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -965,7 +1063,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -1370,7 +1468,7 @@ checksum = "aafe972d60b0b9bee71a91b92fee2d4fb3c9d7e8f6b179aa99f27203d99a4816" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -1471,6 +1569,17 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.28" @@ -1518,7 +1627,7 @@ checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -1593,6 +1702,21 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "tokio-cron-scheduler" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2c1fd54a857b29c6cd1846f31903d0ae8e28175615c14a277aed45c58d8e27" +dependencies = [ + "chrono", + "cron", + "num-derive", + "num-traits", + "tokio", + "tracing", + "uuid", +] + [[package]] name = "tokio-macros" version = "2.1.0" @@ -1601,7 +1725,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -1697,7 +1821,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", ] [[package]] @@ -1864,7 +1988,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.28", "wasm-bindgen-shared", ] @@ -1898,7 +2022,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.28", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1941,6 +2065,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 88017f4..6024add 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,5 @@ async-tempfile = "0.4.0" uuid = "1.4.1" sentry = { version = "0.31.5", features = ["debug-images"] } + +tokio-cron-scheduler = "0.9.4" diff --git a/src/main.rs b/src/main.rs index 545e2dc..9ddfb2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ -use std::{net::SocketAddr, time::{SystemTime, Duration}, str::FromStr, io::SeekFrom}; -use axum::{Router, routing::{post, get}, extract::{Multipart, Path, BodyStream}, response::{IntoResponse, AppendHeaders, Response}, http::{StatusCode, header, Request, self}, body::StreamBody, middleware::{Next, self}}; +use std::{net::SocketAddr, str::FromStr, io::SeekFrom}; +use axum::{Router, routing::{post, get}, extract::{Path, BodyStream}, response::{IntoResponse, AppendHeaders, Response}, http::{StatusCode, header, Request, self}, body::StreamBody, middleware::{Next, self}}; use axum_prometheus::PrometheusMetricLayer; use futures_util::StreamExt; use sentry::{ClientOptions, types::Dsn, integrations::debug_images::DebugImagesIntegration}; -use tokio::{fs::{remove_file, read_dir, remove_dir, File}, io::{AsyncWriteExt, copy, AsyncSeekExt}, process::Command, time::sleep}; +use tokio::{fs::{remove_file, read_dir, remove_dir, File}, io::{AsyncWriteExt, AsyncSeekExt}, process::Command}; +use tokio_cron_scheduler::{JobScheduler, Job}; use tower_http::trace::{TraceLayer, self}; use tracing::{info, log, Level}; use async_tempfile::TempFile; @@ -11,19 +12,11 @@ use tokio_util::io::ReaderStream; async fn remove_temp_files() -> Result<(), Box> { - let _ = remove_file("./conversion.log").await?; - let mut dir = read_dir("/tmp/").await?; - let now = SystemTime::now(); - while let Some(child) = dir.next_entry().await? { let metadata = child.metadata().await?; - if now.duration_since(metadata.modified()?)?.as_secs() < 60 * 60 * 3 { - continue; - } - if metadata.is_dir() { let _ = remove_dir(child.path()).await; } else { @@ -126,11 +119,6 @@ async fn convert_file( ) ]); - // tokio::spawn(async { - // sleep(Duration::from_secs(5 * 60)).await; - // remove_temp_files().await - // }); - (headers, body).into_response() } @@ -177,6 +165,45 @@ fn get_router() -> Router { } +async fn cron_jobs() { + let job_scheduler = JobScheduler::new().await.unwrap(); + + let remote_temp_files_job = match Job::new_async("0 0 */6 * * *", |_uuid, _l| Box::pin(async { + match remove_temp_files().await { + Ok(_) => log::info!("Updated"), + Err(err) => log::info!("Update err: {:?}", err), + }; + })) { + Ok(v) => v, + Err(err) => panic!("{:?}", err), + }; + + job_scheduler.add(remote_temp_files_job).await.unwrap(); + + log::info!("Scheduler start..."); + match job_scheduler.start().await { + Ok(v) => v, + Err(err) => panic!("{:?}", err), + }; + + log::info!("Scheduler shutdown..."); +} + + +async fn start_app() { + let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); + + let app = get_router(); + + info!("Start webserver..."); + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); + info!("Webserver shutdown..."); +} + + #[tokio::main] async fn main() { tracing_subscriber::fmt() @@ -195,14 +222,8 @@ async fn main() { let _guard = sentry::init(options); - let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); - - let app = get_router(); - - info!("Start webserver..."); - axum::Server::bind(&addr) - .serve(app.into_make_service()) - .await - .unwrap(); - info!("Webserver shutdown...") + tokio::join![ + cron_jobs(), + start_app() + ]; }