diff --git a/fang/Cargo.toml b/fang/Cargo.toml index 97be165..f1b8daf 100644 --- a/fang/Cargo.toml +++ b/fang/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "fang" version = "0.11.0-rc1" -authors = ["Ayrat Badykov " , "Pepe Márquez "] +authors = [ + "Ayrat Badykov ", + "Pepe Márquez ", +] description = "Background job processing library for Rust" repository = "https://github.com/ayrat555/fang" edition = "2021" @@ -15,17 +18,35 @@ rust-version = "1.77" doctest = false [features] -default = ["blocking", "asynk-sqlx", "derive-error", "blocking-postgres", "blocking-mysql" , "blocking-sqlite", "migrations-postgres", "migrations-sqlite", "migrations-mysql"] -asynk-postgres = ["asynk-sqlx" , "sqlx?/postgres"] -asynk-sqlite = ["asynk-sqlx" , "sqlx?/sqlite"] -asynk-mysql = ["asynk-sqlx" , "sqlx?/mysql"] -asynk-sqlx = ["asynk" , "dep:sqlx"] -asynk = ["dep:tokio", "dep:async-trait", "dep:async-recursion" ] +default = [ + "blocking", + "asynk-sqlx", + "derive-error", + "blocking-postgres", + "blocking-mysql", + "blocking-sqlite", + "migrations-postgres", + "migrations-sqlite", + "migrations-mysql", +] +asynk-postgres = ["asynk-sqlx", "sqlx?/postgres"] +asynk-sqlite = ["asynk-sqlx", "sqlx?/sqlite"] +asynk-mysql = ["asynk-sqlx", "sqlx?/mysql"] +asynk-sqlx = ["asynk", "dep:sqlx"] +asynk = ["dep:tokio", "dep:async-trait", "dep:async-recursion"] derive-error = ["dep:fang-derive-error"] -blocking = ["dep:diesel", "dep:diesel-derive-enum", "dep:dotenvy", "diesel?/chrono" , "diesel?/serde_json" , "diesel?/uuid", "diesel?/r2d2"] -blocking-postgres = [ "blocking", "diesel?/postgres"] -blocking-sqlite = ["blocking", "diesel?/sqlite" ] -blocking-mysql = [ "blocking", "diesel?/mysql"] +blocking = [ + "dep:diesel", + "dep:diesel-derive-enum", + "dep:dotenvy", + "diesel?/chrono", + "diesel?/serde_json", + "diesel?/uuid", + "diesel?/r2d2", +] +blocking-postgres = ["blocking", "diesel?/postgres"] +blocking-sqlite = ["blocking", "diesel?/sqlite"] +blocking-mysql = ["blocking", "diesel?/mysql"] migrations-postgres = ["migrations", "diesel?/postgres"] migrations-sqlite = ["migrations", "diesel?/sqlite"] migrations-mysql = ["migrations", "diesel?/mysql"] @@ -33,9 +54,39 @@ migrations = ["dep:diesel_migrations", "dep:diesel"] [dev-dependencies] -fang-derive-error = { version = "0.1.0"} -diesel_migrations = { version = "2.1" , features = ["postgres", "sqlite" , "mysql"]} -sqlx = {version = "0.6.3", features = ["any" , "macros" , "chrono", "uuid", "json","runtime-tokio-rustls", "postgres", "sqlite", "mysql"]} +fang-derive-error = { version = "0.1.0" } +diesel_migrations = { version = "2.1", features = [ + "postgres", + "sqlite", + "mysql", +] } +#sqlx = { version = "0.8", features = [ +# "any", +# "macros", +# "chrono", +# "uuid", +# "json", +# "runtime-tokio-rustls", +# "postgres", +# "sqlite", +# "mysql", +#] } + +sqlx = { git = "https://github.com/launchbadge/sqlx.git", branch = "main", features = [ + "any", + "macros", + "chrono", + "uuid", + "json", + "runtime-tokio-rustls", + "postgres", + "sqlite", + "mysql", +] } + +url = { version = "2.2.2" } + + #console-subscriber = "0.2.0" # for tokio tracing debug [dependencies] @@ -51,8 +102,29 @@ thiserror = "1.0" typed-builder = "0.14" typetag = "0.2" uuid = { version = "1.1", features = ["v4"] } -fang-derive-error = { version = "0.1.0" , optional = true} -sqlx = {version = "0.6.3", features = ["any" , "macros" , "chrono", "uuid", "json", "runtime-tokio-rustls"], optional = true} +fang-derive-error = { version = "0.1.0", optional = true } +#sqlx = { version = "0.8", features = [ +# "any", +# "macros", +# "chrono", +# "uuid", +# "json", +# "runtime-tokio-rustls", +# "postgres", +# "sqlite", +# "mysql", +#] } +sqlx = { git = "https://github.com/launchbadge/sqlx.git", branch = "main", features = [ + "any", + "macros", + "chrono", + "uuid", + "json", + "runtime-tokio-rustls", +], optional = true } + +url = { version = "2.2.2" } + [dependencies.diesel] version = "2.1" @@ -70,7 +142,7 @@ optional = true [dependencies.tokio] version = "1.25" -features = ["rt", "time", "macros"]#, "tracing"] +features = ["rt", "time", "macros"] #, "tracing"] optional = true [dependencies.async-trait] diff --git a/fang/src/asynk/async_queue.rs b/fang/src/asynk/async_queue.rs index c44abb0..62fa34a 100644 --- a/fang/src/asynk/async_queue.rs +++ b/fang/src/asynk/async_queue.rs @@ -13,18 +13,18 @@ use async_trait::async_trait; use chrono::DateTime; use chrono::Utc; use cron::Schedule; -use sqlx::any::AnyConnectOptions; -use sqlx::any::AnyKind; +use sqlx::any::install_default_drivers; + #[cfg(any( feature = "asynk-postgres", feature = "asynk-mysql", feature = "asynk-sqlite" ))] use sqlx::pool::PoolOptions; -//use sqlx::any::install_default_drivers; // this is supported in sqlx 0.7 use std::str::FromStr; use thiserror::Error; use typed_builder::TypedBuilder; +use url::Url; use uuid::Uuid; #[cfg(feature = "asynk-postgres")] @@ -54,6 +54,8 @@ pub enum AsyncQueueError { #[error(transparent)] SerdeError(#[from] serde_json::Error), #[error(transparent)] + UrlError(#[from] url::ParseError), + #[error(transparent)] CronError(#[from] CronError), #[error("returned invalid result (expected {expected:?}, found {found:?})")] ResultError { expected: u64, found: u64 }, @@ -238,13 +240,13 @@ use std::env; use super::backend_sqlx::BackendSqlX; async fn get_pool( - kind: AnyKind, + kind: &str, _uri: &str, _max_connections: u32, ) -> Result { match kind { #[cfg(feature = "asynk-postgres")] - AnyKind::Postgres => { + "postgres" => { let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) @@ -252,23 +254,23 @@ async fn get_pool( Ok(InternalPool::Pg(pool)) } - #[cfg(feature = "asynk-mysql")] - AnyKind::MySql => { - let pool = PoolOptions::::new() + #[cfg(feature = "asynk-sqlite")] + "sqlite" => { + let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) .await?; - Ok(InternalPool::MySql(pool)) + Ok(InternalPool::Sqlite(pool)) } - #[cfg(feature = "asynk-sqlite")] - AnyKind::Sqlite => { - let pool = PoolOptions::::new() + #[cfg(feature = "asynk-mysql")] + "mysql" => { + let pool = PoolOptions::::new() .max_connections(_max_connections) .connect(_uri) .await?; - Ok(InternalPool::Sqlite(pool)) + Ok(InternalPool::MySql(pool)) } #[allow(unreachable_patterns)] _ => Err(AsyncQueueError::ConnectionError), @@ -287,9 +289,11 @@ impl AsyncQueue { /// Connect to the db if not connected pub async fn connect(&mut self) -> Result<(), AsyncQueueError> { - //install_default_drivers(); + install_default_drivers(); + + let kind = Url::parse(&self.uri)?; - let kind: AnyKind = self.uri.parse::()?.kind(); + let kind = kind.scheme(); let pool = get_pool(kind, &self.uri, self.max_pool_size).await?; diff --git a/fang/src/asynk/async_runnable.rs b/fang/src/asynk/async_runnable.rs index bc0852b..d63dd56 100644 --- a/fang/src/asynk/async_runnable.rs +++ b/fang/src/asynk/async_runnable.rs @@ -58,13 +58,12 @@ pub trait AsyncRunnable: Send + Sync { /// /** ```rust - fn cron(&self) -> Option { - let expression = "0/20 * * * Aug-Sep * 2022/1"; - Some(Scheduled::CronPattern(expression.to_string())) - } + fn cron(&self) -> Option { + let expression = "0/20 * * * Aug-Sep * 2022/1"; + Some(Scheduled::CronPattern(expression.to_string())) + } ``` */ - /// In order to schedule a task once, use the `Scheduled::ScheduleOnce` enum variant. fn cron(&self) -> Option { None diff --git a/fang/src/asynk/backend_sqlx.rs b/fang/src/asynk/backend_sqlx.rs index cfe57bb..ef138fc 100644 --- a/fang/src/asynk/backend_sqlx.rs +++ b/fang/src/asynk/backend_sqlx.rs @@ -2,8 +2,8 @@ use chrono::{DateTime, Utc}; use sha2::Digest; use sha2::Sha256; use { - chrono::Duration, sqlx::any::AnyQueryResult, sqlx::database::HasArguments, sqlx::Database, - sqlx::Encode, sqlx::Executor, sqlx::FromRow, sqlx::IntoArguments, sqlx::Pool, sqlx::Type, + chrono::Duration, sqlx::any::AnyQueryResult, sqlx::Database, sqlx::Encode, sqlx::Executor, + sqlx::FromRow, sqlx::IntoArguments, sqlx::Pool, sqlx::Type, }; use std::fmt::Debug; @@ -102,7 +102,6 @@ impl BackendSqlX { } } - // I think it is useful to have this method, although it is not used pub(crate) fn _name(&self) -> &str { match *self { #[cfg(feature = "asynk-postgres")] @@ -155,7 +154,7 @@ where for<'r> &'r Uuid: Encode<'r, DB> + Type, for<'r> &'r serde_json::Value: Encode<'r, DB> + Type, for<'r> &'r Pool: Executor<'r, Database = DB>, - for<'r> >::Arguments: IntoArguments<'r, DB>, + for<'r> ::Arguments<'r>: IntoArguments<'r, DB>, ::QueryResult: Into, { async fn fetch_task_type( diff --git a/fang/src/blocking/worker.rs b/fang/src/blocking/worker.rs index f2dc33a..937874c 100644 --- a/fang/src/blocking/worker.rs +++ b/fang/src/blocking/worker.rs @@ -435,7 +435,6 @@ mod worker_tests { #[test] #[ignore] - fn no_schedule_until_run() { let task = TaskScheduled {}; diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..9a58c88 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,6 @@ +[toolchain] +versioned = "1.83" +channel = "stable-x86_64-unknown-linux-gnu" +components = ["rustfmt", "rustc-dev", "clippy"] +targets = ["x86_64-unknown-linux-gnu"] +profile = "minimal"