Skip to content

Commit

Permalink
Build in background
Browse files Browse the repository at this point in the history
Do all the building in a new thread, and return control to Sinatra
right away. While a project is building, the project page shows a
'Building...' message with a spinning throbber.
  • Loading branch information
foca committed Nov 16, 2008
1 parent 2f4995a commit 17576db
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/integrity/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ class Project
def build(commit_identifier="HEAD")
return if building?
update_attributes(:building => true)
Builder.new(self).build(commit_identifier)
ensure
update_attributes(:building => false)
send_notifications
Thread.new(self) do |project|
begin
Builder.new(project).build(commit_identifier)
ensure
project.update_attributes(:building => false)
project.send_notifications
end
end
end

def last_build
Expand Down Expand Up @@ -59,6 +63,12 @@ def notifies?(notifier)
def enable_notifiers(*args)
Notifier.enable_notifiers(id, *args)
end

def send_notifications
notifiers.each do |notifier|
notifier.notify_of_build last_build
end
end

private
def set_permalink
Expand All @@ -73,11 +83,5 @@ def delete_code
builds.destroy!
Builder.new(self).delete_code
end

def send_notifications
notifiers.each do |notifier|
notifier.notify_of_build last_build
end
end
end
end
18 changes: 18 additions & 0 deletions spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@
get_it '/integrity'
status.should == 404
end

describe "displaying a 'building' throbber" do
before do
Project.stub!(:first).with(:permalink => "integrity").and_return(mock_project)
end

it "should display the throbber when building" do
mock_project.stub!(:building?).and_return(true)
get_it '/integrity'
body.should have_tag("#building_marker", /building/i)
end

it "should not display the throbber when not building" do
mock_project.stub!(:building?).and_return(false)
get_it '/integrity'
body.should_not have_tag("#building_marker")
end
end

describe 'without builds' do
before(:each) do
Expand Down
6 changes: 6 additions & 0 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def valid_attributes(attributes={})
@project = Integrity::Project.new(:uri => @uri, :branch => 'production', :command => 'rake spec')
@builder = mock('Builder', :build => true)
Integrity::Builder.stub!(:new).and_return(@builder)
Thread.stub!(:new).with(@project).and_yield(@project)
end

it "should offload the building to a new thread" do
Thread.should_receive(:new).with(@project).and_yield(@project)
@project.build
end

it "should not build if it's already building" do
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def mock_project(messages={})
:destroy => nil,
:errors => stub("errors", :on => nil),
:notifies? => false,
:enable_notifiers => nil
:enable_notifiers => nil,
:building? => true
}.merge(messages)

@project ||= stub("project", messages)
Expand Down
11 changes: 11 additions & 0 deletions views/integrity.sass
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ a
.color37
:color #fff

#building_marker
:position absolute
:right 2em
:top .8em
:padding
:top .2em
:bottom .2em
:right 1.2em
:background transparent url(/spinner.gif) no-repeat right
:font-weight bold

#push_url
:display block
:margin
Expand Down
4 changes: 4 additions & 0 deletions views/project.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#administrative
%a{ :href => project_url(@project, :edit) } Edit Project

- if @project.building?
#building_marker
Building...

- if @project.builds.empty?
%form.blank_slate{ :action => project_url(@project, :builds), :method => :post }
%p No builds for this project, buddy
Expand Down

1 comment on commit 17576db

@nakajima
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome

Please sign in to comment.