summaryrefslogtreecommitdiff
path: root/lib/oxidized/nodes.rb
blob: 981e082e1f83f1e5748b681ac1762a210c69fe59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
70
71
72
73
74
75
76
77
78
79
80
81
82
module Oxidized
  require 'oxidized/node'
 class Oxidized::NotSupported < StandardError; end
 class Oxidized::NodeNotFound < StandardError; end
  class Nodes < Array
    attr_accessor :source
    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
      unlock(replace new)
    end
    def list
      lock
      unlock(map { |e| e.serialize })
    end
    def show node
      lock
      i = find_node_index node
      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
      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={}
      lock
      n = del node
      if n
        n.user = opt['user']
        n.msg  = opt['msg']
        n.from = opt['from']
        put n
      end
      unlock
    end
    alias :top :next
    # @return [String] node from the head of the array
    def get
      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, "unable to find '#{node}'"
    end
  end
end