Class: Focuslight::Worker

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/focuslight/worker.rb

Constant Summary collapse

DEFAULT_RRD_UPDATE_WORKER_INTERVAL =
300
WORKER_TARGET_VALUES =
[:normal, :short]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, #logger, #logger=

Constructor Details

#initialize(opts) ⇒ Worker

Returns a new instance of Worker.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/focuslight/worker.rb', line 19

def initialize(opts)
  @interval = opts[:interval] || DEFAULT_RRD_UPDATE_INTERVAL
  @target = opts[:target] || :normal
  raise ArgumentError, "invalid worker target #{@target}" unless WORKER_TARGET_VALUES.include?(@target)

  @signals = []
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



13
14
15
# File 'lib/focuslight/worker.rb', line 13

def interval
  @interval
end

Class Method Details

.run(opts) ⇒ Object



15
16
17
# File 'lib/focuslight/worker.rb', line 15

def self.run(opts)
  Focuslight::Worker.new(opts).run
end

Instance Method Details

#dataObject



27
28
29
# File 'lib/focuslight/worker.rb', line 27

def data
  @data ||= Focuslight::Data.new
end

#rrdObject



31
32
33
# File 'lib/focuslight/worker.rb', line 31

def rrd
  @rrd ||= Focuslight::RRD.new
end

#runObject



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/focuslight/worker.rb', line 40

def run
  Signal.trap(:INT){  @signals << :INT }
  Signal.trap(:HUP){  @signals << :HUP }
  Signal.trap(:TERM){ @signals << :TERM }
  Signal.trap(:PIPE, "IGNORE")

  update_next!
  logger.info("[#{@target}] first updater start in #{@next_time}")

  childpid = nil
  while sleep(0.5) do
    if childpid
      begin
        if Process.waitpid(childpid, Process::WNOHANG)
          #TODO: $? (Process::Status object)
          logger.debug("[#{@target}] update finished pid: #{childpid}, code: #{$? >> 8}")
          logger.debug("[#{@target}] next rader start in #{@next_time}")
          childpid = nil
        end
      rescue Errno::ECHILD
        logger.warn("[#{@target}] no child process");
        childpid = nil
      end
    end

    unless @signals.empty?
      logger.warn("[#{@target}] signals_received: #{@signals.join(',')}")
      break
    end

    next if Time.now < @next_time
    update_next!
    logger.debug("[#{@target}] (#{@next_time}) updater start")

    if childpid
      logger.warn("[#{@target}] Previous radar exists, skipping this time")
      next
    end

    childpid = fork do
      graphs = data().get_all_graph_all()
      graphs.each do |graph|
        logger.debug("[#{@target}] update #{graph.id}")
        rrd().update(graph, @target)
      end
    end
  end

  if childpid
    logger.warn("[#{@target}] waiting for updater process finishing")
    begin
      waitpid childpid
    rescue Errno::ECHILD
      # ignore
    end
  end
end

#update_next!Object



35
36
37
38
# File 'lib/focuslight/worker.rb', line 35

def update_next!
  now = Time.now
  @next_time = now - ( now.to_i % @interval ) + @interval
end