lib/buildbox/agent.rb in buildbox-0.3.3 vs lib/buildbox/agent.rb in buildbox-0.3.4
- old
+ new
@@ -1,39 +1,40 @@
-require 'rubygems'
require 'celluloid'
module Buildbox
class Agent
+ include Celluloid
include Celluloid::Logger
- def initialize(access_token, api)
+ def initialize(access_token, api = Buildbox::API.new)
@api = api
@access_token = access_token
end
def work
- running_builds = scheduled_builds.map do |build|
- Monitor.new(build, @api).async.monitor
- Runner.new(build).future(:start)
+ builds = scheduled_builds
+
+ # Start off by letting each build know that it's been picked up
+ # by an agent.
+ builds.each do |build|
+ @api.update_build(build, :agent_accepted => @access_token)
end
- # wait for all the running builds to finish
- running_builds.map(&:value)
+ # Run the builds one at a time
+ builds.each do |build|
+ Monitor.new(build, @api).async.monitor
+ Runner.new(build).start
+ end
end
private
- def projects
- @api.agent(@access_token, hostname).projects
- rescue Faraday::Error::ClientError
- warn "Agent #{@access_token} doesn't exist"
- [] # return empty array to avoid breakage
- end
-
def scheduled_builds
- projects.map do |project|
- @api.scheduled_builds(project)
- end.flatten
+ agent = @api.agent(@access_token, hostname)
+ @api.scheduled_builds agent
+ rescue Buildbox::API::AgentNotFoundError
+ warn "Agent `#{@access_token}` doesn't exist"
+ [] # return empty array to avoid breakage
end
def hostname
`hostname`.chomp
end