From 1073629c131b3fbdf60b8460a19c5b62d13bc5c2 Mon Sep 17 00:00:00 2001 From: John O'Connor Date: Fri, 3 Jul 2020 20:02:57 +0100 Subject: [PATCH] (POOLER-181) Separate Dashboard from api Allow the dashboard to run separately from the API, so that the dashboard can be scaled appropriately. Note - this implementation actually co-runs the api in the dashboard instance to re-use the api code for the dashboard endpoints and other v1 endpoints collecting the operating information. --- bin/vmpooler | 9 +++++---- lib/vmpooler/api.rb | 15 ++++++++++----- spec/integration/dashboard_spec.rb | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/bin/vmpooler b/bin/vmpooler index 139d79c..ed7afa7 100755 --- a/bin/vmpooler +++ b/bin/vmpooler @@ -16,20 +16,21 @@ metrics = Vmpooler::Metrics.init(logger, config) torun_threads = [] if ARGV.count == 0 - torun = %i[api manager] + torun = %i[api manager dashboard] else torun = [] torun << :api if ARGV.include? 'api' torun << :manager if ARGV.include? 'manager' + torun << :dashboard if ARGV.include? 'dashboard' exit(2) if torun.empty? end -if torun.include? :api - api = Thread.new do +if (torun & %i[api dashboard]).any? + api_or_dashboard = Thread.new do redis = Vmpooler.new_redis(redis_host, redis_port, redis_password) Vmpooler::API.execute(torun, config, redis, metrics, logger) end - torun_threads << api + torun_threads << api_or_dashboard elsif metrics.respond_to?(:setup_prometheus_metrics) # Run the cut down API - Prometheus Metrics only. prometheus_only_api = Thread.new do diff --git a/lib/vmpooler/api.rb b/lib/vmpooler/api.rb index 9ffe386..358e1a1 100644 --- a/lib/vmpooler/api.rb +++ b/lib/vmpooler/api.rb @@ -3,7 +3,7 @@ module Vmpooler class API < Sinatra::Base # Load API components - %w[helpers dashboard reroute v1 request_logger].each do |lib| + %w[helpers reroute v1 request_logger dashboard].each do |lib| require "vmpooler/api/#{lib}" end # Load dashboard components @@ -42,12 +42,17 @@ module Vmpooler use Prometheus::Middleware::Exporter, path: metrics.endpoint end - if torun.include? :api + # Determine which parts of the API to actually run. + # The Dashboard also runs the API parts for its own "self-referencing" purposes, i.e. to re-use those parts + # that allow it to pull the information from redis. + if (torun & %i[api dashboard]).any? # Enable API request logging only if required - use Vmpooler::API::RequestLogger, logger: logger if config[:config]['request_logger'] + use Vmpooler::API::RequestLogger, logger: logger if config[:config]['request_logger'] && (torun.include? :api) - use Vmpooler::Dashboard - use Vmpooler::API::Dashboard + if torun.include? :dashboard + use Vmpooler::Dashboard + use Vmpooler::API::Dashboard + end use Vmpooler::API::Reroute use Vmpooler::API::V1 end diff --git a/spec/integration/dashboard_spec.rb b/spec/integration/dashboard_spec.rb index b0abbea..98c1673 100644 --- a/spec/integration/dashboard_spec.rb +++ b/spec/integration/dashboard_spec.rb @@ -27,7 +27,7 @@ describe Vmpooler::API do before(:each) do expect(app).to receive(:run!) - app.execute([:api], config, redis, nil, nil) + app.execute([:dashboard], config, redis, nil, nil) app.settings.set :config, auth: false end