ribbon-event_bus 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd26c9dead029e9b0486e8ba596468db76fb7bb3
4
- data.tar.gz: 0051194033a07c691886ce03b1f9a2ff07f83a94
3
+ metadata.gz: a4e3cc06e8e927ee8d9699026ce5616dc2596121
4
+ data.tar.gz: e826ceefcec880ea0afcdc15c7a7f055c5496176
5
5
  SHA512:
6
- metadata.gz: 0b42f285a3fbca1401bff8810282a38c3392da3d56a03c9c594cd2fc2c93556a55a403ed0ab943f05bc96f040d1b90b4741e2c0b61e3e630c41a64a4d3d93c84
7
- data.tar.gz: 2757b6055e37f0bb290d3ff98ee279d955f947f75a9de935c310ae2c91b7f5d16f9f4bf4affd2c6f917be3929901aa057f5a2a5818a94367ed858142f23ecc39
6
+ metadata.gz: 8df0d797c59cf738ef4fad03df797a9d71ad5c2bdac7cb16e2aec7f1a8870a7741196a4b051f3ddfa9f1cebe0397ddb0e45e37ea9c30c8c78f08ac41860c4d30
7
+ data.tar.gz: 267e799cd369659d1d209e1f12362a2fd2287ed757a602b2c6eb1928fd2a13427a8832ec7b3bf710a2c8da1c31b8847e8575ae02bffe82b377c655ba93f9b75b
@@ -157,4 +157,4 @@ module Ribbon
157
157
  end
158
158
  end # Instance
159
159
  end # EventBus
160
- end # Ribbon
160
+ end # Ribbon
@@ -0,0 +1,78 @@
1
+ require 'resque'
2
+ require 'redis'
3
+ require 'celluloid/current'
4
+
5
+ module Ribbon::EventBus
6
+ module Publishers
7
+ class AsyncResquePublisher < Publisher
8
+ config_key :resque
9
+
10
+ attr_reader :resque
11
+
12
+ def initialize(instance=nil, params={})
13
+ super
14
+ @resque = config.resque? ? config.resque : Resque
15
+ PublisherWorker.supervise(args: [self], as: worker_id)
16
+ end
17
+
18
+ def worker_id
19
+ @__worker_id ||= "event_bus_resque_worker_#{object_id}".to_sym
20
+ end
21
+
22
+ ##
23
+ # The suprvisor created in the initializer will restart the PublisherWorker
24
+ # but there can be a short period of time when the actor returned by
25
+ # Celluloid::Actor[...] is dead. To avoid that we sleep for a millisecond
26
+ # to give it time to create a new worker thread. We try three times before
27
+ # giving up.
28
+ #
29
+ # This should ensure that it's unlikely for a dead worker to be returned.
30
+ # However, if a dead worker is returned, then async calls will silently
31
+ # fail, allowing normal execution. This makes firing events best-effort.
32
+ def worker
33
+ w = Celluloid::Actor[worker_id]
34
+
35
+ 3.times {
36
+ if w.dead?
37
+ sleep(0.001)
38
+ w = Celluloid::Actor[worker_id]
39
+ else
40
+ break
41
+ end
42
+ }
43
+
44
+ w
45
+ end
46
+
47
+ ##
48
+ # Needs to exist for the ResquePublisher::PublisherJob to succeed.
49
+ def subscription_queue_formatter
50
+ ResquePublisher.subscription_queue_formatter(config)
51
+ end
52
+
53
+ def publish(event)
54
+ super
55
+ worker.async.publish(event) unless event.subscriptions.empty?
56
+ end
57
+
58
+ class PublisherWorker
59
+ include Celluloid
60
+
61
+ attr_reader :publisher
62
+
63
+ def initialize(publisher)
64
+ @publisher = publisher
65
+ end
66
+
67
+ def publish(event)
68
+ publisher.resque.enqueue_to(
69
+ publisher.config.publisher_queue.to_sym,
70
+ ResquePublisher::PublisherJob,
71
+ event.serialize,
72
+ :async_resque
73
+ )
74
+ end
75
+ end # PublisherWorker
76
+ end
77
+ end
78
+ end
@@ -27,4 +27,4 @@ module Ribbon::EventBus
27
27
  end
28
28
  end
29
29
  end
30
- end
30
+ end
@@ -20,31 +20,37 @@ module Ribbon::EventBus
20
20
  end
21
21
 
22
22
  def subscription_queue_formatter
23
- if config.subscription_queue_formatter?
24
- formatter = config.subscription_queue_formatter
25
- case formatter
26
- when Array
27
- formatter.last
28
- when Proc
29
- formatter
23
+ self.class.subscription_queue_formatter(config)
24
+ end
25
+
26
+ class << self
27
+ def subscription_queue_formatter(config)
28
+ if config.subscription_queue_formatter?
29
+ formatter = config.subscription_queue_formatter
30
+ case formatter
31
+ when Array
32
+ formatter.last
33
+ when Proc
34
+ formatter
35
+ else
36
+ raise Errors::PublisherError, "Invalid subscription_queue_formatter: #{formatter.inspect}"
37
+ end
30
38
  else
31
- raise Errors::PublisherError, "Invalid subscription_queue_formatter: #{formatter.inspect}"
39
+ lambda { |subscription| "subscriptions_p#{subscription.priority}" }
32
40
  end
33
- else
34
- lambda { |subscription| "subscriptions_p#{subscription.priority}" }
35
41
  end
36
- end
42
+ end # Class Methods
37
43
 
38
44
  module PublisherJob
39
45
  def self.set_queue(queue)
40
46
  @queue = queue
41
47
  end
42
48
 
43
- def self.perform(serialized_event)
49
+ def self.perform(serialized_event, publisher_name=:resque)
44
50
  event = Event.deserialize(serialized_event)
45
51
  instance = event.instance
46
52
 
47
- publisher = instance.find_publisher(:resque)
53
+ publisher = instance.find_publisher(publisher_name)
48
54
  raise Errors::PublisherError, 'No ResquePublisher found' unless publisher
49
55
  queue_formatter = publisher.subscription_queue_formatter
50
56
 
@@ -78,4 +84,4 @@ module Ribbon::EventBus
78
84
  end
79
85
  end
80
86
  end
81
- end
87
+ end
@@ -10,6 +10,8 @@ module Ribbon::EventBus
10
10
  'ribbon/event_bus/publishers/resque_publisher')
11
11
  autoload(:RemoteResquePublisher,
12
12
  'ribbon/event_bus/publishers/remote_resque_publisher')
13
+ autoload(:AsyncResquePublisher,
14
+ 'ribbon/event_bus/publishers/async_resque_publisher')
13
15
 
14
16
  module_function
15
17
  def load_for_instance(instance)
@@ -49,4 +51,4 @@ module Ribbon::EventBus
49
51
  ProcPublisher.new(&publisher_proc)
50
52
  end
51
53
  end
52
- end
54
+ end
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  module EventBus
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-event_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ribbon-plugins
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.2.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: celluloid
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.17.2
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.17.2
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: rails
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -163,6 +177,7 @@ files:
163
177
  - lib/ribbon/event_bus/plugins/logging_plugin.rb
164
178
  - lib/ribbon/event_bus/plugins/plugin.rb
165
179
  - lib/ribbon/event_bus/publishers.rb
180
+ - lib/ribbon/event_bus/publishers/async_resque_publisher.rb
166
181
  - lib/ribbon/event_bus/publishers/proc_publisher.rb
167
182
  - lib/ribbon/event_bus/publishers/publisher.rb
168
183
  - lib/ribbon/event_bus/publishers/remote_resque_publisher.rb