Class: RqQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/rq_queue.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRqQueue

Returns a new instance of RqQueue.



103
104
105
# File 'lib/rq_queue.rb', line 103

def initialize
  self.logger = self.class.logger
end

Class Attribute Details

.benchmarkObject

Returns the value of attribute benchmark.



100
101
102
# File 'lib/rq_queue.rb', line 100

def benchmark
  @benchmark
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/rq_queue.rb', line 10

def logger
  @logger
end

Class Method Details

.add_event(method_name, *args) ⇒ Object



30
31
32
# File 'lib/rq_queue.rb', line 30

def self.add_event(method_name, *args)
  Resque.enqueue(self, method_name.to_s, args)
end

.add_event_in(times, method_name, *args) ⇒ Object

For resque-scheduler



45
46
47
# File 'lib/rq_queue.rb', line 45

def self.add_event_in(times, method_name, *args)
  Resque.enqueue_in(times, self, method_name.to_s, args)
end

.enqueue(method_name, *args) ⇒ Object



34
35
36
# File 'lib/rq_queue.rb', line 34

def self.enqueue(method_name, *args)
  add_event method_name, *args
end

.enqueue_in(times, method_name, *args) ⇒ Object

For resque-scheduler



50
51
52
# File 'lib/rq_queue.rb', line 50

def self.enqueue_in(times, method_name, *args)
  add_event_in(times, method_name, *args)
end

.extract_queue_nameObject



16
17
18
# File 'lib/rq_queue.rb', line 16

def self.extract_queue_name
  name.gsub(/^Rq/, '').underscore.gsub('/', '-').to_sym rescue :default
end

.inherited(subclass) ⇒ Object



12
13
14
# File 'lib/rq_queue.rb', line 12

def self.inherited(subclass)
  subclass.set_queue_name(subclass.extract_queue_name)
end

.instanceObject



64
65
66
# File 'lib/rq_queue.rb', line 64

def self.instance
  @instance ||= self.new
end

.loggerObject



56
57
58
59
60
61
62
# File 'lib/rq_queue.rb', line 56

def self.logger
  @logger ||= Logger.new(logger_path).tap do |logger|
    logger.formatter = lambda { |s, d, p, m| "#{d.strftime("%d.%m.%Y %H:%M:%S")} #{m}\n" }
    # Rails.logger = logger if defined?(Rails)
    # ActiveRecord::Base.logger = logger if defined?(ActiveRecord)
  end
end

.logger_pathObject



94
95
96
97
98
# File 'lib/rq_queue.rb', line 94

def logger_path 
  @logger_path ||= begin        
    "#{Rails.root}/log/workers/#{extract_queue_name}.log"
  end
end

.logger_path=(_logger_path) ⇒ Object



90
91
92
# File 'lib/rq_queue.rb', line 90

def logger_path=(_logger_path)
  @logger_path = _logger_path
end

.method_missing(method, *args) ⇒ Object



38
39
40
# File 'lib/rq_queue.rb', line 38

def self.method_missing(method, *args)
  add_event(method, *args)
end

.notify_about_error(exception) ⇒ Object



111
112
113
# File 'lib/rq_queue.rb', line 111

def self.notify_about_error(exception)
  # stub
end

.perform(method_name, args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rq_queue.rb', line 68

def self.perform(method_name, args)
  start_time = benchmark ? Time.now : nil
  
  instance.send(method_name, *args)
  
  logger.info "done #{method_name}, #{"%.6f" % (Time.now - start_time)} s" if benchmark
 
rescue => ex
  logger.error "!Failed event: #{method_name} => #{ex.message}"    
  notify_about_error(ex)
  raise ex
end

.proxy(method_name) ⇒ Object

proxing method for tests



82
83
84
85
86
87
# File 'lib/rq_queue.rb', line 82

def self.proxy(method_name)
  self.should_receive(method_name) do |*data|
    x = ::Resque.decode(::Resque.encode(data))
    self.instance.send(method_name, *x)
  end.any_number_of_times
end

.queue_nameObject



24
25
26
# File 'lib/rq_queue.rb', line 24

def self.queue_name
  @queue
end

.set_queue_name(queue_name) ⇒ Object



20
21
22
# File 'lib/rq_queue.rb', line 20

def self.set_queue_name(queue_name)
  self.instance_variable_set('@queue', queue_name.to_sym)
end

Instance Method Details

#queue_nameObject



107
108
109
# File 'lib/rq_queue.rb', line 107

def queue_name
  self.class.queue_name
end