Class: Gom::Remote::Daemon

Inherits:
Object
  • Object
show all
Includes:
OAttr, Timeout
Defined in:
lib/gom/remote/daemon.rb

Constant Summary collapse

Defaults =
{ 
  :actor_dt       => 60,
  :sensor_dt      => 1,
  :callback_port  => 8815,
  :stealth        => false,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url, options = {}, &blk) ⇒ Daemon

Returns a new instance of Daemon.



21
22
23
24
25
26
# File 'lib/gom/remote/daemon.rb', line 21

def initialize service_url, options = {}, &blk
  @options = (Defaults.merge options)
  callback_port = @options[:callback_port]
  @gom, @service_path = (Connection.init service_url, callback_port)
  (blk.call self, @service_path) unless blk.nil?
end

Instance Attribute Details

#service_pathObject (readonly)

service_path is derived from the service_url (server part stripped)



19
20
21
# File 'lib/gom/remote/daemon.rb', line 19

def service_path
  @service_path
end

Instance Method Details

#actor_loop(interval = actor_dt, &tic) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/gom/remote/daemon.rb', line 46

def actor_loop interval = actor_dt, &tic
  puts " -- running gom-script actor loop.."
  forever(interval) do 
    @gom.refresh
    tic && (tic.call self) || :continue
  end
end

#background_loop(intervall = 0, &callback) ⇒ Object



54
55
56
57
58
# File 'lib/gom/remote/daemon.rb', line 54

def background_loop intervall = 0, &callback
  Thread.new do
    forever intervall, &callback
  end
end

#check_inObject

push own ip and monitoring port back to GOM node



74
75
76
77
78
79
80
81
# File 'lib/gom/remote/daemon.rb', line 74

def check_in
  if stealth
    puts " -- no GOM check-in in stealth mode"
  else
    @gom.write "#{service_path}:daemon_ip", @gom.callback_server.host
    #@gom.write "#{service_path}:nagios_port", nagios_port
  end
end

#forever(interval = 1, &callback) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gom/remote/daemon.rb', line 60

def forever interval = 1, &callback 
  loop do
    begin
      rc = callback.call
    rescue Exception => e
      puts " ## <#{e.class}> #{self} - #{e}\n -> #{e.backtrace.join "\n    "}"
    ensure
      break if rc == :stop
      sleep interval
    end
  end
end

#redirect_log(filepath) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gom/remote/daemon.rb', line 83

def redirect_log filepath
  (@logfile_fd && @logfile_fd.close) rescue nil
  puts " -- redirecting stdout/stderr to: #{filepath}"
  if filepath == '-'
    # when @stdout is not defined/nil, we leave the file descriptors as
    # they are 
    if @stdout 
      $stdout, $stderr = @stdout, @stderr
    end
  else
    @stdout, @stderr = $stdout, $stderr
    @logfile_fd = File.open(filepath, File::WRONLY|File::APPEND|File::CREAT)
    @logfile_fd.sync = true
    $stderr = $stdout = @logfile_fd
  end
  # first line after redirect
  puts " -- daemon logile redirect at #{Time.now}"
end

#sensor_loop(interval = sensor_dt, &tic) ⇒ Object

def open_nagios_port jjjjj

  @gom.callback_server.mount(%r{^/nagios}, lambda do |*args|
    uri, env = *args
    req = Rack::Request.new(env)
    envmap = (env.map { |k,v| "#{k}: #{v}" }.join "\n")
    body = ["OK -- #{env['REQUEST_URI']}\n---\n#{envmap}"]
    [200, {"Content-Type"=>"text/plain"}, body]
  end)
end
    hs = HttpServer.new o 
    hs.mount "^/gnp;", lambda {|*args| gnp_handler *args}


41
42
43
44
# File 'lib/gom/remote/daemon.rb', line 41

def sensor_loop interval = sensor_dt, &tic
  puts " -- running gom-script sensor loop.."
  forever(interval) { tic.call self }
end