Class: Workling::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



62
63
64
65
66
# File 'lib/workling/base.rb', line 62

def initialize
  super
  
  create
end

Class Method Details

.expose(method, options) ⇒ Object

expose a method using a custom queue name



35
36
37
# File 'lib/workling/base.rb', line 35

def self.expose(method, options)
  self.exposed_methods[method.to_s] = options[:as]
end

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  Workling::Discovery.add_worker subclass
end

.loggerObject



19
20
21
# File 'lib/workling/base.rb', line 19

def self.logger
  @@logger ||= defined?(RAILS_DEFAULT_LOGGER) ? ::RAILS_DEFAULT_LOGGER : Logger.new($stdout)
end

.method_for(queue) ⇒ Object

identify the method linked to the queue



49
50
51
52
53
54
55
# File 'lib/workling/base.rb', line 49

def self.method_for(queue)
  if self.exposed_methods.invert.has_key?(queue)
    self.exposed_methods.invert[queue]
  else
    queue.split("__").last
  end
end

.method_missing(method, *args, &block) ⇒ Object

thanks to blaine cook for this suggestion.



101
102
103
104
105
106
107
# File 'lib/workling/base.rb', line 101

def self.method_missing(method, *args, &block)
  if method.to_s =~ /^asynch?_(.*)/
    Workling::Remote.run(self.to_s.dasherize, $1, *args)
  else
    super
  end
end

.queue_for(method) ⇒ Object

identify the queue for a given method



40
41
42
43
44
45
46
# File 'lib/workling/base.rb', line 40

def self.queue_for(method)
  if self.exposed_methods.has_key?(method)
    self.exposed_methods[method]
  else
    "#{ self.to_s.tableize }/#{ method }".split("/").join("__") # Don't split with : because it messes up memcache stats
  end
end

Instance Method Details

#createObject

Put worker initialization code in here. This is good for restarting jobs that were interrupted.



70
71
# File 'lib/workling/base.rb', line 70

def create
end

#default_optionsObject

supply default_options as a hash in classes that inherit Workling::Base



96
97
98
# File 'lib/workling/base.rb', line 96

def default_options
  {}
end

#dispatch_to_worker_method(method, options = {}) ⇒ Object

takes care of suppressing remote errors but raising Workling::WorklingNotFoundError where appropriate. swallow workling exceptions so that everything behaves like remote code. otherwise StarlingRunner and SpawnRunner would behave too differently to NotRemoteRunner.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/workling/base.rb', line 81

def dispatch_to_worker_method(method, options = {})
  begin
    options = default_options.merge(options)
    self.send(method, options)
  rescue Workling::WorklingError => e
    raise e
  rescue Exception => e
    notify_exception e, method, options

    # reraise after logging. the exception really can't go anywhere in many cases. (spawn traps the exception)
    raise e if Workling.raise_exceptions?
  end
end

#loggerObject



23
24
25
# File 'lib/workling/base.rb', line 23

def logger
  self.class.logger
end

#method_for(queue) ⇒ Object



57
58
59
# File 'lib/workling/base.rb', line 57

def method_for(queue)
  self.class.method_for(queue)
end

#notify_exception(exception, method, options) ⇒ Object

override this if you want to set up exception notification



74
75
76
# File 'lib/workling/base.rb', line 74

def notify_exception(exception, method, options)
  logger.error "WORKLING ERROR: runner could not invoke #{ self.class }:#{ method } with #{ options.inspect }. error was: #{ exception.inspect }\n #{ exception.backtrace.join("\n") }"
end