Class: Grotesque::Server
- Inherits:
-
Object
- Object
- Grotesque::Server
- Defined in:
- lib/grotesque/server.rb
Instance Attribute Summary collapse
-
#zk ⇒ Object
readonly
Returns the value of attribute zk.
Class Method Summary collapse
Instance Method Summary collapse
- #del(*keys) ⇒ Object
- #flushall ⇒ Object
- #flushdb ⇒ Object
- #get(key) ⇒ Object
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
13 14 15 16 17 18 19 20 21 |
# File 'lib/grotesque/server.rb', line 13 def initialize(={}) @options = .clone @port = .delete(:port) || 9090 @host = .delete(:host) || "0.0.0.0" @zk_port = .delete(:zk_port) || 2181 @zk_host = .delete(:zk_host) || "localhost" @zk_chroot = .delete(:zk_chroot) || "grotesque" @zk = ZK.new("#{@zk_host}:#{@zk_port}/#{@zk_chroot}") end |
Instance Attribute Details
#zk ⇒ Object (readonly)
Returns the value of attribute zk.
7 8 9 |
# File 'lib/grotesque/server.rb', line 7 def zk @zk end |
Class Method Details
.run(options) ⇒ Object
9 10 11 |
# File 'lib/grotesque/server.rb', line 9 def self.run() new().run end |
Instance Method Details
#del(*keys) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/grotesque/server.rb', line 53 def del(*keys) deleted = 0 keys.each do |key| key = normalize_key(key) @zk.with_lock(key, :mode => :exclusive) do begin @zk.delete(key) deleted += 1 rescue ZK::Exceptions::NoNode end end end deleted end |
#flushall ⇒ Object
71 72 73 |
# File 'lib/grotesque/server.rb', line 71 def flushall flushdb end |
#flushdb ⇒ Object
75 76 77 78 79 |
# File 'lib/grotesque/server.rb', line 75 def flushdb @zk.rm_rf("/") @zk.create("/") true end |
#get(key) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/grotesque/server.rb', line 29 def get(key) key = normalize_key(key) value = nil @zk.with_lock(key, :mode => :shared) do begin value, _ = @zk.get(key) rescue ZK::Exceptions::NoNode end end value end |
#run ⇒ Object
23 24 25 26 27 |
# File 'lib/grotesque/server.rb', line 23 def run server = MessagePack::RPC::Server.new server.listen(@host, @port, self, ["get", "set"]) server.run end |
#set(key, value) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/grotesque/server.rb', line 43 def set(key, value) key = normalize_key(key) @zk.with_lock(key, :mode => :exclusive) do @zk.create(key, value, :or => :set) end true end |