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
} }