Skip to content

Commit

Permalink
moved start_time for a build into command.
Browse files Browse the repository at this point in the history
  • Loading branch information
srushti committed Nov 9, 2012
1 parent 0bf99ca commit 2063936
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
13 changes: 5 additions & 8 deletions app/models/build.rb
Expand Up @@ -58,24 +58,21 @@ def run
end

def execute_async(command)
start_time = DateTime.now
command = Command.new(command)
command.fork
while (!exceeded_timeout?(start_time) && command.running? && !reload.cancelled?)
while (!exceeded_timeout?(command.start_time) && command.running? && !reload.cancelled?)
sleep(10)
end
if cancelled?
command.stop_tree
Goldberg.logger.info "Cancelled pid #{command.pid}, build no #{number} of #{project.name}, #{command.stopped?}"
end
if exceeded_timeout?(start_time)
elsif exceeded_timeout?(command.start_time)
command.stop_tree
Goldberg.logger.error "Timeout (#{project.timeout})- killing #{command.pid}:#{command.cmd}"
self.status = 'timeout'
elsif !cancelled?
self.status = command.success? ? 'passed' : 'failed'
update_attributes(status: 'timeout')
else
update_attributes(status: command.success? ? 'passed' : 'failed')
end
save
end

def exceeded_timeout?(start_time)
Expand Down
8 changes: 7 additions & 1 deletion app/models/command.rb
@@ -1,5 +1,6 @@
class Command
attr_reader :cmd
attr_reader :cmd, :start_time

def initialize(cmd)
@cmd = cmd
end
Expand All @@ -21,6 +22,7 @@ def execute
end

def fork
@start_time = DateTime.now
command = %{/usr/bin/env bash -c "#{@cmd.gsub(/"/, '\"')}"}
Goldberg.logger.info "Forking: #{command}"
@process = ChildProcess.build(command)
Expand All @@ -31,6 +33,10 @@ def running?
@process.alive?
end

def finished?
!running?
end

def stop
@process.stop
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/build_spec.rb
Expand Up @@ -138,13 +138,13 @@
end

it "sets build status to failed if the build command succeeds" do
Command.stub(:new).and_return(mock(Command, :execute => true, :running? => false, :fork => nil, :success? => true))
Command.stub(:new).and_return(mock(Command, :execute => true, :running? => false, :fork => nil, :success? => true, :start_time => DateTime.now))
build.run
build.status.should == "passed"
end

it "sets build status to failed if the build command fails" do
Command.stub(:new).and_return(mock(:command, :execute => true, :running? => false, :fork => nil, :success? => false))
Command.stub(:new).and_return(mock(:command, :execute => true, :running? => false, :fork => nil, :success? => false, :start_time => DateTime.now))
build.run
build.status.should == "failed"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/expectation_helpers.rb
@@ -1,7 +1,7 @@
module Goldberg
module ExpectationHelpers
def expect_command(command, stubs = {})
command.tap{|c| Command.should_receive(:new).with(c).and_return(c = mock(Command, stubs)) }
command.tap{|c| Command.should_receive(:new).with(c).and_return(c = mock(Command, { start_time: DateTime.now }.merge(stubs))) }
end
end
end

0 comments on commit 2063936

Please sign in to comment.