diff --git a/Cargo.lock b/Cargo.lock index 2efac25100..3598343782 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3624,20 +3624,13 @@ name = "lock-service" version = "0.1.0" dependencies = [ "async-graphql", - "chrono", "common-models", "common-utils", - "config", - "database-models", - "dependent-models", - "env-utils", "sea-orm", - "sea-query", "serde", "serde_json", "sqlx", "tracing", - "uuid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d4fd8a3420..00dbee9e11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -145,7 +145,7 @@ serde_with = { version = "=3.11.0", features = ["chrono_0_4"] } serde-xml-rs = "=0.6.0" slug = "=0.1.6" sonarr-api-rs = "=3.0.0" -sqlx = "=0.8.2" +sqlx = { version = "=0.8.2", default-features = false, features = ["postgres"] } strum = { version = "=0.26.3", features = ["derive"] } struson = { version = "=0.6.0", features = ["serde"] } reqwest = { version = "=0.12.9", features = ["json", "stream"] } diff --git a/crates/services/lock/Cargo.toml b/crates/services/lock/Cargo.toml index 65183b0ac7..45b73dde8a 100644 --- a/crates/services/lock/Cargo.toml +++ b/crates/services/lock/Cargo.toml @@ -5,20 +5,13 @@ edition = "2021" [dependencies] async-graphql = { workspace = true } -chrono = { workspace = true } common-models = { path = "../../models/common" } common-utils = { path = "../../utils/common" } -config = { path = "../../config" } -database-models = { path = "../../models/database" } -dependent-models = { path = "../../models/dependent" } -env-utils = { path = "../../utils/env" } sea-orm = { workspace = true } -sea-query = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } sqlx = { workspace = true } tracing = { workspace = true } -uuid = { workspace = true } [package.metadata.cargo-machete] ignored = ["serde", "tracing"] diff --git a/crates/services/lock/src/lib.rs b/crates/services/lock/src/lib.rs index 771ec78d40..96c1705786 100644 --- a/crates/services/lock/src/lib.rs +++ b/crates/services/lock/src/lib.rs @@ -1,6 +1,8 @@ use async_graphql::Result; use common_models::ApplicationCacheKey; +use common_utils::ryot_log; use sea_orm::DatabaseConnection; +use sqlx::postgres::PgAdvisoryLock; pub struct LockService { db: DatabaseConnection, @@ -13,7 +15,12 @@ impl LockService { } impl LockService { - pub async fn acquire_lock(&self, key: ApplicationCacheKey) -> Result { - todo!() + pub async fn acquire_lock(&self, key: &ApplicationCacheKey) -> Result<()> { + let key_string = serde_json::to_string(key)?; + let lock = PgAdvisoryLock::new(key_string); + ryot_log!(debug, "Acquiring lock for key: {:?}", key); + let conn = self.db.get_postgres_connection_pool().acquire().await?; + lock.acquire(conn).await?; + Ok(()) } }