Fix RuboCop offenses

This commit is contained in:
Mahima Singh 2025-12-24 15:06:22 +05:30
parent fe9f98e281
commit a4abe2652a
3 changed files with 39 additions and 29 deletions

View file

@ -302,27 +302,29 @@ module Vmpooler
# Use a single pipeline to fetch all queue counts at once for better performance # Use a single pipeline to fetch all queue counts at once for better performance
results = backend.pipelined do |pipeline| results = backend.pipelined do |pipeline|
# Order matters - we'll use indices to extract values # Order matters - we'll use indices to extract values
pools.each { |pool| pipeline.scard("vmpooler__provisioning__request#{pool['name']}") } # 0..n-1 pools.each do |pool|
pools.each { |pool| pipeline.scard("vmpooler__provisioning__processing#{pool['name']}") } # n..2n-1 pipeline.scard("vmpooler__provisioning__request#{pool['name']}") # 0..n-1
pools.each { |pool| pipeline.scard("vmpooler__odcreate__task#{pool['name']}") } # 2n..3n-1 pipeline.scard("vmpooler__provisioning__processing#{pool['name']}") # n..2n-1
pools.each { |pool| pipeline.scard("vmpooler__pending__#{pool['name']}") } # 3n..4n-1 pipeline.scard("vmpooler__odcreate__task#{pool['name']}") # 2n..3n-1
pools.each { |pool| pipeline.scard("vmpooler__ready__#{pool['name']}") } # 4n..5n-1 pipeline.scard("vmpooler__pending__#{pool['name']}") # 3n..4n-1
pools.each { |pool| pipeline.scard("vmpooler__running__#{pool['name']}") } # 5n..6n-1 pipeline.scard("vmpooler__ready__#{pool['name']}") # 4n..5n-1
pools.each { |pool| pipeline.scard("vmpooler__completed__#{pool['name']}") } # 6n..7n-1 pipeline.scard("vmpooler__running__#{pool['name']}") # 5n..6n-1
pipeline.get('vmpooler__tasks__clone') # 7n pipeline.scard("vmpooler__completed__#{pool['name']}") # 6n..7n-1
pipeline.get('vmpooler__tasks__ondemandclone') # 7n+1 end
pipeline.get('vmpooler__tasks__clone') # 7n
pipeline.get('vmpooler__tasks__ondemandclone') # 7n+1
end end
n = pools.length n = pools.length
# Safely extract results with default to empty array if slice returns nil # Safely extract results with default to empty array if slice returns nil
queue[:requested] = (results[0...n] || []).sum(&:to_i) + queue[:requested] = (results[0...n] || []).sum(&:to_i) +
(results[n...(2*n)] || []).sum(&:to_i) + (results[n...(2 * n)] || []).sum(&:to_i) +
(results[(2*n)...(3*n)] || []).sum(&:to_i) (results[(2 * n)...(3 * n)] || []).sum(&:to_i)
queue[:pending] = (results[(3*n)...(4*n)] || []).sum(&:to_i) queue[:pending] = (results[(3 * n)...(4 * n)] || []).sum(&:to_i)
queue[:ready] = (results[(4*n)...(5*n)] || []).sum(&:to_i) queue[:ready] = (results[(4 * n)...(5 * n)] || []).sum(&:to_i)
queue[:running] = (results[(5*n)...(6*n)] || []).sum(&:to_i) queue[:running] = (results[(5 * n)...(6 * n)] || []).sum(&:to_i)
queue[:completed] = (results[(6*n)...(7*n)] || []).sum(&:to_i) queue[:completed] = (results[(6 * n)...(7 * n)] || []).sum(&:to_i)
queue[:cloning] = (results[7*n] || 0).to_i + (results[7*n + 1] || 0).to_i queue[:cloning] = (results[7 * n] || 0).to_i + (results[7 * n + 1] || 0).to_i
queue[:booting] = queue[:pending].to_i - queue[:cloning].to_i queue[:booting] = queue[:pending].to_i - queue[:cloning].to_i
queue[:booting] = 0 if queue[:booting] < 0 queue[:booting] = 0 if queue[:booting] < 0
queue[:total] = queue[:requested] + queue[:pending].to_i + queue[:ready].to_i + queue[:running].to_i + queue[:completed].to_i queue[:total] = queue[:requested] + queue[:pending].to_i + queue[:ready].to_i + queue[:running].to_i + queue[:completed].to_i

