Skip to content

Commit

Permalink
Make advisory lock more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 16, 2025
1 parent d598591 commit a1aaf37
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/online_migrations/advisory_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ def unlock

# Runs the given block if an advisory lock is able to be acquired.
def try_with_lock
if try_lock
begin
yield
ensure
unlock
end
end
locked = try_lock
yield if locked
ensure
unlock if locked
end

def active?
Expand Down
3 changes: 3 additions & 0 deletions test/advisory_lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ def test_unlock
def test_try_with_lock
assert_not @lock.active?

called = false
@lock.try_with_lock do
called = true
assert @lock.active?
end
assert called

assert_not @lock.active?
end
Expand Down

0 comments on commit a1aaf37

Please sign in to comment.