lib/right_chimp/daemon/ChimpDaemon.rb in right_chimp-1.1.3 vs lib/right_chimp/daemon/ChimpDaemon.rb in right_chimp-2.0

- old
+ new

@@ -4,10 +4,11 @@ # Classes for the Chimp Daemon (chimpd) # module Chimp class ChimpDaemon + attr_accessor :verbose, :debug, :port, :concurrency, :delay, :retry_count, :dry_run, :logfile, :chimp_queue attr_reader :queue, :running include Singleton @@ -20,20 +21,24 @@ @retry_count = 0 @threads = [] @running = false @queue = ChimpQueue.instance @chimp_queue = Queue.new + + #Connect to the API + Connection.instance end # # Main entry point for chimpd command line application # def run install_signal_handlers parse_command_line - puts "chimpd #{VERSION} launching with #{@concurrency} workers" + #puts "chimpd #{VERSION} launching with #{@concurrency} workers" + puts "Loading... please wait" spawn_queue_runner spawn_webserver spawn_chimpd_submission_processor run_forever end @@ -49,10 +54,11 @@ [ '--quiet', '-q', GetoptLong::NO_ARGUMENT ], [ '--concurrency', '-c', GetoptLong::REQUIRED_ARGUMENT ], [ '--delay', '-d', GetoptLong::REQUIRED_ARGUMENT ], [ '--retry', '-y', GetoptLong::REQUIRED_ARGUMENT ], [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ], + [ '--help', '-h', GetoptLong::NO_ARGUMENT ], [ '--exit', '-x', GetoptLong::NO_ARGUMENT ] ) opts.each do |opt, arg| case opt @@ -69,10 +75,12 @@ @verbose = true when '--quiet', '-q' @quiet = true when '--port', '-p' @port = arg + when '--help', '-h' + help when '--exit', '-x' uri = "http://localhost:#{@port}/admin" response = RestClient.post uri, { 'shutdown' => true }.to_yaml exit 0 end @@ -88,16 +96,44 @@ Chimp.set_verbose(@verbose, @quiet) if not @verbose ENV['REST_CONNECTION_LOG'] = "/dev/null" ENV['RESTCLIENT_LOG'] = "/dev/null" + Log.threshold= Logger::INFO + else + Log.threshold= Logger::DEBUG end if @quiet Log.threshold = Logger::WARN end + end + # + # Print out help information + # + def help + puts + puts "chimpd -- a RightScale Platform command-line tool" + puts + puts "Syntax: chimpd [--logfile=<name>] [--concurrency=<c>] [--delay=<d>] [--retry=<r>] [--port=<p>] [--verbose]" + puts + puts "Options:" + puts + puts " --logfile=<name> Specifiy the desired log location" + puts " --concurrency=<n> Specify the level of concurrent actions" + puts " --delay=<n> Specify the number of seconds to wait before executing the action" + puts " --retry=<r> Specify the number of times chimpd should retry executing the action" + puts + puts " --verbose Run chimpd in verbose mode." + puts " --quiet Supress non-essential output" + puts + puts " --port=<port> Specify the port number for chimpd to listen on (default: 9055)" + puts + puts " --help Displays this menu" + puts + exit 0 end # # Spawn the ChimpQueue threads # @@ -166,12 +202,10 @@ # # Quit by waiting for all chimp jobs to finish, not allowing # new jobs on the queue, and killing the web server. # - # TODO: call @queue.quit, but with a short timeout? - # def quit @running = false @server.shutdown sleep 5 exit 0 @@ -191,52 +225,65 @@ # begin c = Chimp.new c.interactive = false c.quiet = true - c.tags = ["bogus:tag=true"] + #c.tags = ["bogus:tag=true"] c.run rescue StandardError end + puts "chimpd #{VERSION} launched with #{@concurrency} workers" + Log.debug "Spawning #{n} submission processing threads" + (1..n).each do |n| @threads ||=[] @threads << Thread.new { while true begin queued_request = @chimp_queue.pop group = queued_request.group queued_request.interactive = false - tasks = queued_request.process tasks.each do |task| ChimpQueue.instance.push(group, task) end rescue StandardError => ex - Log.error "submission processor: group=\"#{group}\" script=\"#{queued_request.script}\": #{ex}" + puts ex.backtrace + Log.error " submission processor: group=\"#{group}\" script=\"#{queued_request.script}\": #{ex}" end end } end end # # GenericServlet -- servlet superclass # class GenericServlet < WEBrick::HTTPServlet::AbstractServlet + # + # get_verb + # get_id + # get_payload + # def get_verb(req) r = req.request_uri.path.split('/')[2] end def get_id(req) uri_parts = req.request_uri.path.split('/') id = uri_parts[-2] return id end + def get_job_uuid(req) + string = req.body.scan(/job_uuid: .{6}/).last + job_uuid = string.scan(/ (.{6})/).last.last + return job_uuid + end # # Get the body of the request-- assume YAML # def get_payload(req) begin @@ -249,10 +296,13 @@ # # AdminServlet - admin functions # class AdminServlet < GenericServlet + # + # get do_POST + # def do_POST(req, resp) payload = self.get_payload(req) shutdown = payload['shutdown'] || false if shutdown == true @@ -268,19 +318,23 @@ # # http://localhost:9055/group/default/running # class GroupServlet < GenericServlet # + # do_GET + # do_POST + # + + # # GET a group by name and status # /group/<name>/<status> # def do_GET(req, resp) jobs = {} group_name = req.request_uri.path.split('/')[-2] filter = req.request_uri.path.split('/')[-1] - g = ChimpQueue[group_name.to_sym] raise WEBrick::HTTPStatus::NotFound, "Group not found" unless g jobs = g.get_jobs_by_status(filter) resp.body = jobs.to_yaml raise WEBrick::HTTPStatus::OK @@ -307,22 +361,27 @@ else raise WEBrick::HTTPStatus::PreconditionFailed.new("invalid action") end end + end # GroupServlet - end - # # JobServlet - job control # # HTTP body is a yaml serialized chimp object # class JobServlet < GenericServlet + # + # do_POST + # do_GET + # + def do_POST(req, resp) id = -1 job_id = self.get_id(req) + job_uuid= self.get_job_uuid(req) verb = self.get_verb(req) payload = self.get_payload(req) raise WEBrick::HTTPStatus::PreconditionFailed.new('missing payload') unless payload @@ -336,20 +395,21 @@ # payload.interactive = false # tasks = payload.process # tasks.each do |task| # q.push(group, task) # end - if verb == 'process' or verb == 'add' ChimpDaemon.instance.chimp_queue.push payload id = 0 elsif verb == 'update' puts "UPDATE" q.get_job(job_id).status = payload.status end + resp.body = { + 'job_uuid' => job_uuid , 'id' => id }.to_yaml raise WEBrick::HTTPStatus::OK end @@ -442,10 +502,14 @@ # # DisplayServlet # class DisplayServlet < GenericServlet + # + # do_GET + # + def do_GET(req, resp) # # First determine the path to the files to serve # if ENV['CHIMP_TEST'] != 'TRUE' @@ -491,7 +555,8 @@ resp.body = @template.result(binding) raise WEBrick::HTTPStatus::OK end end end # DisplayServlet + end # ChimpDaemon end