|
| 1 | +use std::error::Error; |
| 2 | + |
| 3 | +use diesel::prelude::*; |
| 4 | +use diesel::sql_types::BigInt; |
| 5 | +use diesel::PgConnection; |
| 6 | + |
| 7 | +sql_function!(fn pg_try_advisory_lock(key: BigInt) -> Bool); |
| 8 | +sql_function!(fn pg_advisory_unlock(key: BigInt) -> Bool); |
| 9 | + |
| 10 | +/// Run the callback if the session advisory lock for the given key can be obtained. |
| 11 | +/// |
| 12 | +/// If the lock is not already held, the callback will be called and the lock will be unlocked |
| 13 | +/// after the closure returns. If the lock is already held, the function returns an error without |
| 14 | +/// calling the callback. |
| 15 | +pub(crate) fn with_advisory_lock<F>( |
| 16 | + conn: &PgConnection, |
| 17 | + key: i64, |
| 18 | + f: F, |
| 19 | +) -> Result<(), Box<dyn Error>> |
| 20 | +where |
| 21 | + F: FnOnce(&PgConnection) -> QueryResult<()>, |
| 22 | +{ |
| 23 | + if !diesel::select(pg_try_advisory_lock(key)).get_result(conn)? { |
| 24 | + let string = format!( |
| 25 | + "A job holding the session advisory lock for key {} is already running", |
| 26 | + key |
| 27 | + ); |
| 28 | + println!("return"); |
| 29 | + return Err(string.into()); |
| 30 | + } |
| 31 | + println!("umm"); |
| 32 | + let _dont_drop_yet = DropGuard { conn, key }; |
| 33 | + f(conn).map_err(Into::into) |
| 34 | +} |
| 35 | + |
| 36 | +struct DropGuard<'a> { |
| 37 | + conn: &'a PgConnection, |
| 38 | + key: i64, |
| 39 | +} |
| 40 | + |
| 41 | +impl<'a> Drop for DropGuard<'a> { |
| 42 | + fn drop(&mut self) { |
| 43 | + match diesel::select(pg_advisory_unlock(self.key)).get_result(self.conn) { |
| 44 | + Ok(true) => (), |
| 45 | + Ok(false) => println!( |
| 46 | + "Error: job advisory lock for key {} was not locked", |
| 47 | + self.key |
| 48 | + ), |
| 49 | + Err(err) => println!("Error unlocking advisory lock (key: {}): {}", self.key, err), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + use crate::test_util::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn lock_released_after_callback_returns() { |
| 61 | + const KEY: i64 = -1; |
| 62 | + let conn1 = pg_connection(); |
| 63 | + let conn2 = pg_connection(); |
| 64 | + |
| 65 | + let mut callback_run = false; |
| 66 | + let result = with_advisory_lock(&conn1, KEY, |_| { |
| 67 | + // Another connection cannot obtain the lock |
| 68 | + assert!(!diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2)?); |
| 69 | + callback_run = true; |
| 70 | + Ok(()) |
| 71 | + }); |
| 72 | + assert!(result.is_ok()); |
| 73 | + assert!(callback_run); |
| 74 | + |
| 75 | + // Another connection can now obtain the lock |
| 76 | + assert_eq!( |
| 77 | + diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2), |
| 78 | + Ok(true) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn test_already_locked() { |
| 84 | + const KEY: i64 = -2; |
| 85 | + let conn1 = pg_connection(); |
| 86 | + let conn2 = pg_connection(); |
| 87 | + |
| 88 | + // Another connection obtains the lock first |
| 89 | + assert_eq!( |
| 90 | + diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2), |
| 91 | + Ok(true) |
| 92 | + ); |
| 93 | + |
| 94 | + let mut callback_run = false; |
| 95 | + let result = with_advisory_lock(&conn1, KEY, |_| { |
| 96 | + callback_run = true; |
| 97 | + Ok(()) |
| 98 | + }); |
| 99 | + assert!(dbg!(result).is_err()); |
| 100 | + assert!(!callback_run); |
| 101 | + } |
| 102 | +} |
0 commit comments