Skip to content

Commit

Permalink
added and started using childprocess (we are now a little more multi-…
Browse files Browse the repository at this point in the history
…platform & have slightly cleaner code)
  • Loading branch information
srushti committed May 23, 2011
1 parent 4c2567a commit e9afb4a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem 'sqlite3', '~> 1.3.3', :platforms => :ruby
gem 'haml', '~> 3.0.25' gem 'haml', '~> 3.0.25'
gem 'sass', '~> 3.1.1' gem 'sass', '~> 3.1.1'
gem 'commander', '~> 4.0.4' gem 'commander', '~> 4.0.4'
gem 'childprocess', '~> 0.1.9'


platform :jruby do platform :jruby do
gem 'jdbc-sqlite3', '~> 3.6.0' gem 'jdbc-sqlite3', '~> 3.6.0'
Expand Down
2 changes: 1 addition & 1 deletion app/models/build.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def execute_async(command)
command.kill command.kill
self.status = 'timeout' self.status = 'timeout'
else else
self.status = Command.success? ? 'passed' : 'failed' self.status = command.success? ? 'passed' : 'failed'
end end
save save
end end
Expand Down
12 changes: 7 additions & 5 deletions app/models/command.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ def execute
end end


def execute_async def execute_async
@pid = Process.spawn(%{/usr/bin/env bash -c "#{@cmd.gsub(/"/, '\"')}"}).tap{|pid| Rails.logger.info("The pid of the build process is #{pid}")} @process = ChildProcess.build(%{/usr/bin/env bash -c "#{@cmd.gsub(/"/, '\"')}"})
Rails.logger.info("The pid of the build process is #{@process.pid}")
@process.start
end end


def running? def running?
Process.waitpid(@pid, Process::WNOHANG).nil? @process.alive?
end end


def kill def kill
Process.kill('KILL', @pid) @process.send_kill
end end


def self.success? def success?
($?).success? @process.exit_code == 0
end end
end end
12 changes: 4 additions & 8 deletions spec/models/build_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -109,23 +109,20 @@
Env.should_receive(:[]=).with('BUILD_ARTIFACTS', 'artefacts path') Env.should_receive(:[]=).with('BUILD_ARTIFACTS', 'artefacts path')
Env.should_receive(:[]=).with('RAILS_ENV', nil) Env.should_receive(:[]=).with('RAILS_ENV', nil)
Environment.stub!(:system) Environment.stub!(:system)
Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil)) Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil, :success? => nil))
Command.stub!(:success?)
build.run build.run
end end


it "runs the build command and update the build status" do it "runs the build command and update the build status" do
Environment.stub!(:system) Environment.stub!(:system)
Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil)) Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil, :success? => true))
Command.stub!(:success?).and_return(true)
build.run build.run
build.status.should == "passed" build.status.should == "passed"
end end


it "sets build status to failed if the build command fails" do it "sets build status to failed if the build command fails" do
Environment.stub!(:system) Environment.stub!(:system)
Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil)) Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil, :success? => false))
Command.stub!(:success?).and_return(false)
build.run build.run
build.status.should == "failed" build.status.should == "failed"
end end
Expand All @@ -138,8 +135,7 @@
it "should pass the environment variables to the system command" do it "should pass the environment variables to the system command" do
build.stub(:before_build) build.stub(:before_build)
RVM.stub(:prepare_ruby) RVM.stub(:prepare_ruby)
Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil)) Command.stub!(:new).and_return(mock(:command, :running? => false, :execute_async => nil, :success? => true))
Command.stub!(:success?).and_return(true)
build.run.should be_true build.run.should be_true
end end
end end
Expand Down

0 comments on commit e9afb4a

Please sign in to comment.