Class: EnhanceSwarm::ResourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/enhance_swarm/resource_manager.rb

Overview

Manages system resources and enforces limits for agent spawning

Constant Summary collapse

MAX_CONCURRENT_AGENTS =
10
MAX_MEMORY_MB =
2048
MAX_DISK_MB =
1024

Instance Method Summary collapse

Constructor Details

#initializeResourceManager

Returns a new instance of ResourceManager.



13
14
15
# File 'lib/enhance_swarm/resource_manager.rb', line 13

def initialize
  @config = EnhanceSwarm.configuration
end

Instance Method Details

#can_spawn_agent?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/enhance_swarm/resource_manager.rb', line 17

def can_spawn_agent?
  result = {
    allowed: true,
    reasons: []
  }

  # Check concurrent agent limit
  current_agents = count_active_agents
  max_agents = @config.max_concurrent_agents || MAX_CONCURRENT_AGENTS
  
  if current_agents >= max_agents
    result[:allowed] = false
    result[:reasons] << "Maximum concurrent agents reached (#{current_agents}/#{max_agents})"
  end

  # Check system memory
  if memory_usage_too_high?
    result[:allowed] = false
    result[:reasons] << "System memory usage too high"
  end

  # Check disk space
  if disk_usage_too_high?
    result[:allowed] = false
    result[:reasons] << "Insufficient disk space"
  end

  # Check system load
  if system_load_too_high?
    result[:allowed] = false
    result[:reasons] << "System load too high"
  end

  result
end

#enforce_limits!Object



63
64
65
66
67
68
69
70
# File 'lib/enhance_swarm/resource_manager.rb', line 63

def enforce_limits!
  stats = get_resource_stats
  
  if stats[:active_agents] > stats[:max_agents]
    Logger.warn("Agent limit exceeded: #{stats[:active_agents]}/#{stats[:max_agents]}")
    cleanup_oldest_agents(stats[:active_agents] - stats[:max_agents])
  end
end

#get_resource_statsObject



53
54
55
56
57
58
59
60
61
# File 'lib/enhance_swarm/resource_manager.rb', line 53

def get_resource_stats
  {
    active_agents: count_active_agents,
    max_agents: @config.max_concurrent_agents || MAX_CONCURRENT_AGENTS,
    memory_usage_mb: get_memory_usage_mb,
    disk_usage_mb: get_disk_usage_mb,
    system_load: get_system_load
  }
end