Class: Patriot::Worker::InfoServer

Inherits:
Object
  • Object
show all
Includes:
Util::Config, Util::Logger
Defined in:
lib/patriot/worker/info_server.rb

Overview

info server (web management console and for monitoring)

Constant Summary collapse

PORT_KEY =

configuratio key for port used by this server

'info_server.port'
DEFAULT_PORT =

default port number

'36104'
RACK_HANDLER_KEY =

configuration key for rack handler used to start this server

'info_server.rack.handler'
DEFAULT_RACK_HANDLER =

default rack handler DEFAULT_RACK_HANDLER = ‘Rack::Handler::WEBrick’

'Rack::Handler::Thin'

Constants included from Util::Config

Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Logger

#create_logger

Methods included from Util::Config

#load_config, #load_plugins

Constructor Details

#initialize(worker, config) ⇒ InfoServer

Returns a new instance of InfoServer.

Parameters:

  • worker (Patriot::Worker::Base)

    worker managed through this server

  • config (Patriot::Util::Config::Bae)


27
28
29
30
31
32
33
# File 'lib/patriot/worker/info_server.rb', line 27

def initialize(worker, config)
  @logger = create_logger(config)
  @worker = worker
  @config = config
  @port = @config.get(Patriot::Worker::InfoServer::PORT_KEY,
                      Patriot::Worker::InfoServer::DEFAULT_PORT)
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



23
24
25
# File 'lib/patriot/worker/info_server.rb', line 23

def port
  @port
end

Instance Method Details

#get_url_mapHash<String, Sinatra::Base>

Returns:

  • (Hash<String, Sinatra::Base>)


72
73
74
75
76
77
78
# File 'lib/patriot/worker/info_server.rb', line 72

def get_url_map
  urlmap = {"/" => Patriot::Worker::Servlet::IndexServlet,
            "/api/v1/jobs" => Patriot::Worker::Servlet::JobAPIServlet,
            "/api/v1/workers" => Patriot::Worker::Servlet::WorkerAPIServlet}
  urlmap.values.each{|servlet| servlet.configure(@worker, @config)}
  return urlmap
end

#shutdown_serverObject

instruct to shutdown server



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/patriot/worker/info_server.rb', line 87

def shutdown_server
  return false if @server.nil?
  unless @server_thread.nil?
    begin
      @handler.shutdown
      @logger.info "info server shutdowned"
    rescue => e
      @logger.error "failed to shutdown infoserver", e
      raise e
    end
  end
end

#start_serverObject

start the server



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
# File 'lib/patriot/worker/info_server.rb', line 36

def start_server
  if @port.nil?
    @logger.info("port is not set. starting info server is skipped")
    return
  end
  @server_thread = Thread.new do
    begin
      @handler = eval(@config.get(RACK_HANDLER_KEY, DEFAULT_RACK_HANDLER))
      app = Rack::URLMap.new(get_url_map)
      app = Rack::CommonLogger.new(app, build_access_logger)
      app = Rack::Rewrite.new(app){
        # below 4 rules are for backward compatibility
        r301 %r{^/jobs/?$}, "/"
        rewrite %r{^/jobs/([^/]+)$}, "/api/v1/jobs/$1"
        rewrite "/worker", "/api/v1/workers/this/state"
        rewrite "/worker/status", "/api/v1/workers/this/state"
        # for web console
        rewrite %r{^(?!/api)}, "/"
      }
      app = Rack::Static.new(app, :urls => ["/js", "/css"], :root => File.join($home, "public"))
      # TODO set options based on Handler type
      @handler.run(app, {:Port => @port, :Host => '0.0.0.0', :signals => false}) do |server|
        server.threaded = true
        server.threadpool_size = 5
        server.timeout = 60
      end
    rescue => e
      @logger.error e
      raise e
    end
  end
  @logger.info "info server has started with #{@handler.class}"
  return true
end