buildbox 0.1.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c49f0164d963671930c2f730f66da39aceb1adaa
4
- data.tar.gz: ee406ff734ec3bc6c70b4bb778dcb2c62bcd92a0
3
+ metadata.gz: 1b76bf6bc33ef1f87601dedd4dc1a6eefed0ed2c
4
+ data.tar.gz: aaa97fcc886c1e55e7dd4fde4c91c2aaa514b269
5
5
  SHA512:
6
- metadata.gz: 0cfa60a987d07bd4058cdc1965ac9ea7616b39a8afb7104160553a6086d1d7224af9fb787627cea0c090d4b0600ee94923d5ed4fcb15123738befccbe7490525
7
- data.tar.gz: 39e976466076ec75a46bfa8d4d8c39ecf0ada9c8298ea1cb94fda9ecfe2316dd0da470b61ae1cbe46a4ae675ed465932fca809721f95006d8295071956a47a05
6
+ metadata.gz: f42ebc6c4718990e2b5dfdaa67828e923e3b1ecf1f5b228262f631c0e6451e63b21aaa6e86ce85b78554a08d978b2ff89102f04da697cc548aae156ea204d53e
7
+ data.tar.gz: ff5f689b68da01fcbd56e3b9da34cdcc31d9a404b3fef28bd11f6d7d8363d7f50cd93def71b5f29eb13cba26c2d9f8c6418a0b3a60e296719db0b8bc49376194
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- buildbox (0.1.1)
4
+ buildbox (0.1.4)
5
5
  celluloid (~> 0.14)
6
6
  faraday (~> 0.8)
7
7
  faraday_middleware (~> 0.9)
@@ -1,4 +1,5 @@
1
1
  require 'pathname'
2
+ require 'logger'
2
3
 
3
4
  module Buildbox
4
5
  autoload :API, "buildbox/api"
@@ -10,13 +11,18 @@ module Buildbox
10
11
  autoload :Monitor, "buildbox/monitor"
11
12
  autoload :Runner, "buildbox/runner"
12
13
  autoload :Script, "buildbox/script"
14
+ autoload :Server, "buildbox/server"
13
15
  autoload :UTF8, "buildbox/utf8"
14
16
  autoload :Worker, "buildbox/worker"
15
17
  autoload :VERSION, "buildbox/version"
16
18
 
17
19
  def self.config
18
20
  @config ||= Configuration.new.tap(&:reload)
19
- end
21
+ end
22
+
23
+ def self.logger
24
+ @logger ||= Logger.new(STDOUT).tap { |logger| logger.level = Logger::INFO }
25
+ end
20
26
 
21
27
  def self.root_path
22
28
  path = Pathname.new File.join(ENV['HOME'], ".buildbox")
@@ -27,12 +27,13 @@ module Buildbox
27
27
  @connection ||= Faraday.new(:url => @config.api_endpoint) do |faraday|
28
28
  faraday.request :json
29
29
 
30
- faraday.response :logger
30
+ faraday.response :logger, Buildbox.logger
31
31
  faraday.response :mashify
32
32
 
33
33
  # json needs to come after mashify as it needs to run before the mashify
34
34
  # middleware.
35
35
  faraday.response :json
36
+ faraday.response :raise_error
36
37
 
37
38
  faraday.adapter Faraday.default_adapter
38
39
  end
@@ -51,7 +51,7 @@ module Buildbox
51
51
  end
52
52
 
53
53
  if command == "worker:start"
54
- Buildbox::Worker.new(Buildbox.config.worker_access_token).start
54
+ Buildbox::Server.new.start
55
55
  elsif command == "worker:setup"
56
56
  if @argv.length == 0
57
57
  puts "No token provided"
@@ -59,7 +59,8 @@ module Buildbox
59
59
  end
60
60
 
61
61
  access_token = @argv.first
62
- Buildbox.config.update(:worker_access_token => access_token)
62
+ worker_access_tokens = Buildbox.config.worker_access_tokens
63
+ Buildbox.config.update(:worker_access_tokens => worker_access_tokens << access_token)
63
64
 
64
65
  puts "Successfully added worker access token"
65
66
  puts "You can now start the worker with `buildbox worker:start`"
@@ -4,8 +4,8 @@ require 'json'
4
4
 
5
5
  module Buildbox
6
6
  class Configuration < Hashie::Dash
7
- property :worker_access_token, :default => nil
8
- property :api_endpoint, :default => "http://api.buildbox.io/v1"
7
+ property :worker_access_tokens, :default => []
8
+ property :api_endpoint, :default => "https://api.buildbox.io/v1"
9
9
 
10
10
  def update(attributes)
11
11
  attributes.each_pair { |key, value| self[key] = value }
