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_payload, #get_verb

Instance Method Details

#do_GET(req, resp) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::PreconditionFailed)


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
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
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 357

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

Raises:

  • (WEBrick::HTTPStatus::PreconditionFailed)


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 321

def do_POST(req, resp)
  id      = -1
  job_id  = self.get_id(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
    id = 0
  elsif verb == 'update'
    puts "UPDATE"
    q.get_job(job_id).status = payload.status
  end

  resp.body = {
    'id' => id
  }.to_yaml

  raise WEBrick::HTTPStatus::OK
end