Class: Bricolage::LogLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/loglocator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, s3_writer) ⇒ LogLocator

Returns a new instance of LogLocator.



7
8
9
10
# File 'lib/bricolage/loglocator.rb', line 7

def initialize(path, s3_writer)
  @path = path
  @s3_writer = s3_writer
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/bricolage/loglocator.rb', line 12

def path
  @path
end

Class Method Details

.emptyObject



3
4
5
# File 'lib/bricolage/loglocator.rb', line 3

def LogLocator.empty
  new(nil, nil)
end

.slice_last_stderr(re, nth = 0) ⇒ Object

CLUDGE: FIXME: We redirect stderr to the file, we can find error messages from there. Using a temporary file or Ruby SQL driver is MUCH better.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bricolage/loglocator.rb', line 41

def self.slice_last_stderr(re, nth = 0)
  return unless $stderr.stat.file?
  $stderr.flush
  f = $stderr.dup
  matched = nil
  begin
    f.seek(0)
    f.each do |line|
      m = line.slice(re, nth)
      matched = m if m
    end
  ensure
    f.close
  end
  matched = matched.to_s.strip
  matched.empty? ? nil : matched
end

Instance Method Details

#redirect_stdoutsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bricolage/loglocator.rb', line 19

def redirect_stdouts
  return yield unless @path
  FileUtils.mkdir_p File.dirname(@path)
  @original_stdout = $stdout.dup
  @original_stderr = $stderr.dup
  begin
    # Use 'w+' to make readable for retrieve_last_match_from_stderr
    File.open(@path, 'w+') {|f|
      f.sync = true
      $stdout.reopen f
      $stderr.reopen f
    }
    return yield
  ensure
    $stdout.reopen @original_stdout; @original_stdout.close
    $stderr.reopen @original_stderr; @original_stderr.close
    upload
  end
end

#s3_urlObject



14
15
16
17
# File 'lib/bricolage/loglocator.rb', line 14

def s3_url
  return nil unless @s3_writer
  @s3_writer.url
end

#uploadObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bricolage/loglocator.rb', line 59

def upload
  return unless @path
  return unless @s3_writer
  # FIXME: Shows HTTP URL?
  puts "bricolage: S3 log: #{s3_url}"
  begin
    @s3_writer.upload(path)
  rescue => ex
    puts "warning: S3 upload failed: #{s3_url}"
  end
end