Class: CassandraLock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_lock/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contact_points, keyspace, id = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
# File 'lib/cassandra_lock/client.rb', line 8

def initialize(contact_points, keyspace, id=nil)
  cluster = Cassandra.cluster hosts: contact_points
  @cassandra = cluster.connect keyspace
  @id = id || SecureRandom.uuid
  @LOCK = @cassandra.prepare "INSERT INTO leases (name, owner) VALUES (?,?) IF NOT EXISTS"
  @KEEPALIVE = @cassandra.prepare "UPDATE leases set owner = ? where name = ? IF owner = ?"
  @UNLOCK = @cassandra.prepare "DELETE FROM leases where name = ? IF owner = ?"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/cassandra_lock/client.rb', line 6

def id
  @id
end

Instance Method Details

#keep_alive(resource) ⇒ Object



22
23
24
# File 'lib/cassandra_lock/client.rb', line 22

def keep_alive(resource)
  @cassandra.execute(@KEEPALIVE, @id, resource, @id)
end

#lock(resource) ⇒ Object



17
18
19
20
# File 'lib/cassandra_lock/client.rb', line 17

def lock(resource)
  result = @cassandra.execute(@LOCK, resource, @id)
  result.first["[applied]"]
end

#unlock(resource) ⇒ Object



26
27
28
# File 'lib/cassandra_lock/client.rb', line 26

def unlock(resource)
  @cassandra.execute(@UNLOCK, resource, @id)
end