Module: Workling

Defined in:
lib/workling.rb,
lib/workling/base.rb,
lib/workling/remote.rb,
lib/workling/discovery.rb,
lib/workling/clients/base.rb,
lib/workling/routing/base.rb,
lib/workling/return/store/base.rb,
lib/workling/clients/amqp_client.rb,
lib/workling/remote/runners/base.rb,
lib/workling/remote/invokers/base.rb,
lib/workling/remote/runners/spawn_runner.rb,
lib/workling/remote/invokers/basic_poller.rb,
lib/workling/remote/runners/client_runner.rb,
lib/workling/clients/memcache_queue_client.rb,
lib/workling/remote/runners/starling_runner.rb,
lib/workling/remote/invokers/threaded_poller.rb,
lib/workling/remote/runners/not_remote_runner.rb,
lib/workling/return/store/memory_return_store.rb,
lib/workling/routing/class_and_method_routing.rb,
lib/workling/return/store/starling_return_store.rb,
lib/workling/remote/runners/backgroundjob_runner.rb,
lib/workling/remote/invokers/eventmachine_subscriber.rb

Overview

Subscribes the workers to the correct queues.

Defined Under Namespace

Modules: Clients, Remote, Return, Routing Classes: Base, ConfigurationError, Discovery, QueueserverNotFoundError, WorklingConnectionError, WorklingError, WorklingNotFoundError

Constant Summary collapse

VERSION =
"0.4.2.3"
@@load_path =
[ File.expand_path(File.join(File.dirname(__FILE__), '../../../../app/workers')) ]
@@raise_exceptions =
(RAILS_ENV == "test" || RAILS_ENV == "development")

Class Method Summary collapse

Class Method Details

.bj_installed?Boolean

is bj installed?

Returns:

  • (Boolean)


88
89
90
# File 'lib/workling.rb', line 88

def self.bj_installed?
  Object.const_defined? "Bj"
end

.configObject

returns a config hash. reads RAILS_ROOT/config/workling.yml



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/workling.rb', line 123

def self.config
  begin
    config_path = File.join(RAILS_ROOT, 'config', 'workling.yml')
    @@config ||=  YAML.load_file(config_path)[RAILS_ENV || 'development'].symbolize_keys
    @@config[:memcache_options].symbolize_keys! if @@config[:memcache_options]
    @@config 
  rescue
     # config files could not be read correctly
    raise ConfigurationError.new
  end
end

.default_runnerObject

determine the runner to use if nothing is specifically set. workling will try to detect starling, spawn, or bj, in that order. if none of these are found, notremoterunner will be used.

this can be overridden by setting Workling::Remote.dispatcher, eg:

Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/workling.rb', line 34

def self.default_runner
  if RAILS_ENV == "test"
    Workling::Remote::Runners::NotRemoteRunner.new
  elsif starling_installed?
    Workling::Remote::Runners::StarlingRunner.new
  elsif spawn_installed?
    Workling::Remote::Runners::SpawnRunner.new
  elsif bj_installed?
    Workling::Remote::Runners::BackgroundjobRunner.new
  else
    Workling::Remote::Runners::NotRemoteRunner.new
  end
end

.find(clazz, method = nil) ⇒ Object

gets the worker instance, given a class. the optional method argument will cause an exception to be raised if the worker instance does not respoind to said method.



52
53
54
55
56
57
58
59
60
# File 'lib/workling.rb', line 52

def self.find(clazz, method = nil)
  begin
    inst = clazz.to_s.camelize.constantize.new 
  rescue NameError
    raise_not_found(clazz, method)
  end
  raise_not_found(clazz, method) if method && !inst.respond_to?(method)
  inst
end

.raise_exceptions?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/workling.rb', line 142

def self.raise_exceptions?
  @@raise_exceptions
end

.returnObject

returns Workling::Return::Store.instance.



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

def self.return
  Workling::Return::Store.instance
end

.spawn_installed?Boolean

is spawn installed?

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/workling.rb', line 68

def self.spawn_installed?
  begin
    require 'spawn'
  rescue LoadError
  end

  Object.const_defined? "Spawn"
end

.starling_installed?Boolean

is starling installed?

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'lib/workling.rb', line 78

def self.starling_installed?
  begin
    require 'starling' 
  rescue LoadError
  end
    
  Object.const_defined? "Starling"
end

.try_load_a_memcache_clientObject

tries to load fiveruns-memcache-client. if this isn’t found, memcache-client is searched for. if that isn’t found, don’t do anything.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/workling.rb', line 94

def self.try_load_a_memcache_client
  begin
    gem 'fiveruns-memcache-client'
    require 'memcache'
  rescue Gem::LoadError
    begin
      gem 'memcache-client'
      require 'memcache'
    rescue Gem::LoadError
      Workling::Base.logger.info "WORKLING: couldn't find a memcache client - you need one for the starling runner. "
    end
  end
end

.try_load_an_amqp_clientObject

attempts to load amqp and writes out descriptive error message if not present



109
110
111
112
113
114
115
116
117
118
# File 'lib/workling.rb', line 109

def self.try_load_an_amqp_client
  begin
    require 'mq'
  rescue Exception => e
    raise WorklingError.new(
      "WORKLING: couldn't find the ruby amqp client - you need it for the amqp runner. " \
      "Install from github: gem sources -a http://gems.github.com/ && sudo gem install tmm1-amqp "
    )
  end
end