@@ -12,12 +12,16 @@ module Buildbox
12
12
 
13
13
  def monitor
14
14
  loop do
15
- @api.update_build(@build) if @build.started?
15
+ # There is an edge case where the build finishes between making the
16
+ # update_build http call, and breaking. So to make sure we're using the
17
+ # same build object throughout this call, we can just dup it.
18
+ build = @build.dup
19
+ @api.update_build(build) if build.started?
16
20
 
17
- if @build.finished?
21
+ if build.finished?
18
22
  break
19
23
  else
20
- sleep 1
24
+ sleep 3 # 3 seconds seems reasonable for now
21
25
  end
22
26
  end
23
27
  end
@@ -1,6 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'celluloid'
3
- require 'tempfile'
4
3
  require 'fileutils'
5
4
 
6
5
  module Buildbox
@@ -29,7 +28,9 @@ module Buildbox
29
28
  build.output = result.output
30
29
  build.exit_status = result.exit_status
31
30
 
32
- info "#{namespace}/#{@build.number} finished"
31
+ File.delete(script_path)
32
+
33
+ info "#{namespace} ##{@build.number} finished with exit status #{result.exit_status}"
33
34
  end
34
35
 
35
36
  private
@@ -45,7 +46,7 @@ module Buildbox
45
46
  end
46
47
 
47
48
  def script_path
48
- @script_path ||= Tempfile.new("buildbox-#{namespace.gsub(/\//, '-')}-#{@build.number}").path
49
+ @script_path ||= Buildbox.root_path.join("buildbox-#{namespace.gsub(/\//, '-')}-#{@build.number}")
49
50
  end
50
51
 
51
52
  def namespace
@@ -0,0 +1,33 @@
1
+ module Buildbox
2
+ class Server
3
+ INTERVAL = 5
4
+
5
+ def initialize(config = Buildbox.config, logger = Buildbox.logger)
6
+ @config = config
7
+ @logger = logger
8
+ end
9
+
10
+ def start
11
+ loop do
12
+ worker_access_tokens.each do |access_token|
13
+ Buildbox::Worker.new(access_token, api).work
14
+ end
15
+
16
+ @config.reload
17
+
18
+ @logger.info "Sleeping for #{INTERVAL} seconds"
19
+ sleep INTERVAL
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def api
26
+ @api ||= Buildbox::API.new
27
+ end
28
+
29
+ def worker_access_tokens
30
+ @config.worker_access_tokens
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Buildbox
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -1,33 +1,38 @@
1
+ require 'rubygems'
2
+ require 'celluloid'
3
+
1
4
  module Buildbox
2
5
  class Worker
3
- def initialize(access_token)
6
+ include Celluloid::Logger
7
+
8
+ def initialize(access_token, api)
9
+ @api = api
4
10
  @access_token = access_token
5
11
  end
6
12
 
7
- def start
8
- loop do
9
- projects.each do |project|
10
- running_builds = api.scheduled_builds(project).map do |build|
11
- Monitor.new(build, api).async.monitor
12
- Runner.new(build).future(:start)
13
- end
14
-
15
- # wait for all the running builds to finish
16
- running_builds.map(&:value)
17
-
18
- sleep 5
19
- end
13
+ def work
14
+ running_builds = scheduled_builds.map do |build|
15
+ Monitor.new(build, @api).async.monitor
16
+ Runner.new(build).future(:start)
20
17
  end
18
+
19
+ # wait for all the running builds to finish
20
+ running_builds.map(&:value)
21
21
  end
22
22
 
23
23
  private
24
24
 
25
- def api
26
- @api ||= Buildbox::API.new
25
+ def projects
26
+ @api.worker(:access_token => @access_token, :hostname => hostname).projects
27
+ rescue Faraday::Error::ClientError
28
+ warn "Worker #{@access_token} doesn't exist"
29
+ [] # return empty array to avoid breakage
27
30
  end
28
31
 
29
- def projects
30
- api.worker(:access_token => @access_token, :hostname => hostname).projects
32
+ def scheduled_builds
33
+ projects.map do |project|
34
+ @api.scheduled_builds(project)
35
+ end.flatten
31
36
  end
32
37
 
33
38
  def hostname
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Pitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-24 00:00:00.000000000 Z
11
+ date: 2013-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -107,6 +107,7 @@ files:
107
107
  - lib/buildbox/environment.rb
108
108
  - lib/buildbox/monitor.rb
109
109
  - lib/buildbox/runner.rb
110
+ - lib/buildbox/server.rb
110
111
  - lib/buildbox/utf8.rb
111
112
  - lib/buildbox/version.rb
112
113
  - lib/buildbox/worker.rb