class Chimp::ChimpDaemon::JobServlet

JobServlet - job control

HTTP body is a yaml serialized chimp object

Public Instance Methods

do_GET(req, resp) click to toggle source
# File lib/right_chimp/daemon/chimp_daemon.rb, line 467
def do_GET(req, resp)
  id          = self.get_id(req)
  verb        = self.get_verb(req)
  job_results = 'OK'
  queue       = ChimpQueue.instance

  #
  # check for special job ids
  #
  jobs = []
  jobs << queue.get_job(id.to_i)

  jobs = queue.get_jobs_by_status(:running) if id == 'running'
  jobs = queue.get_jobs_by_status(:error)   if id == 'error'
  jobs = queue.get_jobs_by_status(:holding) if id == 'holding'
  jobs = queue.get_jobs                     if id == 'all'

  raise WEBrick::HTTPStatus::PreconditionFailed.new('invalid or missing job_id #{id}') unless jobs.size > 0

  #
  # ACK a job -- mark it as successful even if it failed
  #
  if req.request_uri.path =~ /ack$/
    jobs.each do |j|
      j.status = Executor::STATUS_DONE
    end

    resp.set_redirect( WEBrick::HTTPStatus::TemporaryRedirect, req.header['referer'])

  #
  # queue a job
  #
  elsif req.request_uri.path =~ /queue$/
    jobs.each do |j|
      j.queue
    end

    resp.set_redirect( WEBrick::HTTPStatus::TemporaryRedirect, req.header['referer'])

  #
  # retry a job
  #
  elsif req.request_uri.path =~ /retry$/
    jobs.each do |j|
      j.requeue
    end

    resp.set_redirect( WEBrick::HTTPStatus::TemporaryRedirect, req.header['referer'])

  #
  # cancel an active job
  #
  elsif req.request_uri.path =~ /cancel$/
    jobs.each do |j|
          j.cancel if j.respond_to? :cancel
    end

          resp.set_redirect( WEBrick::HTTPStatus::TemporaryRedirect, req.header['referer'])

  #
  # produce a report
  #
  elsif req.request_uri.path =~ /report$/
    results = ["group_name,type,job_id,script,target,start_time,end_time,total_time,status"]
    jobs.each do |j|
      results << [j.group.group_id, j.class.to_s.sub("Chimp::",""), j.job_id, j.info, j.target, j.time_start, j.time_end, j.get_total_exec_time, j.status].join(",")
    end

    queue.group.values.each do |g|
      results << [g.group_id, g.class.to_s.sub("Chimp::",""), "", "", "", g.time_start, g.time_end, g.get_total_exec_time, ""].join(",")
    end

    job_results = results.join("\n") + "\n"

    resp['Content-type'] = "text/csv"
    resp['Content-disposition'] = "attachment;filename=chimp.csv"
  end

  #
  # return a list of the results
  #
  resp.body = job_results
  raise WEBrick::HTTPStatus::OK
end
do_POST(req, resp) click to toggle source

do_POST do_GET

# File lib/right_chimp/daemon/chimp_daemon.rb, line 416
def do_POST(req, resp)
  id      = -1
  # we don't know the job_id because we cant guess how many tasks one call creates.
  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

  q = ChimpQueue.instance
  group = payload.group

  #
  # Ask chimpd to process a Chimp object directly
  #
  if verb == 'process' or verb == 'add'
    ChimpDaemon.instance.semaphore.synchronize do
      # While we are at it, we will store these processing jobs to prevent issues in the event
      # of a very slow API response.
      Log.debug 'Adding job: ' + job_uuid + ' to the processing queue for group: ' + group.to_s

      q.processing[payload.group] = {} if q.processing[payload.group].nil?
      q.processing[payload.group][payload.job_uuid.to_sym] = 0

      ChimpDaemon.instance.proc_counter += 1
    end
    # comment the next line to GET STUCK IN PROCESSING forever
    ChimpDaemon.instance.chimp_queue.push payload

    # Proper count of processing Tasks
    counter = 0
    q.processing.each{|k,v|
      counter += v.size
    }
    Log.debug 'Processing:'
    Log.debug 'Tasks in the processing queue:' + counter.to_s
    Log.debug q.processing.inspect
  elsif verb == 'update'
    puts 'UPDATE'
    q.get_job(job_id).status = payload.status
  end

  resp.body = {
    'job_uuid' => job_uuid,
    'id' => job_id
  }.to_yaml

  raise WEBrick::HTTPStatus::OK
end