Class: BuildBuddy::Recorder

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Internals::Logger
Defined in:
lib/build_buddy/recorder.rb

Constant Summary collapse

LIMIT =
50

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



16
17
18
19
20
21
22
# File 'lib/build_buddy/recorder.rb', line 16

def initialize()
  Mongo::Logger.logger.level = ::Logger::FATAL
  mongo_uri = BuildBuddy::Config.mongo_uri
  @mongo ||= Mongo::Client.new(mongo_uri)
  info "Connected to MongoDB at '#{mongo_uri}'"
  @mongo[:builds].indexes.create_one({:create_time => -1}, :name => "reverse_order")
end

Instance Method Details

#find_report_uriObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/build_buddy/recorder.rb', line 60

def find_report_uri
  uri = nil
  @mongo[:builds].find().sort(:create_time => -1).each do |doc|
    file_name = File.join(Config.build_output_dir, doc[:_id].to_s, "report.html")
    if File.exist?(file_name)
      uri = BuildData.server_report_uri(doc[:_id])
      break
    end
  end
  uri
end

#get_build_data(id) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/build_buddy/recorder.rb', line 46

def get_build_data(id)
  doc = @mongo[:builds].find({ :_id => BSON::ObjectId.from_string(id) }, { :limit => 1 }).first
  if doc.nil?
    return nil
  end
  BuildData.new(doc)
end

#get_build_data_history(limit) ⇒ Object



54
55
56
57
58
# File 'lib/build_buddy/recorder.rb', line 54

def get_build_data_history(limit)
  @mongo[:builds].find().sort(:create_time => -1).limit(limit).map do |doc|
    BuildData.new(doc)
  end
end

#record_build_data_and_start_build(build_data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/build_buddy/recorder.rb', line 24

def record_build_data_and_start_build(build_data)
  builds = @mongo[:builds]
  begin
    # Do this to prevent build _id's from being sequential and so reduce risk
    # of someone guessing a valid build URL.
    build_data._id = BSON::ObjectId.from_string(SecureRandom.hex(12).to_s)
    builds.insert_one(build_data.to_h)
  rescue Mongo::Error::OperationFailure => e
    retry if e.to_s.start_with?('E11000') # Duplicate key error
  end

  Celluloid::Actor[:builder].async.start_build(build_data)
end

#update_build_data(build_data) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/build_buddy/recorder.rb', line 38

def update_build_data(build_data)
  if build_data._id.nil?
    return
  end

  @mongo[:builds].replace_one({ :_id => build_data._id }, build_data.to_h)
end