Class: Chimp::ChimpDaemon::JobServlet

Inherits:
GenericServlet
  • Object
show all
Defined in:
lib/right_chimp/daemon/ChimpDaemon.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)


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
466
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
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 429

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)


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 387

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

  q = ChimpQueue.instance
  group = payload.group

  #
  # Ask chimpd to process a Chimp object directly
  #
  #if verb == 'process' or verb == 'add'
  #  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
    ChimpDaemon.instance.semaphore.synchronize do
      ChimpDaemon.instance.proc_counter +=1
    end
    Log.debug "Tasks in the processing queue: #{ChimpDaemon.instance.proc_counter.to_s}"
    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