Module: Katello::Util::Support

Defined in:
app/lib/katello/util/support.rb

Class Method Summary collapse

Class Method Details

.active_record_retry(retries = 3) ⇒ Object

Used for retrying active record transactions when race conditions could cause

RecordNotUnique exceptions


96
97
98
99
100
101
102
103
104
105
# File 'app/lib/katello/util/support.rb', line 96

def self.active_record_retry(retries = 3)
  yield
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation, ActiveRecord::RecordInvalid => e
  retries -= 1
  if retries == 0
    raise e
  else
    retry
  end
end

.active_record_retry_connect(logger) ⇒ Object



86
87
88
89
90
91
92
# File 'app/lib/katello/util/support.rb', line 86

def self.active_record_retry_connect(logger)
  sleep 3
  ActiveRecord::Base.connection.reconnect!
rescue
  logger.error("Trying to reconnect to the database.")
  retry
end

.array_with_total(a = []) ⇒ Object

We need this so that we can return empty search results on an invalid query Basically this is a empty array with a total method.



111
112
113
114
115
116
# File 'app/lib/katello/util/support.rb', line 111

def self.array_with_total(a = [])
  def a.total
    size
  end
  a
end

.deep_copy(object) ⇒ Object



4
5
6
# File 'app/lib/katello/util/support.rb', line 4

def self.deep_copy(object)
  Marshal.load(Marshal.dump(object))
end

.diff_hash_params(rule, params) ⇒ Object

Given a rules hash in the format {<attrib_name> => …} the method will match the params attributes to provided rule and return a diff. Here are some of the examples rule -> => [[:name, :version, :min_version, :max_version]] will match -> => [{:name = > “boo”, :version => “2.0”,

{:name = > "Foo", :min_version => "2.0"}]}

rule -> => [[:id]], :date_range => [:start, :end],

:errata_type => {, :severity => {}}

will match -> {:units => [{:id => 100}],

:date_range => => "05/14/2011"}

Note of caution this merely shows differences in the structure of params vs rules. It doesnt validate anything. Look at SerializedParamsValidator method for its uses.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/lib/katello/util/support.rb', line 47

def self.diff_hash_params(rule, params)
  params = params.with_indifferent_access
  if rule.is_a?(Array)
    return stringify(params.keys) - stringify(rule)
  end

  rule = rule.with_indifferent_access
  diff_data = rule.keys.collect do |k|
    if params[k]
      if params[k].is_a?(Array) && rule[k].first.is_a?(Array)
        diffs = params[k].collect { |pk| diff_hash_params(rule[k].first, pk) }.flatten
        diffs
      elsif params[k].is_a?(Hash)
        keys = stringify(params[k].keys) - stringify(rule[k])
        if keys.empty?
          nil
        else
          {k => keys}
        end
      end
    end
  end
  diff_data = diff_data.compact.flatten

  return diff_data unless diff_data.blank?
  stringify(params.keys) - stringify(rule.keys)
end

.scrub(params, &block_to_match) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'app/lib/katello/util/support.rb', line 14

def self.scrub(params, &block_to_match)
  params.each_key do |key|
    if params[key].is_a?(Hash)
      scrub(params[key], &block_to_match)
    elsif block_to_match.call(key, params[key])
      params[key] = "[FILTERED]"
    end
  end
  params
end

.stringify(col) ⇒ Object

Helper method to just convert a collection of objects to their string representation (mostly to be used internally)



28
29
30
# File 'app/lib/katello/util/support.rb', line 28

def self.stringify(col)
  col.collect { |c| c.to_s }
end

.timeObject



8
9
10
11
12
# File 'app/lib/katello/util/support.rb', line 8

def self.time
  a = Time.now
  yield
  Time.now - a
end

.with_db_connection(logger = Rails.logger) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'app/lib/katello/util/support.rb', line 75

def self.with_db_connection(logger = Rails.logger)
  yield
rescue PG::ConnectionBad, ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished => e
  logger.error(e.message)
  logger.error("Lost database connection. Attempting reconnect.")

  active_record_retry_connect(logger)

  retry
end