summaryrefslogtreecommitdiff
path: root/lib/oxidized/nodes.rb
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2014-02-23 19:11:29 +0200
committerSaku Ytti <saku@ytti.fi>2014-02-23 19:11:29 +0200
commit8880188001da23a5f0960e3970c4eb9bbd448306 (patch)
tree41a1f79450ff1683a2d5aa1f024a564e84230e25 /lib/oxidized/nodes.rb
parent06e5f68db6cfcbd80295874db1f00a25e8ba1229 (diff)
Migrate to sinatra/puma from webrick
As I can't do IO#select on sinatra/puma to run it when I have time, I have to run it on separate thread. This means Nodes container needs to be thread safe, it now has ghetto mutex locking, but I probably need to be be more focused what are the external methods that can be called and wrap those in @mutex.synchronize Provide also HTML UI not just JSON for ghetto UI to people who don't want to integrate
Diffstat (limited to 'lib/oxidized/nodes.rb')
-rw-r--r--lib/oxidized/nodes.rb32
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb
index affeea8..981e082 100644
--- a/lib/oxidized/nodes.rb
+++ b/lib/oxidized/nodes.rb
@@ -7,36 +7,43 @@ module Oxidized
alias :put :unshift
def initialize *args
super
+ @mutex= Mutex.new # we compete for the nodes with webapi thread
load if args.empty?
end
def load
+ lock
new = []
@source = CFG.source[:default]
Oxidized.mgr.source = @source
Oxidized.mgr.source[@source].new.load.each do |node|
new.push Node.new node
end
- replace new
+ unlock(replace new)
end
def list
- map { |e| e.name }
+ lock
+ unlock(map { |e| e.serialize })
end
def show node
+ lock
i = find_node_index node
- self[i].serialize
+ unlock(self[i].serialize)
end
def fetch node, group
+ lock
i = find_node_index node
output = self[i].output.new
+ unlock
raise Oxidized::NotSupported unless output.respond_to? :fetch
output.fetch node, group
end
def del node
- delete_at find_node_index
+ lock
+ unlock(delete_at find_node_index(node))
end
# @param node [String] name of the node moved into the head of array
def next node, opt={}
- require 'pp'
+ lock
n = del node
if n
n.user = opt['user']
@@ -44,21 +51,32 @@ module Oxidized
n.from = opt['from']
put n
end
+ unlock
end
alias :top :next
# @return [String] node from the head of the array
def get
- (self << shift).last
+ lock
+ unlock((self << shift).last)
end
private
+ def lock
+ @mutex.lock unless @mutex.owned?
+ end
+
+ def unlock arg=nil
+ @mutex.unlock if @mutex.owned?
+ arg
+ end
+
def find_index node
index { |e| e.name == node }
end
def find_node_index node
- find_index node or raise Oxidized::NodeNotFound
+ find_index node or raise Oxidized::NodeNotFound, "unable to find '#{node}'"
end
end
end