Skip to content
Browse files

Properly queue build requests with branch names.

Previously, if a branch was requested to be built while another one was running, cijoe would just build the default branch upon finishing (as the branch name wasn't stored).
  • Loading branch information...
1 parent f9b7670 commit 9f8e225831ed1acf142107c1257d1e72acc7b925 Matt Colyer committed Mar 31, 2011
Showing with 78 additions and 22 deletions.
  1. +3 −3 README.md
  2. +4 −19 lib/cijoe.rb
  3. +43 −0 lib/cijoe/queue.rb
  4. +28 −0 test/test_cijoe_queue.rb
View
6 README.md
@@ -58,13 +58,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.
@@ -172,4 +172,4 @@ Screenshots
Questions? Concerns?
---------------------------------
-[Issues](http://github.com/defunkt/cijoe/issues)
+[Issues](http://github.com/defunkt/cijoe/issues)
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
@@ -31,6 +32,7 @@ def initialize(project_path)
@last_build = nil
@current_build = nil
+ @queue = Queue.new(!repo_config.buildqueue.empty?)
trap("INT") { stop }
end
@@ -47,12 +49,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
@@ -79,25 +75,14 @@ def finish_build(status, output)
write_build 'last', @last_build
@last_build.notify if @last_build.respond_to? :notify
- # 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
43 lib/cijoe/queue.rb
@@ -0,0 +1,43 @@
+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)
+ @enabled = enabled
+ @queue = []
+ 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?
+ @queue << branch unless @queue.include? branch
+ end
+
+ # Returns a String of the next branch to build
+ def next_branch_to_build
+ @queue.shift
+ 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 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

0 comments on commit 9f8e225

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