queue-bus 0.7.0 → 0.8.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
  SHA256:
3
- metadata.gz: 89735322de0ab91d94ca27b4f58598ef5049f829adfdc4788aa84eccd4d4597e
4
- data.tar.gz: b6955c5ac09371018feddf9236f756de49c024ae1ac199b1498c00137761ab6d
3
+ metadata.gz: 86f211839cfb0c4e6e037abcfd1d73a91bbbe38ae8a08138f2bbad0723e092c7
4
+ data.tar.gz: d0474b7d0763983dd5bd54759023e0fe017ced93aa8ecee0b3b599b34ab4e5fd
5
5
  SHA512:
6
- metadata.gz: f4c49c5180981e584fa91e0ba8a185c82b82f9c82533b36a75e2dc57cab75ff327d3585ac109940f234c9414c64ebae33aaa1f54ccbf5ab4b0e16cd1965128bd
7
- data.tar.gz: 7486eb2fbd2cb8ede93beb3af7b2046b224e74983986045577997f227d9df4e056aac8f8054bb42f09c1194354b4c0306c45a30c059c6cab099710365ee58a9e
6
+ metadata.gz: 4c467c372844f04cdb4fd6d8d827fcbc56427936181f4bd08571da0f82c12014e0c9d6db282df17625e1102b7c0cf604af606478affeee51d1b769583a824fe2
7
+ data.tar.gz: 8d16570fa8441b83288790a052d0e09511039b72d6f44a0f9c9c4578d5668a8af04e49836c580f1a9e750602a11a1b607d8db5844e879249a41378a4c2a512f7
data/CHANGELOG.md CHANGED
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.8.0]
10
+
11
+ ### Added
12
+ - Adds `QueueBus.with_local_mode` method. Useful when working with a multithreaded environment.
13
+
9
14
  ## [0.7.0]
10
15
 
11
16
  ### Added
data/README.mdown CHANGED
@@ -141,6 +141,22 @@ event to the appropriate code block.
141
141
  You can also say `QueueBus.local_mode = :suppress` to turn off publishing altogether.
142
142
  This can be helpful inside some sort of migration, for example.
143
143
 
144
+ #### Thread Safe Local Modes
145
+
146
+ **!! This is important if you are using workers that utilize multiple threads, such as Sidekiq !!**
147
+
148
+ The above setting is global to the ruby process and modifying it will impact all threads that are
149
+ currently using QueueBus. If you want to isolate a thread or block of code from QueueBus, you can
150
+ use the method `with_local_mode`:
151
+
152
+ ```ruby
153
+ QueueBus.with_local_mode(:suppress) do
154
+ # QueueBus will be suppressed on this thread, within this block.
155
+ end
156
+ ```
157
+
158
+ The previous value of `local_mode` will be restored after the block exits.
159
+
144
160
  ### TODO
145
161
 
146
162
  * Replace local modes with adapters
data/lib/queue-bus.rb CHANGED
@@ -34,7 +34,7 @@ module QueueBus
34
34
 
35
35
  def_delegators :config, :default_app_key=, :default_app_key,
36
36
  :default_queue=, :default_queue,
37
- :local_mode=, :local_mode,
37
+ :local_mode=, :local_mode, :with_local_mode,
38
38
  :before_publish=, :before_publish_callback,
39
39
  :logger=, :logger, :log_application, :log_worker,
40
40
  :hostname=, :hostname,
@@ -6,9 +6,10 @@ require 'logger'
6
6
  module QueueBus
7
7
  # This class contains all the configuration for a running queue bus application.
8
8
  class Config
9
- attr_accessor :default_queue, :local_mode, :hostname, :incoming_queue, :logger
9
+ attr_accessor :default_queue, :hostname, :incoming_queue, :logger
10
10
 
11
11
  attr_reader :worker_middleware_stack
12
+ attr_writer :local_mode
12
13
 
13
14
  def initialize
