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



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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/right_chimp/daemon/ChimpDaemon.rb', line 523

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_i} / "
    stats << "\n"

    resp.body = stats

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /stats\.json$/
    # instance the queue
    queue = ChimpQueue.instance

    stats_hash = {"running" => queue.get_jobs_by_status(:running).size,
                  "waiting" => queue.get_jobs_by_status(:waiting).size,
                  "failed" => queue.get_jobs_by_status(:error).size,
                  "done" => queue.get_jobs_by_status(:done).size,
                  "processing" => ChimpDaemon.instance.proc_counter.to_i,
                  "holding" => queue.get_jobs_by_status(:holding).size
                }

    resp.body = JSON.generate(stats_hash)

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /jobs\.json$/
    #instance the queue
    queue = ChimpQueue.instance

    job_types = [ :running, :error, :done ]

    jobs = {}

    job_types.each do |type|
      jobs[type] = queue.get_jobs_by_status(type).map do |job|
        { :id => job.job_id,
          :server => job.server.params["name"],
          :name => job.exec.params["right_script"]["name"],
          :error_message => job.error.nil? ? "" : job.error.message
        }
      end
    end

    resp.body = jobs.to_json

    raise WEBrick::HTTPStatus::OK
  end

  #
  # Attempt to return just 1 job data
  #
  if req.request_uri.path =~ /jobs\.json\/id\/*\w{6}$/

    job_uid = File.basename(req.request_uri.path)
    #instance the queue
    queue = ChimpQueue.instance

    my_hash =  {}

    #! Multiple servers WILL match for the same job_uuid
    queue.group.each { |group|
      #per each group, locate all jobs
      group[1].get_jobs.each {|job|
        my_hash[job.job_uuid] = { job.job_id => { "state" => job.results,
                                                  "server" => job.server.params["name"],
                                                  "audit_entry" => job.audit_entry_data,
                                  }
                                }
      }
    }

    resp.body = my_hash[job_uid].to_json

    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