Skip to content

Commit

Permalink
On invoking Project#build! in-queue builds are pruned
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bugno committed Dec 16, 2010
1 parent a537c8c commit 00f5e92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def recent_build

def build!
new_total_builds = self.total_builds + 1
build = nil
ActiveRecord::Base.transaction do
build = self.builds.create!({:scheduled_at => Time.now, :build_no => new_total_builds})
self.update_attributes!(:total_builds => new_total_builds)
remove_project_jobs_in_queue()
Delayed::Job.enqueue(build)
end
Delayed::Job.enqueue(build)
end

def hooks
Expand Down Expand Up @@ -119,4 +119,16 @@ def update_hooks
Hook.create!(:project => self, :hook_name => name)
end
end

def remove_project_jobs_in_queue
jobs_to_destroy = []
Delayed::Job.all.each do |job|
build = job.payload_object
next unless build.is_a?(Build)
if build.status = Build::STATUS_IN_QUEUE && build.project == self
jobs_to_destroy << job
end
end
jobs_to_destroy.each { |job| job.destroy }
end
end
7 changes: 7 additions & 0 deletions test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ def teardown
assert ! commands.include?('#not5')
end

test "invoking #build! cancels previously queued build" do
project = project_with_steps({:vcs_source => "test/files/repo"}, "true", "true\nfalse")
assert_difference("Delayed::Job.count", +1) do
3.times { project.build! }
end
end

private
def create_project_builds(project, *statuses)
statuses.reverse.each do |status|
Expand Down

0 comments on commit 00f5e92

Please sign in to comment.