Class: Chimp::ChimpDaemon::JobServlet

Inherits:
GenericServlet
  • Object
show all
Defined in:
lib/right_chimp/daemon/chimp_daemon.rb

Overview

JobServlet - job control

HTTP body is a yaml serialized chimp object

Instance Method Summary collapse

Methods inherited from GenericServlet

#get_id, #get_job_uuid, #get_payload, #get_verb

Instance Method Details

#do_GET(req, resp) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::PreconditionFailed)


467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# 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) ⇒ Object

do_POST do_GET

Raises:

  • (WEBrick::HTTPStatus::PreconditionFailed)


416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# 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