View file

@ -10,14 +10,21 @@ module Vmpooler
api_prefix = "/api/v#{api_version}" api_prefix = "/api/v#{api_version}"
# Simple in-memory cache for status endpoint # Simple in-memory cache for status endpoint
@@status_cache = {} @status_cache = {}
@@status_cache_mutex = Mutex.new @status_cache_mutex = Mutex.new
STATUS_CACHE_TTL = 30 # seconds STATUS_CACHE_TTL = 30 # seconds
class << self
attr_accessor :status_cache, :status_cache_mutex
end
@status_cache ||= {}
@status_cache_mutex ||= Mutex.new
# Clear cache (useful for testing) # Clear cache (useful for testing)
def self.clear_status_cache def self.clear_status_cache
@@status_cache_mutex.synchronize do @status_cache_mutex.synchronize do
@@status_cache.clear @status_cache.clear
end end
end end
@ -478,18 +485,19 @@ module Vmpooler
# Cache helper methods for status endpoint # Cache helper methods for status endpoint
def get_cached_status(cache_key) def get_cached_status(cache_key)
@@status_cache_mutex.synchronize do self.class.status_cache_mutex.synchronize do
cached = @@status_cache[cache_key] cached = self.class.status_cache[cache_key]
if cached && (Time.now - cached[:timestamp]) < STATUS_CACHE_TTL if cached && (Time.now - cached[:timestamp]) < STATUS_CACHE_TTL
return cached[:data] return cached[:data]
end end
nil nil
end end
end end
def set_cached_status(cache_key, data) def set_cached_status(cache_key, data)
@@status_cache_mutex.synchronize do self.class.status_cache_mutex.synchronize do
@@status_cache[cache_key] = { self.class.status_cache[cache_key] = {
data: data, data: data,
timestamp: Time.now timestamp: Time.now
} }
@ -685,7 +693,7 @@ module Vmpooler
# Create cache key based on view parameters # Create cache key based on view parameters
cache_key = params[:view] ? "status_#{params[:view]}" : "status_all" cache_key = params[:view] ? "status_#{params[:view]}" : "status_all"
# Try to get cached response # Try to get cached response
cached_response = get_cached_status(cache_key) cached_response = get_cached_status(cache_key)
return cached_response if cached_response return cached_response if cached_response
@ -751,10 +759,10 @@ module Vmpooler
result[:status][:uptime] = (Time.now - Vmpooler::API.settings.config[:uptime]).round(1) if Vmpooler::API.settings.config[:uptime] result[:status][:uptime] = (Time.now - Vmpooler::API.settings.config[:uptime]).round(1) if Vmpooler::API.settings.config[:uptime]
response = JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }]) response = JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }])
# Cache the response # Cache the response
set_cached_status(cache_key, response) set_cached_status(cache_key, response)
response response
end end

View file

@ -1699,7 +1699,7 @@ module Vmpooler
start_time = Time.now start_time = Time.now
result = _check_pool(pool, provider) result = _check_pool(pool, provider)
duration = Time.now - start_time duration = Time.now - start_time
$metrics.gauge("vmpooler_performance.check_pool.#{pool['name']}", duration) $metrics.gauge("vmpooler_performance.check_pool.#{pool['name']}", duration)
$logger.log('d', "[!] check_pool for #{pool['name']} took #{duration.round(2)}s") if duration > 5 $logger.log('d', "[!] check_pool for #{pool['name']} took #{duration.round(2)}s") if duration > 5