Module: TResque::Worker::ClassMethods

Defined in:
lib/tresque/worker.rb

Instance Method Summary collapse

Instance Method Details

#app_keyObject



31
32
33
34
# File 'lib/tresque/worker.rb', line 31

def app_key
  return @app_key if @app_key
  @app_key = Util.calculate_namespace_from_class(self)
end

#application(app_key) ⇒ Object



27
28
29
# File 'lib/tresque/worker.rb', line 27

def application(app_key)
  @app_key = Util.normalize(app_key)
end

#enqueue(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
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
74
75
76
77
78
79
80
81
# File 'lib/tresque/worker.rb', line 36

def enqueue(options = {})
  options = options.with_indifferent_access

  options[:locale] ||= I18n.locale.to_s
  options[:tz]     ||= Time.zone.name

  run_at = options.delete(:run_at)
  if options[:full_queue]
    queue_name = options[:full_queue]
  elsif options[:queue] || options[:queue_namespace]
    namespace = options[:queue_namespace] || self.app_key
    queue = options[:queue] || "default"
    queue_name = "#{namespace}_#{queue}"
  else
    queue_name = self.queue
  end

  if queue_name == "t_resque_default"
    message = "QUEUE_ERROR (#{self.class.name}): #{queue_name} will not be worked!"
    Rails.logger.error(message)
    puts message if Rails.env.test?
  end

  if !TResque::Worker.skip_check_queues && !TResque::Registry.queues.include?(queue_name)
    message = "QUEUE_ERROR (#{self.class.name}): #{queue_name} will not be worked!"
    Rails.logger.error(message)
    puts message if Rails.env.test?
  end

  options[:full_queue] = queue_name
  if run_at
    Resque.enqueue_at_with_queue(queue_name, run_at, self, options)
  else
    Resque.enqueue_to(queue_name, self, options)
  end

  # too many events
  # QueueBus.publish_log(:worker_enqueued, {
  #   options: options,
  #   queue_name:  queue_name,
  #   worker_name: self.name.to_s,
  #   run_at: run_at.to_i
  # }) unless Rails.env.test?

  options
end

#full_queue(name) ⇒ Object



23
24
25
# File 'lib/tresque/worker.rb', line 23

def full_queue(name)
  @queue = name.to_s
end

#get_lock_namespace(options) ⇒ Object



150
151
152
# File 'lib/tresque/worker.rb', line 150

def get_lock_namespace(options)
  @lock_namespace ||= self.name
end

#get_queue_lock_attributes(options) ⇒ Object



154
155
156
# File 'lib/tresque/worker.rb', line 154

def get_queue_lock_attributes(options)
  @queue_lock_attributes  ||= []
end

#get_worker_lock_attributes(options) ⇒ Object



158
159
160
# File 'lib/tresque/worker.rb', line 158

def get_worker_lock_attributes(options)
  @worker_lock_attributes ||= []
end

#inputs(*args) ⇒ Object Also known as: input



121
122
123
124
125
126
127
# File 'lib/tresque/worker.rb', line 121

def inputs(*args)
  args.each do |name|
    define_method name do
      enqueued_options[name]
    end
  end
end

#lock_namespace(val) ⇒ Object



134
135
136
# File 'lib/tresque/worker.rb', line 134

def lock_namespace(val)
  @lock_namespace = val.to_s
end

#on_failure_aaa(exception, *args) ⇒ Object

make sure we put it back in the same queue to prevent running twice



188
189
190
191
192
# File 'lib/tresque/worker.rb', line 188

def on_failure_aaa(exception, *args)
  # note: sorted alphabetically
  # queue needs to be set for rety to work (know what queue in Requeue.class_to_queue)
  @requeue_in_queue = args[0]["full_queue"]
end

#on_failure_zzz(exception, *args) ⇒ Object



194
195
196
197
# File 'lib/tresque/worker.rb', line 194

def on_failure_zzz(exception, *args)
  # note: sorted alphabetically
  @requeue_in_queue = nil
end

#options_lock_key(options, keys) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tresque/worker.rb', line 170

def options_lock_key(options, keys)
  return nil unless keys  # not actually locking

  keys = ["all"] if keys.size == 0
  keys = options.keys if keys.size == 1 && keys.first == "all"
  keys.sort!

  vals = [get_lock_namespace(options)]
  keys.each do |key|
    vals << key
    vals << options[key].to_s
  end
  Digest::SHA1.hexdigest(vals.join("-"))
end

#perform(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tresque/worker.rb', line 83

def perform(options)
  Waistband.clear_logs if Waistband.config.logging

  @previous_locale, @previous_zone = I18n.locale, Time.zone

  options = options.with_indifferent_access
  obj = self.new(options.except(:locale, :tz, :bus_locale, :bus_timezone))

  locale = obj.respond_to?(:calculate_locale, true) ? obj.send(:calculate_locale) : nil
  locale ||= options[:locale]
  locale ||= options[:bus_locale]
  locale ||= I18n.locale if Rails.env.production?  # don't crash in production, use default

  zone = obj.respond_to?(:calculate_timezone, true) ? obj.send(:calculate_timezone) : nil
  zone ||= options[:tz]
  zone ||= options[:bus_timezone]

  I18n.locale = locale
  Time.zone   = zone

  # too many events
  # QueueBus.publish_log(:worker_perform, {
  #   options: options,
  #   worker_name: self.name.to_s,
  #   locale:  locale,
  #   time_zone: zone
  # }) unless Rails.env.test?

  obj.worker_perform
rescue Resque::Job::DontPerform
  # it's cool
ensure
  # write waistband logs
  Waistband.write_logs(nil) if Waistband.config.logging
  # reset
  I18n.locale, Time.zone = @previous_locale, @previous_zone
end

#queue(name = nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/tresque/worker.rb', line 16

def queue(name=nil)
  return @requeue_in_queue if !name && @requeue_in_queue
  return @queue if !name && @queue
  name ||= :default
  full_queue("#{app_key}_#{name}")
end

#queue_lock(*args) ⇒ Object



144
145
146
147
148
# File 'lib/tresque/worker.rb', line 144

def queue_lock(*args)
  raise ("queue_lock: what should i lock on?") if args.size == 0
  extend ::TResque::QueueLock
  @queue_lock_attributes = args.collect(&:to_s)
end

#queue_lock_key(options) ⇒ Object



162
163
164
# File 'lib/tresque/worker.rb', line 162

def queue_lock_key(options)
  options_lock_key(options, get_queue_lock_attributes(options))
end

#turn_retry_offObject



130
131
132
# File 'lib/tresque/worker.rb', line 130

def turn_retry_off
  @retry_limit = 0
end

#worker_lock(*args) ⇒ Object



138
139
140
141
142
# File 'lib/tresque/worker.rb', line 138

def worker_lock(*args)
  raise ("worker_lock: what should i lock on?") if args.size == 0
  extend ::TResque::WorkerLock
  @worker_lock_attributes = args.collect(&:to_s)
end

#worker_lock_key(options) ⇒ Object



166
167
168
# File 'lib/tresque/worker.rb', line 166

def worker_lock_key(options)
  options_lock_key(options, get_worker_lock_attributes(options))
end