Module: BackgroundBunnies

Defined in:
lib/background_bunnies.rb,
lib/background_bunnies/job.rb,
lib/background_bunnies/bunny.rb,
lib/background_bunnies/logger.rb,
lib/background_bunnies/version.rb,
lib/background_bunnies/workers.rb,
lib/background_bunnies/producer.rb,
lib/background_bunnies/broadcaster.rb

Defined Under Namespace

Modules: Bunny, Workers Classes: Broadcaster, Job, Producer

Constant Summary collapse

DEFAULT_CONNECTION_OPTIONS =
{:threaded=>false}.freeze
VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.broadcast_exchange_name(queue_name) ⇒ Object



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

def broadcast_exchange_name(queue_name)
  "bunnies.broadcasters.#{queue_name}"
end

.configurationsObject

Group Connection Configurations



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

def configurations
  @configs || @configs = {}
end

.configure(group, url, options = DEFAULT_CONNECTION_OPTIONS) ⇒ Object

Configures the connection of a group



32
33
34
35
36
37
38
# File 'lib/background_bunnies.rb', line 32

def configure(group, url, options = DEFAULT_CONNECTION_OPTIONS)
  info "Configuring #{group} connection with #{url} and options #{options}"
  configurations[group] = {
    :url=>url,
    :options=>options
  }
end

.connect(connection_or_group, options = DEFAULT_CONNECTION_OPTIONS) ⇒ Object

Creates a new connection based on a group configuration



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/background_bunnies.rb', line 43

def connect(connection_or_group, options=DEFAULT_CONNECTION_OPTIONS)
  unless connection_or_group.is_a? Symbol
    return connection_or_group
  end
  configuration = self.configurations[connection_or_group] || DEFAULT_CONNECTION_OPTIONS
  configuration = configuration.dup.merge!(options)
  raise "Unable to connect. Missing configuration for group #{connection_or_group}" unless configuration
  conn = ::Bunny.new(configuration[:url], configuration[:options])
  conn.start
  conn
end

.describe_group(group) ⇒ Object

Describe types in BackgroundBunnies::Workers::* in the given group.



79
80
81
82
# File 'lib/background_bunnies.rb', line 79

def describe_group(group)
  worker_names = BackgroundBunnies::Workers.constants.collect! { |worker_name| BackgroundBunnies::Workers.const_get(worker_name) }
  worker_names.select {|klass| klass.group_name == group }
end

.error(a) ⇒ Object



11
12
13
# File 'lib/background_bunnies/logger.rb', line 11

def self.error(a)
  $stderr.puts "[error] BackgroundBunnies -> #{a}"
end

.info(a) ⇒ Object



7
8
9
# File 'lib/background_bunnies/logger.rb', line 7

def self.info(a)
  $stdout.puts "[info] BackgroundBunnies -> #{a}"
end

.run(group) ⇒ Object

Runs all the tasks in the given group and waits.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/background_bunnies.rb', line 58

def run(group)
  klasses = describe_group(group)
  instances = []
  EventMachine.run do
    url = configurations[group][:url]
    puts "Group Connection: #{url}"
    connection = AMQP.connect(url)
    info "Running #{group} workers: #{ klasses.join(',') }"
    klasses.each do |klass|
      instance = klass.new
      instance.start connection
      info "Started worker: #{klass.demodulized_class_name}"
      instances << instance
    end
  end
  Thread.current.join
end

.warn(a) ⇒ Object



3
4
5
# File 'lib/background_bunnies/logger.rb', line 3

def self.warn(a)
  $stderr.puts "[warn] BackgroundBunnies -> #{a}"
end