-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent multiple instances of schedulers from being running simultane…
…ously It was possible to get into a situation, where multiple instances were running simultaneously, because of cron schedules and previous instances had not finished their work. That situation could lead to database overload and resource exhaustion.
- Loading branch information
Showing
6 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require "zlib" | ||
|
||
module OnlineMigrations | ||
# @private | ||
class AdvisoryLock | ||
attr_reader :name, :connection | ||
|
||
def initialize(name:, connection: ApplicationRecord.connection) | ||
@name = name | ||
@connection = connection | ||
end | ||
|
||
def try_lock | ||
locked = connection.select_value("SELECT pg_try_advisory_lock(#{lock_key})") | ||
Utils.to_bool(locked) | ||
end | ||
|
||
def unlock | ||
connection.select_value("SELECT pg_advisory_unlock(#{lock_key})") | ||
end | ||
|
||
# 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 | ||
end | ||
|
||
def active? | ||
objid = lock_key & 0xffffffff | ||
classid = lock_key >> 32 | ||
|
||
active = connection.select_value(<<~SQL) | ||
SELECT granted | ||
FROM pg_locks | ||
WHERE locktype = 'advisory' | ||
AND pid = pg_backend_pid() | ||
AND mode = 'ExclusiveLock' | ||
AND classid = #{classid} | ||
AND objid = #{objid} | ||
SQL | ||
|
||
Utils.to_bool(active) | ||
end | ||
|
||
private | ||
SALT = 936723412 | ||
|
||
def lock_key | ||
name_hash = Zlib.crc32(name) | ||
SALT * name_hash | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class AdvisoryLockTest < Minitest::Test | ||
def setup | ||
@lock = OnlineMigrations::AdvisoryLock.new(name: "somename") | ||
end | ||
|
||
def teardown | ||
@lock.unlock if @lock.active? | ||
end | ||
|
||
def test_try_lock | ||
locked = @lock.try_lock | ||
assert locked | ||
assert @lock.active? | ||
end | ||
|
||
def test_unlock | ||
@lock.try_lock | ||
assert @lock.active? | ||
@lock.unlock | ||
assert_not @lock.active? | ||
end | ||
|
||
def test_try_with_lock | ||
assert_not @lock.active? | ||
|
||
@lock.try_with_lock do | ||
assert @lock.active? | ||
end | ||
|
||
assert_not @lock.active? | ||
end | ||
end |