Class: Boxxspring::Synchronization::Orchestrator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/boxxspring/synchronization/orchestrator.rb

Instance Method Summary collapse

Constructor Details

#initializeOrchestrator

Returns a new instance of Orchestrator.



10
11
12
13
14
15
16
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 10

def initialize 
  @provider = ::Redis.new(
    url: Synchronization.configuration.url,
    timeout: 10.0
  )
  @operations = {}
end

Instance Method Details

#lock(key, signature, options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 18

def lock( key, signature, options = {} )
  ttl = options[ :ttl ];
  ttl = ttl * 1000 if ttl.is_a? ActiveSupport::Duration
  self.execute_operation( :lock, [ key ], [ signature, ttl ] ) ?
    true : false
end

#read(key) ⇒ Object



30
31
32
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 30

def read( key )
  @provider.get( key )
end

#unlock(key, signature) ⇒ Object



25
26
27
28
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 25

def unlock( key, signature )
  self.execute_operation( :unlock, [ key ], [ signature ] ) ?
    true : false
end

#write(key, value, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 34

def write( key, value, options = {} )
  ttl = options[ :ttl ]
  if ttl 
    ttl = ttl.to_i if ttl.is_a? ActiveSupport::Duration
    ( @provider.set( key, value ) == "OK" ) && 
    ( @provider.expire( key, ttl ) ) ? true : false
  else
    ( @provider.set( key, value ) == "OK" ) ? true : false
  end
end

#write_if_condition(key, value, condition) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/boxxspring/synchronization/orchestrator.rb', line 45

def write_if_condition( key, value, condition )
  operation = "write_if_#{condition}"
  operation_sha = @operations[ operation.to_sym ]
  raise 'Synchronization: An unknown condition was requested.' \
    if operation_sha.nil?
  @provider.evalsha( operation_sha, [ key ], [ value ] ) ? true : false
end