background_bunnies 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ require 'json'
2
+
3
+ module BackgroundBunnies
4
+ class Broadcaster
5
+ attr_reader :queue_name
6
+ attr_reader :connection
7
+ attr_reader :exchange
8
+
9
+ def initialize(connection_or_group, queue_name)
10
+ @connection = BackgroundBunnies.connect connection_or_group
11
+ @queue_name = queue_name.to_s
12
+ @channel = @connection.create_channel
13
+ @exchange = @channel.fanout(BackgroundBunnies.broadcast_exchange_name(queue_name))
14
+ end
15
+
16
+ #
17
+ # Publishes a Job for the Worker
18
+ #
19
+ def enqueue(payload)
20
+ @exchange.publish(JSON.generate(payload))
21
+ end
22
+
23
+ end
24
+ end
@@ -1,13 +1,15 @@
1
1
  require 'thread'
2
2
  require 'bunny'
3
3
  require 'amqp'
4
+ require 'socket'
5
+
4
6
  require_relative 'job'
5
7
 
6
8
  module BackgroundBunnies
7
9
  module Bunny
10
+ DEFAULT_CONNECTION_OPTIONS = {:threaded=>true}
8
11
 
9
12
  module BunnyConfigurators
10
- DEFAULT_CONNECTION_OPTIONS = {:threaded=>true}
11
13
  def group(group_name)
12
14
  @group_name = group_name
13
15
  end
@@ -16,6 +18,10 @@ module BackgroundBunnies
16
18
  @queue_name = queue_name.to_s
17
19
  end
18
20
 
21
+ def type(type)
22
+ @type = type
23
+ end
24
+
19
25
  def group_name
20
26
  @group_name || :default
21
27
  end
@@ -24,6 +30,10 @@ module BackgroundBunnies
24
30
  @queue_name || demodulized_class_name
25
31
  end
26
32
 
33
+ def queue_type
34
+ @type || :queue
35
+ end
36
+
27
37
  def connection_options
28
38
  @connection_options || DEFAULT_CONNECTION_OPTIONS.dup
29
39
  end
@@ -45,6 +55,10 @@ module BackgroundBunnies
45
55
  BackgroundBunnies::Producer.new(connection, queue_name)
46
56
  end
47
57
 
58
+ def create_broadcaster(connection)
59
+ BackgroundBunnies::Broadcaster.new(connection, queue_name)
60
+ end
61
+
48
62
  end
49
63
 
50
64
  def self.included(base)
@@ -58,6 +72,10 @@ module BackgroundBunnies
58
72
  self.class.queue_name
59
73
  end
60
74
 
75
+ def queue_type
76
+ self.class.queue_type
77
+ end
78
+
61
79
  def connection_options
62
80
  self.class.connection_options
63
81
  end
@@ -74,7 +92,18 @@ module BackgroundBunnies
74
92
  def start(connection_or_group)
75
93
  @connection = connection_or_group
76
94
  @channel = AMQP::Channel.new(@connection)
77
- @queue = @channel.queue(queue_name)
95
+ queue_options = {}
96
+ name = queue_name
97
+ if queue_type == :broadcast
98
+ queue_options[:exclusive] = true
99
+ queue_options[:auto_delete] = true
100
+ name = "#{Socket.gethostname}-#{Process.pid}-#{self.object_id}"
101
+ @queue = @channel.queue(name, queue_options)
102
+ @exchange = @channel.fanout(BackgroundBunnies.broadcast_exchange_name(queue_name))
103
+ @queue.bind(@exchange)
104
+ else
105
+ @queue = @channel.queue(queue_name, queue_options)
106
+ end
78
107
  @consumer = @queue.subscribe(:ack=>true) do |metadata, payload|
79
108
  info = metadata
80
109
  properties = nil
@@ -1,3 +1,3 @@
1
1
  module BackgroundBunnies
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -2,6 +2,7 @@ require "background_bunnies/version"
2
2
  require "background_bunnies/logger"
3
3
  require "background_bunnies/bunny"
4
4
  require "background_bunnies/producer"
5
+ require "background_bunnies/broadcaster"
5
6
  require "background_bunnies/job"
6
7
  require "background_bunnies/workers"
7
8
  require "thread"
@@ -12,6 +13,10 @@ module BackgroundBunnies
12
13
 
13
14
  class << self
14
15
 
16
+ def broadcast_exchange_name(queue_name)
17
+ "bunnies.broadcasters.#{queue_name}"
18
+ end
19
+
15
20
  #
16
21
  # Group Connection Configurations
17
22
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: background_bunnies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-09 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny
@@ -93,6 +93,7 @@ files:
93
93
  - examples/increments_producer_reset.rb
94
94
  - examples/increments_worker.rb
95
95
  - lib/background_bunnies.rb
96
+ - lib/background_bunnies/broadcaster.rb
96
97
  - lib/background_bunnies/bunny.rb
97
98
  - lib/background_bunnies/job.rb
98
99
  - lib/background_bunnies/logger.rb