Class: Diplomat::Lock

Inherits:
RestClient show all
Defined in:
lib/diplomat/lock.rb

Overview

Methods for interacting with the Consul lock API endpoint

Instance Method Summary collapse

Methods inherited from RestClient

access_method?, #concat_url, #configuration, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter

Constructor Details

This class inherits a constructor from Diplomat::RestClient

Instance Method Details

#acquire(key, session, value = nil, options = {}) ⇒ Boolean

Acquire a lock

Parameters:

  • key (String)

    the key

  • session (String)

    the session, generated from Diplomat::Session.create

  • value (String) (defaults to: nil)

    the value for the key

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)

    If the lock was acquired


14
15
16
17
18
19
20
21
22
23
# File 'lib/diplomat/lock.rb', line 14

def acquire(key, session, value = nil, options = {})
  key = normalize_key_for_uri(key)
  custom_params = []
  custom_params << use_named_parameter('acquire', session)
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('flags', options[:flags]) if options && options[:flags]
  data = value unless value.nil?
  raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, data, custom_params)
  raw.body.chomp == 'true'
end

#release(key, session, options = {}) ⇒ nil

Release a lock rubocop:disable Metrics/AbcSize

Parameters:

  • key (String)

    the key

  • session (String)

    the session, generated from Diplomat::Session.create

  • options (Hash) (defaults to: {})

    :dc string for dc specific query

Returns:

  • (nil)

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

def release(key, session, options = {})
  key = normalize_key_for_uri(key)
  custom_params = []
  custom_params << use_named_parameter('release', session)
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('flags', options[:flags]) if options && options[:flags]
  raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, nil, custom_params)
  raw.body
end

#wait_to_acquire(key, session, value = nil, check_interval = 10, options = {}) ⇒ Boolean

wait to aquire a lock

Parameters:

  • key (String)

    the key

  • session (String)

    the session, generated from Diplomat::Session.create

  • value (String) (defaults to: nil)

    the value for the key

  • check_interval (Integer) (defaults to: 10)

    number of seconds to wait between retries

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)

    If the lock was acquired


32
33
34
35
36
37
38
39
# File 'lib/diplomat/lock.rb', line 32

def wait_to_acquire(key, session, value = nil, check_interval = 10, options = {})
  acquired = false
  until acquired
    acquired = acquire(key, session, value, options)
    sleep(check_interval) unless acquired
    return true if acquired
  end
end