Class: Chimp::ChimpDaemon::DisplayServlet

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



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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/right_chimp/daemon/chimp_daemon.rb', line 561

def do_GET(req, resp)
  #
  # First determine the path to the files to serve
  #
  if ENV['CHIMP_TEST'] != 'TRUE'
    template_path = File.expand_path('../templates', __dir__)
  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} / "
    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(:none).size,
                  "failed" => queue.get_jobs_by_status(:error).size,
                  "done" => queue.get_jobs_by_status(:done).size,
                  "processing" => ChimpDaemon.instance.proc_counter,
                  "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,
          :uuid => job.job_uuid,
          :server => job.server.name,
          :script => job.info,
          :audit_entry_url => job.audit_entry_url
        }
      end
    end

    resp.body = jobs.to_json

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /jobs\.json\/id\/\d+$/

    job_id = File.basename(req.request_uri.path)
    queue = ChimpQueue.instance

    res = queue.get_job(job_id)

    case res
    when ExecRightScript

      result = {}
      result[:id] = job_id
      result[:uuid] = res.job_uuid
      result[:status] = res.status
      result[:server] = res.server.name
      result[:script] = res.info
      result[:audit_entry_url] = res.audit_entry_url

      resp.body = result.to_json
    end

    raise WEBrick::HTTPStatus::OK

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

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

    res = queue.get_jobs_by_uuid(uuid)

    jobs = {}

    res.each_with_index do |r, i|
      jobs[i] = { id: r.job_id,
                  uuid: r.job_uuid,
                  status: r.status,
                  server: r.server.name,
                  script: r.info,
                  audit_entry_url: r.audit_entry_url
                }
    end

    resp.body = jobs.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