module TResque::Worker::ClassMethods

Public Instance Methods

app_key() click to toggle source
# 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) click to toggle source
# File lib/tresque/worker.rb, line 27
def application(app_key)
  @app_key = Util.normalize(app_key)
end
enqueue(options = {}) click to toggle source
# 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) click to toggle source
# File lib/tresque/worker.rb, line 23
def full_queue(name)
  @queue = name.to_s
end
get_lock_namespace(options) click to toggle source
# File lib/tresque/worker.rb, line 150
def get_lock_namespace(options)
  @lock_namespace ||= self.name
end
get_queue_lock_attributes(options) click to toggle source
# File lib/tresque/worker.rb, line 154
def get_queue_lock_attributes(options)
  @queue_lock_attributes  ||= []
end
get_worker_lock_attributes(options) click to toggle source
# File lib/tresque/worker.rb, line 158
def get_worker_lock_attributes(options)
  @worker_lock_attributes ||= []
end
input(*args)
Alias for: inputs
inputs(*args) click to toggle source
# File lib/tresque/worker.rb, line 121
def inputs(*args)
  args.each do |name|
    define_method name do
      enqueued_options[name]
    end
  end
end
Also aliased as: input
lock_namespace(val) click to toggle source
# File lib/tresque/worker.rb, line 134
def lock_namespace(val)
  @lock_namespace = val.to_s
end
on_failure_aaa(exception, *args) click to toggle source

make sure we put it back in the same queue @failure_hooks_already_ran on github.com/defunkt/resque/tree/1-x-stable to prevent running twice

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/tresque/worker.rb, line 162
def queue_lock_key(options)
  options_lock_key(options, get_queue_lock_attributes(options))
end
turn_retry_off() click to toggle source
# File lib/tresque/worker.rb, line 130
def turn_retry_off
  @retry_limit = 0
end
worker_lock(*args) click to toggle source
# 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) click to toggle source
# File lib/tresque/worker.rb, line 166
def worker_lock_key(options)
  options_lock_key(options, get_worker_lock_attributes(options))
end