class RSence::HTTPDaemon

@private Simple process control, constructed here and called from Daemon::Controller

Public Instance Methods

addr() click to toggle source

Returns the configured bind address

# File lib/rsence/daemon.rb, line 346
def addr
  RSence.config[:http_server][:bind_address]
end
alrm() click to toggle source

Called on ALRM signals (save data, reload all plugins manually)

# File lib/rsence/daemon.rb, line 408
def alrm
  save
  @transporter.plugins.shutdown
  @transporter.plugins.init_bundles!
end
autosave_loop() click to toggle source

Saves plugin and session state periodically

# File lib/rsence/daemon.rb, line 356
def autosave_loop
  Thread.new do
    Thread.pass
    sleep RSence.config[:daemon][:autosave_interval]
    while true
      if @transporter.online?
        save
      end
      sleep RSence.config[:daemon][:autosave_interval]
    end
  end
end
daemonize!() click to toggle source

Main entry point, daemonizes itself using Controller.

# File lib/rsence/daemon.rb, line 437
def daemonize!
  Daemon.daemonize( self )
end
info() click to toggle source

Called on INFO (PWR) signals (“Alive?”)

# File lib/rsence/daemon.rb, line 403
def info
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -- RSence version #{RSence.version} is running the project: #{RSence.args[:env_path]}"
end
log_fn() click to toggle source

Returns the log path.

# File lib/rsence/daemon.rb, line 341
def log_fn
  RSence.config[:daemon][:log_fn]
end
pid_fn() click to toggle source

Returns the pid file path.

# File lib/rsence/daemon.rb, line 336
def pid_fn
  RSence.config[:daemon][:pid_fn]
end
port() click to toggle source

Returns the configured port.

# File lib/rsence/daemon.rb, line 351
def port
  RSence.config[:http_server][:port]
end
ps_name() click to toggle source
# File lib/rsence/daemon.rb, line 272
def ps_name
  config = RSence.config
  url = "http://#{config[:http_server][:bind_address]}:#{config[:http_server][:port]}#{config[:base_url]}"
  env_path = RSence.args[:env_path]
  "RSence-#{RSence.version} on #{url} in #{env_path}"
end
run() click to toggle source

RSence top-level run handler. Almost identical to start.

# File lib/rsence/daemon.rb, line 314
def run
  
  # Sets the process name
  $0 = ps_name
  
  puts "Starting as a foreground process." if RSence.args[:verbose]
  puts "Press CTRL-C to terminate."

  @transporter = Transporter.new

  conf = RSence.config[:http_server]

  unless RSence.args[:log_fg]
    Daemon.start_logging( self )
  end
  
  autosave_loop if RSence.config[:daemon][:autosave_interval] > 0
  start_broker( conf )
  
end
save() click to toggle source

Save state

# File lib/rsence/daemon.rb, line 426
def save
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -- Saving state..."
  # transporter_state = @transporter.online?
  # @transporter.online = false
  @transporter.sessions.store_sessions
  @transporter.plugins.delegate(:flush)
  # @transporter.online = transporter_state
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -- State saved."
end
start() { |broker| ... } click to toggle source

Called by Controller#start, contains RSence-specific operations

# File lib/rsence/daemon.rb, line 370
def start
  
  # Sets the process name
  $0 = ps_name
  
  @transporter = Transporter.new
  
  conf = RSence.config[:http_server]
  
  unless RSence.args[:log_fg]
    Daemon.start_logging( self )
    STDIN.reopen( "/dev/null" )
  end
  
  Process.setsid
  
  autosave_loop if RSence.config[:daemon][:autosave_interval] > 0
  start_broker( conf )
  yield @broker

end
start_broker( conf ) click to toggle source
# File lib/rsence/daemon.rb, line 279
def start_broker( conf )
  http_delayed_seconds = RSence.config[:daemon][:http_delayed_start].to_i
  if http_delayed_seconds == -1
    puts "The HTTP Broker is disabled. RSence won't bind a http listener to any address."
  else
    if http_delayed_seconds > 0
      puts "Delaying the start of the HTTP Broker by #{http_delayed_seconds} seconds."
      if http_delayed_seconds > 10
        # report when starting in 10 second intervals
        sleep_remainder = http_delayed_seconds % 10
        sleep_count = http_delayed_seconds / 10
        sleep_time_left = http_delayed_seconds
        sleep_count.times do |sleep_count_num|
          puts "Waiting #{sleep_time_left} seconds..."
          sleep 10
          sleep_time_left -= 10
        end
        if sleep_remainder != 0
          puts "Waiting #{sleep_remainder} seconds..."
          sleep sleep_remainder
        end
      else
        sleep http_delayed_seconds
      end
      puts "Starting the HTTP Broker now."
    end
    # This is the main http handler instance:
    @broker = Broker.start(
      @transporter,
      conf
    )
  end
end
stop() click to toggle source

Called by Controller#stop, contains RSence-specific operations

# File lib/rsence/daemon.rb, line 393
def stop
  @transporter.shutdown
end
usr1() click to toggle source

Called on usr1 signals (updates bundles manually, like the regular intervals of the -a switch and forces client to be rebuilt)

# File lib/rsence/daemon.rb, line 415
def usr1
  @transporter.plugins.update_bundles!
  # @transporter.plugins.client_pkg.rebuild_client
end
usr2() click to toggle source

Called on USR2 signals

# File lib/rsence/daemon.rb, line 421
def usr2
  save
end