ribbon-event_bus 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa0e255bfceecf580acb711845bdf8402d212593
4
- data.tar.gz: 107d32e1e5dd6c66597aaefae1d3dfa6a2cc13ab
3
+ metadata.gz: 07d4601534a5d201e75d5c81367616d82e278c2e
4
+ data.tar.gz: de763f8aeb39610d210860de8050f6dd6a78327d
5
5
  SHA512:
6
- metadata.gz: 29404aa9a31933c838acd94e60d9c0f22bd8d9ea2e46739fcca65cc83dc6778527df8ac2ecc4f801c23daff2067f61465b642eb39d65da2d801f5bc256eb7900
7
- data.tar.gz: 830ded2e0e09da4db4403dbdc228565302895f711edb5b2c117d612747f576f3797f2343ae1815b3ad5de00e166e65d81e587037ed780a04e8a2de2b6ae57c14
6
+ metadata.gz: 0d1adb38016cc6bb5c59b9d345d147f2dc224d69de9bf166bc13a1a0e4c32869a84a3f3e362a163473551f54a88b1d90d1679838e5d0fcf99488b728063d7a36
7
+ data.tar.gz: 76a811606c9e92f8c87f6c9131ad7e20e8a011648729c8b86f2da4b053d2a34e5dfd1e8a347cbe50a01820837a15401c4e440d777e3b8098c3a54ce244e4eb29
data/config/defaults.yml CHANGED
@@ -1,10 +1,11 @@
1
1
  subscriptions:
2
2
  default_priority: medium
3
+ max_priority: 5
3
4
 
4
5
  publishers:
5
6
  resque:
6
7
  publisher_queue: publisher
7
- subscription_queue_format: 'subscriptions_p%{priority}'
8
+ subscription_queue_formatter: null
8
9
 
9
10
  remote_resque:
10
11
  queue: publisher
@@ -72,6 +72,15 @@ module Ribbon
72
72
  _subscriptions_by_locators[locator]
73
73
  end
74
74
 
75
+ def find_publisher(publisher)
76
+ klass = Publishers.load(publisher)
77
+ publishers && publishers.find { |pub| pub.is_a?(klass) }
78
+ end
79
+
80
+ def has_publisher?(publisher)
81
+ !!find_publisher(publisher)
82
+ end
83
+
75
84
  def _register_subscription(subscription)
76
85
  if _subscriptions_by_locators[subscription.locator]
77
86
  # This is not expected to occur
@@ -21,7 +21,6 @@ module Ribbon::EventBus
21
21
  # These should be the same as the args passed to Resque.enqueue in
22
22
  # ResquePublisher#publish(event).
23
23
  args = [
24
- config.subscription_queue_format.to_s,
25
24
  event.serialize
26
25
  ]
27
26
 
@@ -5,13 +5,33 @@ module Ribbon::EventBus
5
5
  class ResquePublisher < Publisher
6
6
  config_key :resque
7
7
 
8
+ def initialize(instance=nil, params={})
9
+ super
10
+ _disallow_multiple_per_instance
11
+ end
12
+
8
13
  def publish(event)
9
14
  super
10
15
 
11
16
  unless event.subscriptions.empty?
12
17
  PublisherJob.set_queue(config.publisher_queue.to_sym)
13
- sub_queue_format = config.subscription_queue_format.to_s
14
- Resque.enqueue(PublisherJob, sub_queue_format, event.serialize)
18
+ Resque.enqueue(PublisherJob, event.serialize)
19
+ end
20
+ end
21
+
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
30
+ else
31
+ raise Errors::PublisherError, "Invalid subscription_queue_formatter: #{formatter.inspect}"
32
+ end
33
+ else
34
+ lambda { |subscription| "subscriptions_p#{subscription.priority}" }
15
35
  end
16
36
  end
17
37
 
@@ -20,24 +40,22 @@ module Ribbon::EventBus
20
40
  @queue = queue
21
41
  end
