Skip to content

Commit

Permalink
feat(services/lock): add implementation to lock service
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Dec 26, 2024
1 parent a0f96ed commit 0a9ec67
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 0 additions & 7 deletions crates/services/lock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
11 changes: 9 additions & 2 deletions crates/services/lock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,7 +15,12 @@ impl LockService {
}

impl LockService {
pub async fn acquire_lock(&self, key: ApplicationCacheKey) -> Result<bool> {
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(())
}
}

0 comments on commit 0a9ec67

Please sign in to comment.