Module: Backburner::Helpers
Class Method Summary collapse
-
.included(base) ⇒ Object
Loads in instance and class levels.
Instance Method Summary collapse
-
#classify(dashed_word) ⇒ Object
Given a word with dashes, returns a camel cased version of it.
-
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:.
-
#dasherize(word) ⇒ Object
Given a class, dasherizes the name, used for getting tube names.
-
#exception_message(e) ⇒ Object
Prints out exception_message based on specified e.
-
#expand_tube_name(tube) ⇒ Object
Expands a tube to include the prefix.
-
#queue_config ⇒ Object
Returns configuration options for backburner.
-
#resolve_max_job_retries(retries) ⇒ Object
Resolves max retries based on the value given.
-
#resolve_priority(pri) ⇒ Object
Resolves job priority based on the value given.
-
#resolve_respond_timeout(ttr) ⇒ Object
Resolves job respond timeout based on the value given.
-
#resolve_retry_delay(delay) ⇒ Object
Resolves retry delay based on the value given.
-
#resolve_retry_delay_proc(proc) ⇒ Object
Resolves retry delay proc based on the value given.
Class Method Details
.included(base) ⇒ Object
Loads in instance and class levels
4 5 6 |
# File 'lib/backburner/helpers.rb', line 4 def self.included(base) base.extend self end |
Instance Method Details
#classify(dashed_word) ⇒ Object
Given a word with dashes, returns a camel cased version of it.
25 26 27 |
# File 'lib/backburner/helpers.rb', line 25 def classify(dashed_word) dashed_word.to_s.split('-').each { |part| part[0] = part[0].chr.upcase }.join end |
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:
NameError is raised when the constant is unknown.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/backburner/helpers.rb', line 48 def constantize(camel_cased_word) camel_cased_word = camel_cased_word.to_s if camel_cased_word.include?('-') camel_cased_word = classify(camel_cased_word) end names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| args = Module.method(:const_get).arity != 1 ? [false] : [] if constant.const_defined?(name, *args) constant = constant.const_get(name) else constant = constant.const_missing(name) end end constant end |
#dasherize(word) ⇒ Object
Given a class, dasherizes the name, used for getting tube names
34 35 36 37 38 39 |
# File 'lib/backburner/helpers.rb', line 34 def dasherize(word) classify(word).to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("_", "-").downcase end |
#exception_message(e) ⇒ Object
Prints out exception_message based on specified e
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/backburner/helpers.rb', line 9 def (e) msg = [ "Exception #{e.class} -> #{e.}" ] base = File.(Dir.pwd) + '/' e.backtrace.each do |t| msg << " #{File.(t).gsub(/#{base}/, '')}" end if e.backtrace msg.join("\n") end |
#expand_tube_name(tube) ⇒ Object
Expands a tube to include the prefix
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/backburner/helpers.rb', line 87 def (tube) prefix = queue_config.tube_namespace separator = queue_config.namespace_separator queue_name = if tube.is_a?(String) tube elsif tube.respond_to?(:queue) # use queue name queue = tube.queue queue.is_a?(Proc) ? queue.call(tube) : queue elsif tube.is_a?(Proc) tube.call elsif tube.is_a?(Class) # no queue name, use default queue_config.primary_queue # tube.name else # turn into a string tube.to_s end [prefix.gsub(/\.$/, ''), dasherize(queue_name).gsub(/^#{prefix}/, '')].join(separator).gsub(/#{Regexp::escape(separator)}+/, separator).split(':').first end |
#queue_config ⇒ Object
Returns configuration options for backburner
76 77 78 |
# File 'lib/backburner/helpers.rb', line 76 def queue_config Backburner.configuration end |
#resolve_max_job_retries(retries) ⇒ Object
Resolves max retries based on the value given. Can be integer, a class or nothing
148 149 150 151 152 153 154 155 156 |
# File 'lib/backburner/helpers.rb', line 148 def resolve_max_job_retries(retries) if retries.respond_to?(:queue_max_job_retries) resolve_max_job_retries(retries.queue_max_job_retries) elsif retries.is_a?(Integer) # numerical retries else # default Backburner.configuration.max_job_retries end end |
#resolve_priority(pri) ⇒ Object
Resolves job priority based on the value given. Can be integer, a class or nothing
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/backburner/helpers.rb', line 112 def resolve_priority(pri) if pri.respond_to?(:queue_priority) resolve_priority(pri.queue_priority) elsif pri.is_a?(String) || pri.is_a?(Symbol) # named priority resolve_priority(Backburner.configuration.priority_labels[pri.to_sym]) elsif pri.is_a?(Integer) # numerical pri else # default Backburner.configuration.default_priority end end |
#resolve_respond_timeout(ttr) ⇒ Object
Resolves job respond timeout based on the value given. Can be integer, a class or nothing
131 132 133 134 135 136 137 138 139 |
# File 'lib/backburner/helpers.rb', line 131 def resolve_respond_timeout(ttr) if ttr.respond_to?(:queue_respond_timeout) resolve_respond_timeout(ttr.queue_respond_timeout) elsif ttr.is_a?(Integer) # numerical ttr else # default Backburner.configuration.respond_timeout end end |
#resolve_retry_delay(delay) ⇒ Object
Resolves retry delay based on the value given. Can be integer, a class or nothing
165 166 167 168 169 170 171 172 173 |
# File 'lib/backburner/helpers.rb', line 165 def resolve_retry_delay(delay) if delay.respond_to?(:queue_retry_delay) resolve_retry_delay(delay.queue_retry_delay) elsif delay.is_a?(Integer) # numerical delay else # default Backburner.configuration.retry_delay end end |
#resolve_retry_delay_proc(proc) ⇒ Object
Resolves retry delay proc based on the value given. Can be proc, a class or nothing
182 183 184 185 186 187 188 189 190 |
# File 'lib/backburner/helpers.rb', line 182 def resolve_retry_delay_proc(proc) if proc.respond_to?(:queue_retry_delay_proc) resolve_retry_delay_proc(proc.queue_retry_delay_proc) elsif proc.is_a?(Proc) proc else # default Backburner.configuration.retry_delay_proc end end |