Skip to content
Browse files

Merge pull request #57 from mcolyer/build-queue-supports-branches

Build queue supports branches
  • Loading branch information...
2 parents fa120fc + 9cf0bed commit 46c559e69a28601526197fde680b38b4d64a32ca @queso queso committed Jun 22, 2011
Showing with 90 additions and 23 deletions.
  1. +2 −2 README.md
  2. +4 −19 lib/cijoe.rb
  3. +55 −0 lib/cijoe/queue.rb
  4. +28 −0 test/test_cijoe_queue.rb
  5. +1 −2 test/test_cijoe_server.rb
View
4 README.md
@@ -61,13 +61,13 @@ Want joe to run against a branch other than `master`? No problem:
$ git config --add cijoe.branch deploy
-Concurrent Push's - a kind of "queueing"
+Queueing
----------------------------------------
Joe runs just one build at the time. If you expect concurrent push's
to your repo and want joe to build each in a kind of queue, just set:
- $ git config --add cijoe.buildallfile tmp/cijoe.txt
+ $ git config --add cijoe.buildqueue true
Joe will save requests while another build runs. If more than one push
hits joe, he just picks the last after finishing the prior.
View
23 lib/cijoe.rb
@@ -19,6 +19,7 @@
require 'cijoe/build'
require 'cijoe/campfire'
require 'cijoe/server'
+require 'cijoe/queue'
class CIJoe
attr_reader :user, :project, :url, :current_build, :last_build, :campfire
@@ -33,6 +34,7 @@ def initialize(project_path)
@last_build = nil
@current_build = nil
+ @queue = Queue.new(!repo_config.buildqueue.to_s.empty?, true)
trap("INT") { stop }
end
@@ -49,12 +51,6 @@ def pid
# kill the child and exit
def stop
- # another build waits
- if !repo_config.buildallfile.to_s.empty? && File.exist?(repo_config.buildallfile.to_s)
- # clean out on stop
- FileUtils.rm(repo_config.buildallfile.to_s)
- end
-
Process.kill(9, pid) if pid
exit!
end
@@ -81,25 +77,14 @@ def finish_build(status, output)
write_build 'last', @last_build
@campfire.notify(@last_build) if @campfire.valid?
- # another build waits
- if !repo_config.buildallfile.to_s.empty? && File.exist?(repo_config.buildallfile.to_s)
- # clean out before new build
- FileUtils.rm(repo_config.buildallfile.to_s)
- build
- end
+ build(@queue.next_branch_to_build) if @queue.waiting?
end
# run the build but make sure only one is running
# at a time (if new one comes in we will park it)
def build(branch=nil)
if building?
- # only if switched on to build all incoming requests
- if !repo_config.buildallfile.to_s.empty?
- # and there is no previous request
- return if File.exist?(repo_config.buildallfile.to_s)
- # we will mark awaiting builds
- FileUtils.touch(repo_config.buildallfile.to_s)
- end
+ @queue.append_unless_already_exists(branch)
# leave anyway because a current build runs
return
end
View
55 lib/cijoe/queue.rb
@@ -0,0 +1,55 @@
+class CIJoe
+ # An in memory queue used for maintaining an order list of requested
+ # builds.
+ class Queue
+ # enabled - determines whether builds should be queued or not.
+ def initialize(enabled, verbose=false)
+ @enabled = enabled
+ @verbose = verbose
+ @queue = []
+
+ log("Build queueing enabled") if enabled
+ end
+
+ # Public: Appends a branch to be built, unless it already exists
+ # within the queue.
+ #
+ # branch - the name of the branch to build or nil if the default
+ # should be built.
+ #
+ # Returns nothing
+ def append_unless_already_exists(branch)
+ return unless enabled?
+ unless @queue.include? branch
+ @queue << branch
+ log "#{Time.now.to_i}: Queueing #{branch}"
+ end
+ end
+
+ # Returns a String of the next branch to build
+ def next_branch_to_build
+ branch = @queue.shift
+ log "#{Time.now.to_i}: De-queueing #{branch}"
+ branch
+ end
+
+ # Returns true if there are requested builds waiting and false
+ # otherwise.
+ def waiting?
+ if enabled?
+ not @queue.empty?
+ else
+ false
+ end
+ end
+
+ protected
+ def log(msg)
+ puts msg if @verbose
+ end
+
+ def enabled?
+ @enabled
+ end
+ end
+end
View
28 test/test_cijoe_queue.rb
@@ -0,0 +1,28 @@
+require 'helper'
+
+class TestCIJoeQueue < Test::Unit::TestCase
+ def test_a_disabled_queue
+ subject = CIJoe::Queue.new(false)
+ subject.append_unless_already_exists("test")
+ assert_equal false, subject.waiting?
+ end
+
+ def test_adding_two_items_to_a_queue
+ subject = CIJoe::Queue.new(true)
+ subject.append_unless_already_exists("test")
+ subject.append_unless_already_exists(nil)
+ assert_equal true, subject.waiting?
+ assert_equal "test", subject.next_branch_to_build
+ assert_equal nil, subject.next_branch_to_build
+ assert_equal false, subject.waiting?
+ end
+
+ def test_adding_two_duplicate_items_to_a_queue
+ subject = CIJoe::Queue.new(true)
+ subject.append_unless_already_exists("test")
+ subject.append_unless_already_exists("test")
+ assert_equal true, subject.waiting?
+ assert_equal "test", subject.next_branch_to_build
+ assert_equal false, subject.waiting?
+ end
+end
View
3 test/test_cijoe_server.rb
@@ -113,8 +113,7 @@ def test_jsonp_should_return_jsonp_with_param
get "/api/json?jsonp=fooberz"
assert_equal 200, last_response.status
assert_equal 'application/json', last_response.content_type
- assert_match /^fooberz\(/, last_response.body
-
+ assert_match /^fooberz\(/, last_response.body
end
def test_should_not_barf_when_no_build

0 comments on commit 46c559e

Please sign in to comment.
Something went wrong with that request. Please try again.