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 339
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 417
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 364
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 451
def daemonize!
  Daemon.daemonize( self )
end
info() click to toggle source

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

# File lib/rsence/daemon.rb, line 412
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 334
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 329
def pid_fn
  RSence.config[:daemon][:pid_fn]
end
port() click to toggle source

Returns the configured port.

# File lib/rsence/daemon.rb, line 344
def port
  RSence.config[:http_server][:port]
end
ps_name() click to toggle source
# File lib/rsence/daemon.rb, line 264
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 306
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
  
  ses_expire_loop
  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 435
def save
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -- Saving state..."
  # transporter_state = @transporter.online?
  # @transporter.online = false
  begin
    # Store remaining active sessions
    @transporter.sessions.store_sessions
  rescue => e
    puts "Exception #{e.inspect} occurred while storing sessions"
  end
  @transporter.plugins.delegate(:flush)
  # @transporter.online = transporter_state
  puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} -- State saved."
end
ses_expire_loop() click to toggle source

Expires old sessions once a second

# File lib/rsence/daemon.rb, line 349
def ses_expire_loop
  Thread.new do
    Thread.pass
    while true
      sleep 1
      begin
        @transporter.sessions.expire_sessions if @transporter.online?
      rescue => e
        warn "Session expiration error: #{e.inspect}"
      end
    end
  end
end
start() { |broker| ... } click to toggle source

Called by Controller#start, contains RSence-specific operations

# File lib/rsence/daemon.rb, line 378
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
  
  ses_expire_loop
  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 271
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 402
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 424
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 430
def usr2
  save
end