14
15
  @worker_middleware_stack = QueueBus::Middleware::Stack.new
@@ -16,6 +17,35 @@ module QueueBus
16
17
  @hostname = Socket.gethostname
17
18
  end
18
19
 
20
+ # A wrapper that is always "truthy" but can contain an inner value. This is useful for
21
+ # checking that a thread local variable is set to a value, even if that value happens to
22
+ # be nil. This is important because setting a thread local value to nil will cause it to
23
+ # be deleted.
24
+ Wrap = Struct.new(:value)
25
+
26
+ LOCAL_MODE_VAR = :queue_bus_local_mode
27
+
28
+ # Returns the current local mode of QueueBus
29
+ def local_mode
30
+ if Thread.current.thread_variable?(LOCAL_MODE_VAR)
31
+ Thread.current.thread_variable_get(LOCAL_MODE_VAR).value
32
+ else
33
+ @local_mode
34
+ end
35
+ end
36
+
37
+ # Overrides the current local mode for the duration of a block. This is a threadsafe
38
+ # implementation. After, the global setting will be resumed.
39
+ #
40
+ # @param mode [Symbol] the mode to switch to
41
+ def with_local_mode(mode)
42
+ previous = Thread.current.thread_variable_get(LOCAL_MODE_VAR)
43
+ Thread.current.thread_variable_set(LOCAL_MODE_VAR, Wrap.new(mode))
44
+ yield if block_given?
45
+ ensure
46
+ Thread.current.thread_variable_set(LOCAL_MODE_VAR, previous)
47
+ end
48
+
19
49
  def adapter=(val)
20
50
  raise "Adapter already set to #{@adapter_instance.class.name}" if has_adapter?
21
51
 
@@ -1,3 +1,3 @@
1
1
  module QueueBus
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/spec/config_spec.rb CHANGED
@@ -26,6 +26,40 @@ describe 'QueueBus config' do
26
26
  expect(QueueBus.local_mode).to eq(:standalone)
27
27
  end
28
28
 
29
+ describe '#with_local_mode' do
30
+ it 'sets the local mode on the thread' do
31
+ QueueBus.with_local_mode(:suppress) do
32
+ expect(QueueBus.local_mode).to eq :suppress
33
+ Thread.new { expect(QueueBus.local_mode).to eq nil }.join
34
+ end
35
+ end
36
+
37
+ it 'supports nesting' do
38
+ QueueBus.with_local_mode(:suppress) do
39
+ expect(QueueBus.local_mode).to eq :suppress
40
+ QueueBus.with_local_mode(:inline) do
41
+ expect(QueueBus.local_mode).to eq :inline
42
+ end
43
+ expect(QueueBus.local_mode).to eq :suppress
44
+ end
45
+ end
46
+
47
+ it 'respects an override of nil' do
48
+ QueueBus.local_mode = :suppress
49
+ QueueBus.with_local_mode(nil) do
50
+ expect(QueueBus.local_mode).to eq nil
51
+ end
52
+ QueueBus.local_mode = :suppress
53
+ end
54
+
55
+ it 'resets to the original local mode after the block' do
56
+ QueueBus.with_local_mode(:suppress) do
57
+ expect(QueueBus.local_mode).to eq :suppress
58
+ end
59
+ expect(QueueBus.local_mode).to eq nil
60
+ end
61
+ end
62
+
29
63
  it 'sets the hostname' do
30
64
  expect(QueueBus.hostname).not_to eq(nil)
31
65
  QueueBus.hostname = 'whatever'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queue-bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Leonard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-24 00:00:00.000000000 Z
11
+ date: 2019-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -173,8 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
175
  requirements: []
176
- rubyforge_project: queue-bus
177
- rubygems_version: 2.7.6
176
+ rubygems_version: 3.0.3
178
177
  signing_key:
179
178
  specification_version: 4
180
179
  summary: A simple event bus on top of background queues