Class: SandthornSequelProjection::Lock
- Inherits:
-
Object
- Object
- SandthornSequelProjection::Lock
- 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
-
#db_connection ⇒ Object
readonly
Returns the value of attribute db_connection.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Instance Method Summary collapse
- #acquire ⇒ Object
- #attempt_lock ⇒ Object
- #db_row ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(identifier, db_connection = nil, table_name = nil) ⇒ Lock
constructor
A new instance of Lock.
- #lock ⇒ Object
- #lock_column_name ⇒ Object
- #locked? ⇒ Boolean
- #locked_at ⇒ Object
- #release ⇒ Object
- #set_lock(value) ⇒ Object
- #table ⇒ Object
- #timeout ⇒ Object
- #unlocked? ⇒ Boolean
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_connection ⇒ Object (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 |
#identifier ⇒ Object (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
#acquire ⇒ Object
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_lock ⇒ Object
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_row ⇒ Object
65 66 67 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 65 def db_row table.where(identifier: @identifier) end |
#expired? ⇒ Boolean
27 28 29 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 27 def expired? locked_at && (Time.now - locked_at > timeout) end |
#lock ⇒ Object
61 62 63 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 61 def lock set_lock(Time.now) end |
#lock_column_name ⇒ Object
57 58 59 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 57 def lock_column_name DEFAULT_LOCK_COLUMN end |
#locked? ⇒ Boolean
19 20 21 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 19 def locked? !unlocked? end |
#locked_at ⇒ Object
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 |
#release ⇒ Object
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 |
#table ⇒ Object
69 70 71 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 69 def table db_connection[@table_name] end |
#timeout ⇒ Object
31 32 33 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 31 def timeout DEFAULT_TIMEOUT end |
#unlocked? ⇒ Boolean
23 24 25 |
# File 'lib/sandthorn_sequel_projection/lock.rb', line 23 def unlocked? locked_at.nil? end |