lib/buildbox/worker.rb in buildbox-0.1.1 vs lib/buildbox/worker.rb in buildbox-0.1.4
- old
+ new
@@ -1,34 +1,39 @@
+require 'rubygems'
+require 'celluloid'
+
module Buildbox
class Worker
- def initialize(access_token)
+ include Celluloid::Logger
+
+ def initialize(access_token, api)
+ @api = api
@access_token = access_token
end
- def start
- loop do
- projects.each do |project|
- running_builds = api.scheduled_builds(project).map do |build|
- Monitor.new(build, api).async.monitor
- Runner.new(build).future(:start)
- end
-
- # wait for all the running builds to finish
- running_builds.map(&:value)
-
- sleep 5
- end
+ def work
+ running_builds = scheduled_builds.map do |build|
+ Monitor.new(build, @api).async.monitor
+ Runner.new(build).future(:start)
end
+
+ # wait for all the running builds to finish
+ running_builds.map(&:value)
end
private
- def api
- @api ||= Buildbox::API.new
+ def projects
+ @api.worker(:access_token => @access_token, :hostname => hostname).projects
+ rescue Faraday::Error::ClientError
+ warn "Worker #{@access_token} doesn't exist"
+ [] # return empty array to avoid breakage
end
- def projects
- api.worker(:access_token => @access_token, :hostname => hostname).projects
+ def scheduled_builds
+ projects.map do |project|
+ @api.scheduled_builds(project)
+ end.flatten
end
def hostname
`hostname`.chomp
end