Sha256: aa89fbd6fe4fc2166c165c2e9a76d3e12b3d337000b7cac5695b1c1929d6cb63

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

require 'sinatra/base'
require 'sinatra/contrib'

module Patriot
  module Worker
    module Servlet
      # excepton thrown when a woker is not accessible
      class WorkerInaccessibleException < Exception; end
      # provide worker management functionalities
      class WorkerAPIServlet < Patriot::Worker::Servlet::APIServletBase
        register Sinatra::Contrib

        HOST_KEY = "host"
        VERSION_KEY = "version"
        CLASS_KEY = "class"
        STARTED_AT_KEY = "started_at"
        CONFIG_KEY = "config"

        STATE_KEY = "state"

        LOCALHOST_EXPS = ["localhost", "127.0.0.1"]

        set :show_exceptions, :after_handler

        get '/' do
          worker_hosts = @@config.get(Patriot::Util::Config::WORKER_HOST_KEY)
          worker_hosts = [worker_hosts] unless worker_hosts.is_a?(Array)
          worker_hosts = @@config.get(Patriot::Util::Config::WORKER_HOST_KEY).map do |h|
            h = h.split(":")
            port = h.size == 2 ? h[1] : Patriot::Worker::InfoServer::DEFAULT_PORT
            {'host' => h[0], 'port' => port}
          end
          return JSON.generate(worker_hosts)
        end

        get '/this' do
          return JSON.generate(
            STATE_KEY => @@worker.status,
            HOST_KEY => @@worker.host,
            VERSION_KEY => Patriot::VERSION,
            CLASS_KEY => @@worker.class.to_s,
            STARTED_AT_KEY => @@worker.started_at,
            CONFIG_KEY => {} # TODO
          )
        end

        get '/this/state' do
          return JSON.generate(
            STATE_KEY => @@worker.status
          )
        end

        put '/this/state' do
          protected!
          new_status = params['status']
          if [Patriot::Worker::Status::ACTIVE, Patriot::Worker::Status::SLEEP ].include?(@@worker.status)
            @@worker.status = new_status
          else
            # state cannot be changed in shutdown process
            halt 403
          end
        end

      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
patriot-workflow-scheduler-0.8.8 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.7 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.6 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.5 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.4 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.3 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.2 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.1 lib/patriot/worker/servlet/worker_api_servlet.rb
patriot-workflow-scheduler-0.8.0 lib/patriot/worker/servlet/worker_api_servlet.rb