6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/railbox/workers/base_worker.rb', line 6
def with_lock
lock_id = Zlib.crc32(self.class.name, 0)
lock_sql = "SELECT pg_try_advisory_lock(#{lock_id}) AS locked"
result = ActiveRecord::Base.connection.execute(lock_sql)
locked = result.first['locked'] unless result.nil?
unless locked
Rails.logger.info "Another #{self.class.name} work now."
return
end
yield
ensure
ActiveRecord::Base.connection.execute("SELECT pg_advisory_unlock(#{lock_id})")
end
|