Class: SandthornSequelProjection::Lock

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sandthorn_sequel_projection/lock.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

3 minutes

3*60
DEFAULT_LOCK_COLUMN =
:locked_at

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, db_connection = nil, table_name = nil) ⇒ Lock

Returns a new instance of Lock.



13
14
15
16
17
# 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

Instance Attribute Details

#db_connectionObject (readonly)

Returns the value of attribute db_connection.



6
7
8
# File 'lib/sandthorn_sequel_projection/lock.rb', line 6

def db_connection
  @db_connection
end

#identifierObject (readonly)

Returns the value of attribute identifier.



6
7
8
# File 'lib/sandthorn_sequel_projection/lock.rb', line 6

def identifier
  @identifier
end

Instance Method Details

#acquireObject



35
36
37
38
39
40
41
42
43
# File 'lib/sandthorn_sequel_projection/lock.rb', line 35

def acquire
  if attempt_lock
    begin
      yield
    ensure
      release
    end
  end
end

#attempt_lockObject



49
50
51
52
53
54
55
# File 'lib/sandthorn_sequel_projection/lock.rb', line 49

def attempt_lock
  transaction do
    if unlocked? || expired?
      lock
    end
  end
end

#db_rowObject



65
66
67
# File 'lib/sandthorn_sequel_projection/lock.rb', line 65

def db_row
  table.where(identifier: @identifier)
end

#expired?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/sandthorn_sequel_projection/lock.rb', line 27

def expired?
  locked_at && (Time.now - locked_at > timeout)
end

#lockObject



61
62
63
# File 'lib/sandthorn_sequel_projection/lock.rb', line 61

def lock
  set_lock(Time.now)
end

#lock_column_nameObject



57
58
59
# File 'lib/sandthorn_sequel_projection/lock.rb', line 57

def lock_column_name
  DEFAULT_LOCK_COLUMN
end

#locked?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/sandthorn_sequel_projection/lock.rb', line 19

def locked?
  !unlocked?
end

#locked_atObject



77
78
79
80
81
# File 'lib/sandthorn_sequel_projection/lock.rb', line 77

def locked_at
  if row = db_row.first
    row[lock_column_name]
  end
end

#releaseObject



45
46
47
# File 'lib/sandthorn_sequel_projection/lock.rb', line 45

def release
  set_lock(nil)
end

#set_lock(value) ⇒ Object



73
74
75
# File 'lib/sandthorn_sequel_projection/lock.rb', line 73

def set_lock(value)
  db_row.update(lock_column_name => value)
end

#tableObject



69
70
71
# File 'lib/sandthorn_sequel_projection/lock.rb', line 69

def table
  db_connection[@table_name]
end

#timeoutObject



31
32
33
# File 'lib/sandthorn_sequel_projection/lock.rb', line 31

def timeout
  DEFAULT_TIMEOUT
end

#unlocked?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/sandthorn_sequel_projection/lock.rb', line 23

def unlocked?
  locked_at.nil?
end