Module: Backburner::Helpers

Included in:
Job, Worker
Defined in:
lib/backburner/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

classify('job-name') # => 'JobName'


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.

Examples:

constantize("Module") # => Module
constantize("Test::Unit") # => Test::Unit


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

Examples:

dasherize('JobName') # => "job-name"


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 exception_message(e)
  msg = [ "Exception #{e.class} -> #{e.message}" ]

  base = File.expand_path(Dir.pwd) + '/'
  e.backtrace.each do |t|
    msg << "   #{File.expand_path(t).gsub(/#{base}/, '')}"
  end if e.backtrace

  msg.join("\n")
end

#expand_tube_name(tube) ⇒ Object

Expands a tube to include the prefix

Examples:

expand_tube_name("foo_with_settings:3:100:6") # => <prefix>.foo_with_settings
expand_tube_name("foo") # => <prefix>.foo
expand_tube_name(FooJob) # => <prefix>.foo-job


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 expand_tube_name(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_configObject

Returns configuration options for backburner

Examples:

queue_config.max_job_retries => 3


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

Examples:

resolve_max_job_retries(5) => 5
resolve_max_job_retries(FooBar) => <queue max_job_retries>
resolve_max_job_retries(nil) => <default max_job_retries>


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

Examples:

resolve_priority(1000) => 1000
resolve_priority(FooBar) => <queue priority>
resolve_priority(nil) => <default priority>


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

Examples:

resolve_respond_timeout(1000) => 1000
resolve_respond_timeout(FooBar) => <queue respond_timeout>
resolve_respond_timeout(nil) => <default respond_timeout>


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

Examples:

resolve_retry_delay(5) => 5
resolve_retry_delay(FooBar) => <queue retry_delay>
resolve_retry_delay(nil) => <default retry_delay>


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

Examples:

resolve_retry_delay_proc(proc) => proc
resolve_retry_delay_proc(FooBar) => <queue retry_delay_proc>
resolve_retry_delay_proc(nil) => <default retry_delay_proc>


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