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
Expand Up @@ -6,6 +6,7 @@ gem 'sqlite3', '~> 1.3.3', :platforms => :ruby
gem 'haml', '~> 3.0.25'
gem 'sass', '~> 3.1.1'
gem 'commander', '~> 4.0.4'
gem 'childprocess', '~> 0.1.9'

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

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

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

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

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

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

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

0 comments on commit e9afb4a

Please sign in to comment.