Class: SkQueue
Class Method Summary collapse
- .add_event(method_name, *args) ⇒ Object
- .add_event_in(interval, method_name, *args) ⇒ Object
- .benchmark ⇒ Object
- .benchmark=(val) ⇒ Object
- .enqueue(method_name, *args) ⇒ Object
- .enqueue_in(interval, method_name, *args) ⇒ Object
- .extract_queue_name ⇒ Object
- .inherited(subclass) ⇒ Object
- .logger_path ⇒ Object
-
.method_missing(method_name, *args) ⇒ Object
Worker.some_method(“call new method on Worker async”) Worker.some_method_in(2.minutes.from_now,“call new method on Worker sheduled async”) Worker.some_method_at(2.minutes.from_now,“call new method on Worker sheduled async”).
- .notify_about_error(exception) ⇒ Object
- .proxy(method_name) ⇒ Object
- .queue_name ⇒ Object
- .set_queue_name(val) ⇒ Object
Instance Method Summary collapse
- #benchmark ⇒ Object
- #logger ⇒ Object
- #logger_path ⇒ Object
- #perform(method_name, args) ⇒ Object
- #queue_name ⇒ Object
Class Method Details
.add_event(method_name, *args) ⇒ Object
71 72 73 |
# File 'lib/sk_queue.rb', line 71 def self.add_event(method_name, *args) client_push('class' => self, 'args' => [method_name, args]) end |
.add_event_in(interval, method_name, *args) ⇒ Object
79 80 81 |
# File 'lib/sk_queue.rb', line 79 def self.add_event_in(interval, method_name, *args) perform_in(interval, method_name, args) end |
.benchmark ⇒ Object
35 36 37 |
# File 'lib/sk_queue.rb', line 35 def self.benchmark ['benchmark'] end |
.benchmark=(val) ⇒ Object
39 40 41 |
# File 'lib/sk_queue.rb', line 39 def self.benchmark=(val) :benchmark => val end |
.enqueue(method_name, *args) ⇒ Object
75 76 77 |
# File 'lib/sk_queue.rb', line 75 def self.enqueue(method_name, *args) add_event method_name, *args end |
.enqueue_in(interval, method_name, *args) ⇒ Object
83 84 85 |
# File 'lib/sk_queue.rb', line 83 def self.enqueue_in(interval, method_name, *args) add_event_in(interval, method_name, *args) end |
.extract_queue_name ⇒ Object
11 12 13 |
# File 'lib/sk_queue.rb', line 11 def self.extract_queue_name name.gsub(/^Sk/, '').underscore.gsub('/', '-').to_sym rescue :default end |
.inherited(subclass) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/sk_queue.rb', line 15 def self.inherited(subclass) subclass.class_eval do :queue => extract_queue_name :logger_path => File.("log/workers/#{queue_name}.log") end end |
.logger_path ⇒ Object
60 61 62 |
# File 'lib/sk_queue.rb', line 60 def self.logger_path ['logger_path'] end |
.method_missing(method_name, *args) ⇒ Object
Worker.some_method(“call new method on Worker async”) Worker.some_method_in(2.minutes.from_now,“call new method on Worker sheduled async”) Worker.some_method_at(2.minutes.from_now,“call new method on Worker sheduled async”)
90 91 92 93 94 95 96 |
# File 'lib/sk_queue.rb', line 90 def self.method_missing(method_name, *args) if method_name.to_s[/\A(\w*)_((at)|(in))\z/] add_event_in(args.shift, $1.to_s.to_sym, *args) else add_event(method_name, *args) end end |
.notify_about_error(exception) ⇒ Object
114 115 116 |
# File 'lib/sk_queue.rb', line 114 def self.notify_about_error(exception) # stub end |
.proxy(method_name) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/sk_queue.rb', line 107 def self.proxy(method_name) self.should_receive(method_name) do |*data| x = Sidekiq.load_json(Sidekiq.dump_json(data)) self.new.send(method_name, *x) end.any_number_of_times end |
.queue_name ⇒ Object
47 48 49 |
# File 'lib/sk_queue.rb', line 47 def self.queue_name ['queue'] end |
.set_queue_name(val) ⇒ Object
55 56 57 |
# File 'lib/sk_queue.rb', line 55 def self.set_queue_name(val) :queue => val end |
Instance Method Details
#benchmark ⇒ Object
43 44 45 |
# File 'lib/sk_queue.rb', line 43 def benchmark self.class.benchmark end |
#logger ⇒ Object
101 102 103 104 105 |
# File 'lib/sk_queue.rb', line 101 def 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" } end end |
#logger_path ⇒ Object
64 65 66 |
# File 'lib/sk_queue.rb', line 64 def logger_path self.class.logger_path end |
#perform(method_name, args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sk_queue.rb', line 22 def perform(method_name, args) start_time = benchmark ? Time.now : nil self.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.}, #{args.inspect}" self.class.notify_about_error(ex) raise ex end |
#queue_name ⇒ Object
51 52 53 |
# File 'lib/sk_queue.rb', line 51 def queue_name self.class.queue_name end |