right_scraper 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -71,35 +71,46 @@ module RightScale
71
71
  # === Return
72
72
  # res(RightScale::WatchStatus):: Outcome of watch, see RightScale::WatchStatus
73
73
  def launch_and_watch(cmd, dest_dir)
74
- status = nil
74
+ exit_code = nil
75
75
  output = ''
76
76
 
77
77
  # Run external process and monitor it in a new thread
78
- r = IO.popen(cmd)
79
- Thread.new do
80
- Process.wait(r.pid)
81
- status = $?
78
+ io = IO.popen(cmd)
79
+ reader = Thread.new do
80
+ o = io.read
81
+ until o == ''
82
+ output += o
83
+ o = io.read
84
+ end
85
+ Process.wait(io.pid)
86
+ exit_code = $?.exitstatus
82
87
  end
83
88
 
84
89
  # Loop until process is done or times out or takes too much space
85
90
  timed_out = repeat(1, @max_seconds) do
86
- output += r.readlines.join
87
91
  if @max_bytes < 0
88
- status
92
+ exit_code
89
93
  else
90
94
  size = 0
91
- Find.find(dest_dir) { |f| size += File.stat(f).size unless File.directory?(f) } if File.directory?(dest_dir)
92
- size > @max_bytes || status
95
+ Find.find(dest_dir) { |f| size += File.stat(f).size rescue 0 if File.file?(f) } if File.directory?(dest_dir)
96
+ size > @max_bytes || exit_code
93
97
  end
94
98
  end
95
99
 
96
100
  # Cleanup and report status
97
- output += r.readlines.join
98
- Process.kill('TERM', r.pid) unless status
99
- r.close
100
- s = status ? :success : (timed_out ? :timeout : :size_exceeded)
101
- exit_code = status && status.exitstatus || -1
102
- res = WatchStatus.new(s, exit_code, output)
101
+ # Note: We need to store the exit status before we kill the underlying process so that
102
+ # if it finished in the mean time we still report -1 as exit code
103
+ if exit_code
104
+ exit_status = exit_code
105
+ outcome = :success
106
+ else
107
+ exit_status = -1
108
+ outcome = (timed_out ? :timeout : :size_exceeded)
109
+ Process.kill('INT', io.pid)
110
+ end
111
+ reader.join
112
+ io.close
113
+ res = WatchStatus.new(outcome, exit_status, output)
103
114
  end
104
115
 
105
116
  protected
@@ -23,7 +23,7 @@ require 'rubygems'
23
23
 
24
24
  spec = Gem::Specification.new do |spec|
25
25
  spec.name = 'right_scraper'
26
- spec.version = '1.0.6'
26
+ spec.version = '1.0.7'
27
27
  spec.authors = ['Raphael Simon']
28
28
  spec.email = 'raphael@rightscale.com'
29
29
  spec.homepage = 'https://github.com/rightscale/right_scraper'
@@ -66,10 +66,12 @@ describe RightScale::SvnScraper do
66
66
  end
67
67
 
68
68
  before(:each) do
69
+ file_prefix = 'file://'
70
+ file_prefix += '/' if RUBY_PLATFORM =~ /mswin/
69
71
  @scraper = RightScale::SvnScraper.new(@repo_path, max_bytes=1024**2, max_seconds=20)
70
72
  @repo = RightScale::Repository.from_hash(:display_name => 'test repo',
71
73
  :repo_type => :svn,
72
- :url => "file://#{@svn_repo_path}")
74
+ :url => "#{file_prefix}#{@svn_repo_path}")
73
75
  end
74
76
 
75
77
  after(:all) do
data/spec/watcher_spec.rb CHANGED
@@ -45,7 +45,7 @@ describe RightScale::Watcher do
45
45
 
46
46
  it 'should report timeouts' do
47
47
  watcher = RightScale::Watcher.new(max_bytes=1, max_seconds=2)
48
- status = watcher.launch_and_watch('ruby -e "puts 42; sleep 3"', @dest_dir)
48
+ status = watcher.launch_and_watch('ruby -e "STDOUT.sync = true; puts 42; sleep 5"', @dest_dir)
49
49
  status.status.should == :timeout
50
50
  status.exit_code.should == -1
51
51
  status.output.should == "42\n"
@@ -53,7 +53,7 @@ describe RightScale::Watcher do
53
53
 
54
54
  it 'should report size exceeded' do
55
55
  watcher = RightScale::Watcher.new(max_bytes=1, max_seconds=5)
56
- status = watcher.launch_and_watch("ruby -e 'puts 42; File.open(File.join(\"#{@dest_dir}\", \"test\"), \"w\") { |f| f.puts \"MORE THAN 2 CHARS\" }'", @dest_dir)
56
+ status = watcher.launch_and_watch("ruby -e 'STDOUT.sync = true; puts 42; File.open(File.join(\"#{@dest_dir}\", \"test\"), \"w\") { |f| f.puts \"MORE THAN 2 CHARS\" }'; sleep 5", @dest_dir)
57
57
  status.status.should == :size_exceeded
58
58
  status.exit_code.should == -1
59
59
  status.output.should == "42\n"
@@ -61,7 +61,7 @@ describe RightScale::Watcher do
61
61
 
62
62
  it 'should allow infinite size and timeout' do
63
63
  watcher = RightScale::Watcher.new(max_bytes=-1, max_seconds=-1)
64
- status = watcher.launch_and_watch("ruby -e 'puts 42; File.open(File.join(\"#{@dest_dir}\", \"test\"), \"w\") { |f| f.puts \"MORE THAN 2 CHARS\" };sleep 2'", @dest_dir)
64
+ status = watcher.launch_and_watch("ruby -e 'STDOUT.sync = true; puts 42; File.open(File.join(\"#{@dest_dir}\", \"test\"), \"w\") { |f| f.puts \"MORE THAN 2 CHARS\" };sleep 2'", @dest_dir)
65
65
  status.status.should == :success
66
66
  status.exit_code.should == 0
67
67
  status.output.should == "42\n"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_scraper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Simon