Class: Chimp::ChimpDaemon::DisplayServlet

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

Overview

DisplayServlet

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

do_GET



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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 522

def do_GET(req, resp)
  #
  # First determine the path to the files to serve
  #
  if ENV['CHIMP_TEST'] != 'TRUE'
    template_path = File.join(Gem.dir, 'gems', 'right_chimp-' + VERSION, 'lib/right_chimp/templates')
  else
    template_path = 'lib/right_chimp/templates'
  end

  #
  # Check if we are asked for stats
  #
  if req.request_uri.path =~ /stats/
    queue = ChimpQueue.instance
    stats = ""
    stats << "running: #{queue.get_jobs_by_status(:running).size} / "
    stats << "waiting: #{queue.get_jobs_by_status(:none).size} / "
    stats << "failed: #{queue.get_jobs_by_status(:error).size} / "
    stats << "done: #{queue.get_jobs_by_status(:done).size} / "
    stats << "processing: #{ChimpDaemon.instance.proc_counter.to_s} / "
    stats << "\n"

    resp.body = stats

    raise WEBrick::HTTPStatus::OK
  end

  #
  # Check for static CSS files and serve them
  #
  if req.request_uri.path =~ /\.(css|js)$/
    filename = req.request_uri.path.split('/').last
    resp.body = File.read(File.join(template_path, filename))
    raise WEBrick::HTTPStatus::OK
  else

    #
    # Otherwise process ERB template
    #
    job_filter = self.get_verb(req) || "running"

    if not @template
      @template = ERB.new(File.read(File.join(template_path, "all_jobs.erb")), nil, ">")
    end

    queue = ChimpQueue.instance
    jobs = queue.get_jobs
    group_name = nil

    if job_filter == "group"
      group_name = req.request_uri.path.split('/')[-1]
      g = ChimpQueue[group_name.to_sym]
      jobs = g.get_jobs if g
    end

    count_jobs_running = queue.get_jobs_by_status(:running).size
    count_jobs_queued  = queue.get_jobs_by_status(:none).size
    count_jobs_holding  = queue.get_jobs_by_status(:holding).size
    count_jobs_failed  = queue.get_jobs_by_status(:error).size
    count_jobs_done    = queue.get_jobs_by_status(:done).size
    count_jobs_processing = queue.get_jobs_by_status(:processing).size

    resp.body = @template.result(binding)
    raise WEBrick::HTTPStatus::OK
  end
end