class SandthornSequelProjection::Lock

Constants

DEFAULT_LOCK_COLUMN
DEFAULT_TIMEOUT

Attributes

db_connection[R]
identifier[R]

Public Class Methods

new(identifier, db_connection = nil, table_name = nil) click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 13
def initialize(identifier, db_connection = nil, table_name = nil)
  @identifier     = identifier.to_s
  @db_connection  = db_connection || SandthornDriverSequel.configuration.db_connection
  @table_name     = table_name || ProcessedEventsTracker::DEFAULT_TABLE_NAME
end

Public Instance Methods

acquire() { || ... } click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 35
def acquire
  if attempt_lock
    begin
      yield
    ensure
      release
    end
  end
end
attempt_lock() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 49
def attempt_lock
  transaction do
    if unlocked? || expired?
      lock
    end
  end
end
db_row() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 65
def db_row
  table.where(identifier: @identifier)
end
expired?() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 27
def expired?
  locked_at && (Time.now - locked_at > timeout)
end
lock() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 61
def lock
  set_lock(Time.now)
end
lock_column_name() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 57
def lock_column_name
  DEFAULT_LOCK_COLUMN
end
locked?() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 19
def locked?
  !unlocked?
end
locked_at() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 77
def locked_at
  if row = db_row.first
    row[lock_column_name]
  end
end
release() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 45
def release
  set_lock(nil)
end
set_lock(value) click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 73
def set_lock(value)
  db_row.update(lock_column_name => value)
end
table() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 69
def table
  db_connection[@table_name]
end
timeout() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 31
def timeout
  DEFAULT_TIMEOUT
end
unlocked?() click to toggle source
# File lib/sandthorn_sequel_projection/lock.rb, line 23
def unlocked?
  locked_at.nil?
end