Class: SpotBuild::BuildkiteAgents

Inherits:
Object
  • Object
show all
Defined in:
lib/spot_build/buildkite_agents.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, org_slug) ⇒ BuildkiteAgents

Returns a new instance of BuildkiteAgents.



7
8
9
10
# File 'lib/spot_build/buildkite_agents.rb', line 7

def initialize(token, org_slug)
  @client = Buildkit.new(token: token)
  @org_slug = org_slug
end

Instance Method Details

#agents_running?(host = Socket.gethostname) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/spot_build/buildkite_agents.rb', line 29

def agents_running?(host = Socket.gethostname)
  !agents_on_this_host(host).empty?
end

#stop(host = Socket.gethostname) ⇒ Object



33
34
35
36
37
# File 'lib/spot_build/buildkite_agents.rb', line 33

def stop(host = Socket.gethostname)
  agents_on_this_host(host).each do |agent|
    stop_agent(agent, force: false)
  end
end

#stop_agent(agent, force: false) ⇒ Object



23
24
25
26
27
# File 'lib/spot_build/buildkite_agents.rb', line 23

def stop_agent(agent, force: false)
  @client.stop_agent(@org_slug, agent.id, "{\"force\": #{force}}")
rescue Buildkit::UnprocessableEntity
  # Swallow the error, this is generally thrown when the agent has already stopped
end

#the_end_is_nigh(host = Socket.gethostname) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/spot_build/buildkite_agents.rb', line 12

def the_end_is_nigh(host = Socket.gethostname)
  agents = agents_on_this_host(host)
  agents.each do |agent|
    stop_agent(agent, force: true)
  end
  agents.each do |agent|
    reschedule_job(agent.job)
  end
  agents.count
end