Class: Grotesque::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/grotesque/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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={})
  @options   = options.clone
  @port      = options.delete(:port)      || 9090
  @host      = options.delete(:host)      || "0.0.0.0"
  @zk_port   = options.delete(:zk_port)   || 2181
  @zk_host   = options.delete(:zk_host)   || "localhost"
  @zk_chroot = options.delete(:zk_chroot) || "grotesque"
  @zk        = ZK.new("#{@zk_host}:#{@zk_port}/#{@zk_chroot}")
end

Instance Attribute Details

#zkObject (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(options)
  new(options).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

#flushallObject



71
72
73
# File 'lib/grotesque/server.rb', line 71

def flushall
  flushdb
end

#flushdbObject



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

#runObject



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