Module: Ocular::DSL::Etcd
- Included in:
- RunContext, Event::DefinitionProxy
- Defined in:
- lib/ocular/dsl/etcd.rb
Constant Summary collapse
- @@__etcd_instance =
nil
Instance Method Summary collapse
Instance Method Details
#etcd ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ocular/dsl/etcd.rb', line 12 def etcd() if @@__etcd_instance return @@__etcd_instance end datasources = ::Ocular::Settings::get(:datasources) if !datasources raise "No etcd client settings" end settings = datasources[:etcd] || {} @@__etcd_instance = ::Etcd.client( host: (settings[:host] || "localhost"), port: (settings[:port] || 2379), user_name: (settings[:user_name] || nil), password: (settings[:password] || nil), ) return @@__etcd_instance end |
#locked?(key) ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ocular/dsl/etcd.rb', line 81 def locked?(key) client = etcd() begin ret = client.get("/ocular/locks/#{key}") if ret.node and ret.node.expiration == nil warn("Key #{key} has been locked permanently with value '#{ret.node.value}'!") end return ret.node.value # Key is locked rescue ::Etcd::KeyNotFound => e return nil end end |
#ttl_lock(key, ttl: 10) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ocular/dsl/etcd.rb', line 33 def ttl_lock(key, ttl:10) id = @run_id if !id id = Process.pid.to_s Socket.ip_address_list.each do |addr| next if addr.ip_address == "127.0.0.1" next if addr.ip_address.start_with?("::1") next if addr.ip_address.start_with?("fe80::1") id += "-" + addr.ip_address end end client = etcd() current_lock = locked?(key) if current_lock != id and current_lock != nil return nil # Somebody else has lock, can't lock by ourself end if current_lock == id and current_lock != nil begin client.test_and_set("/ocular/locks/#{key}", value: id, prevValue: id, ttl: ttl) return id rescue ::Etcd::NodeExist => e return nil end else begin client.create("/ocular/locks/#{key}", value: id, ttl: ttl) return id rescue ::Etcd::NodeExist => e return nil end end end |
#unlock(key) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/ocular/dsl/etcd.rb', line 71 def unlock(key) client = etcd() begin client.delete("/ocular/locks/#{key}") rescue ::Etcd::KeyNotFound end return true end |