22
42
 
23
- def self.perform(sub_queue_format, serialized_event)
43
+ def self.perform(serialized_event)
24
44
  event = Event.deserialize(serialized_event)
25
45
  instance = event.instance
26
46
 
47
+ publisher = instance.find_publisher(:resque)
48
+ raise Errors::PublisherError, 'No ResquePublisher found' unless publisher
49
+ queue_formatter = publisher.subscription_queue_formatter
50
+
27
51
  instance.plugins.perform(:resque_publish, event) do |event|
28
52
  event.subscriptions.each { |s|
29
- SubscriptionJob.set_queue(
30
- (sub_queue_format % {
31
- event: event.name,
32
- priority: s.priority
33
- }).to_sym
34
- )
35
-
53
+ SubscriptionJob.set_queue(queue_formatter.call(s).to_sym)
36
54
  Resque.enqueue(SubscriptionJob, s.serialize, event.serialize)
37
55
  }
38
56
  end
39
57
  end
40
- end
58
+ end # PublisherJob
41
59
 
42
60
  module SubscriptionJob
43
61
  def self.set_queue(queue)
@@ -49,6 +67,14 @@ module Ribbon::EventBus
49
67
  event = Event.deserialize(serialized_event)
50
68
  subscription.handle(event)
51
69
  end
70
+ end # SubscriptionJob
71
+
72
+ private
73
+ def _disallow_multiple_per_instance
74
+ if instance.has_publisher?(:resque)
75
+ raise Errors::PublisherError,
76
+ "cannot have multiple ResquePublishers in an EventBus instance"
77
+ end
52
78
  end
53
79
  end
54
80
  end
@@ -14,14 +14,6 @@ module Ribbon::EventBus
14
14
  attr_reader :priority
15
15
  attr_reader :locator
16
16
 
17
- PRIORITY_SYMBOL_TO_INTEGER_MAP = {
18
- highest: 1,
19
- high: 3,
20
- medium: 5,
21
- low: 7,
22
- lowest: 10
23
- }.freeze
24
-
25
17
  def initialize(event_name, params={}, &block)
26
18
  @event_name = event_name.to_sym
27
19
  @_block = block
@@ -73,6 +65,19 @@ module Ribbon::EventBus
73
65
  Digest::MD5.hexdigest(_path).to_sym
74
66
  end
75
67
 
68
+ def _symbol_to_priority(sym)
69
+ (@__symbol_to_priority_map ||= _generate_priority_shortcut_map)[sym]
70
+ end
71
+
72
+ def _generate_priority_shortcut_map(max_priority=config.max_priority)
73
+ {}.tap { |map|
74
+ map.merge!(highest: 1, lowest: max_priority)
75
+ map[:medium] = (map[:lowest] / 2.0).ceil
76
+ map[:high] = (map[:medium] / 2.0).ceil
77
+ map[:low] = ((map[:lowest] + map[:medium]) / 2.0).ceil
78
+ }.freeze
79
+ end
80
+
76
81
  ############################################################################
77
82
  # Parameter Evaluation Logic
78
83
  #
@@ -106,13 +111,13 @@ module Ribbon::EventBus
106
111
 
107
112
  # Evaluate an integer as a priority.
108
113
  def _evaluate_priority_int(int)
109
- raise Errors::InvalidPriorityError, int unless int > 0 && int <= 10
114
+ raise Errors::InvalidPriorityError, int unless int > 0 && int <= config.max_priority
110
115
  int
111
116
  end
112
117
 
113
118
  # Evaluate a symbol as a priority.
114
119
  def _evaluate_priority_symbol(sym)
115
- if (priority = PRIORITY_SYMBOL_TO_INTEGER_MAP[sym])
120
+ if (priority = _symbol_to_priority(sym))
116
121
  _evaluate_priority(priority)
117
122
  else
118
123
  raise Errors::InvalidPriorityError, sym.inspect
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  module EventBus
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ribbon-plugins