queue-bus 0.10.0 → 0.11.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: 315c7263eb40e8d6165b96b8f50738ff5861127f35f1ce282f7b1d8a20914290
4
- data.tar.gz: 067034c67ce6232de23fe552c1fc3bac50ca64dd898dec9b4958009ce6067fe9
3
+ metadata.gz: 139511a2de01b9ffb7e58b63ecf62cbc01ae94fe38896282de2ae0dbb13178de
4
+ data.tar.gz: 0e481639105b4dceff9991ee448f378923fdb3ae5820294f954602e96a177243
5
5
  SHA512:
6
- metadata.gz: dfea248252d2b17eca07eb5dae5e7feb4e32ac8673f154f959f0a622221dd466470f0b243f2e8c07191c951897b646c8341e5df3933556361dc1fd831de483a6
7
- data.tar.gz: 6585cc65f3bb494161fc18e45d421203278db133b3471747caa8d526da7a551b524a6ba6289320f4a6f7fe80566f6bb7436dadda267d69d96497c7e2f14e64a2
6
+ metadata.gz: 3ce3ebbeee0e6be7513af1ed093f5612877a6293a78bad68da502817c10f205ddf69bbc3071ef497f48baef2e66f2823311ef80e579805dbeedccec9b0794bef
7
+ data.tar.gz: 8b93e84ff4b5d6801d6b490560af46532c729eb7ff2b32b0d88e565bd620388d029f1a010027b9032e56731aef936945922bf7afc642453aeb9db9cbeacd0bcd
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.11.0]
10
+
11
+ ### Added
12
+
13
+ - Adds `QueueBus.in_context` method. Useful when working with a multithreaded environment to add a description for all events published within this scope.
14
+
9
15
  ## [0.10.0]
10
16
 
11
17
  ### Added
data/README.mdown CHANGED
@@ -141,13 +141,16 @@ 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
144
+ #### Thread Safe Options
145
145
 
146
146
  **!! This is important if you are using workers that utilize multiple threads, such as Sidekiq !!**
147
147
 
148
148
  The above setting is global to the ruby process and modifying it will impact all threads that are
149
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`:
150
+ use the methods `with_local_mode` or `in_context`:
151
+
152
+
153
+ With local mode
151
154
 
152
155
  ```ruby
153
156
  QueueBus.with_local_mode(:suppress) do
@@ -155,7 +158,16 @@ QueueBus.with_local_mode(:suppress) do
155
158
  end
156
159
  ```
157
160
 
158
- The previous value of `local_mode` will be restored after the block exits.
161
+ In context
162
+
163
+ ```ruby
164
+ QueueBus.in_context('some_context') do
165
+ # Context attribute will be set for all events published within this scope.
166
+ end
167
+ ```
168
+
169
+
170
+ The previous values will be restored after the block exits.
159
171
 
160
172
  ### TODO
161
173
 
data/lib/queue-bus.rb CHANGED
@@ -43,7 +43,8 @@ module QueueBus
43
43
  :hostname=, :hostname,
44
44
  :adapter=, :adapter, :has_adapter?,
45
45
  :incoming_queue=, :incoming_queue,
46
- :redis, :worker_middleware_stack
46
+ :redis, :worker_middleware_stack,
47
+ :context=, :context, :in_context
47
48
 
48
49
  def_delegators :_dispatchers, :dispatch, :dispatchers, :dispatcher_by_key, :dispatcher_execute
49
50
 
@@ -9,7 +9,7 @@ module QueueBus
9
9
  attr_accessor :default_queue, :hostname, :incoming_queue, :logger
10
10
 
11
11
  attr_reader :worker_middleware_stack
12
- attr_writer :local_mode
12
+ attr_writer :local_mode, :context
13
13
 
14
14
  def initialize
15
15
  @worker_middleware_stack = QueueBus::Middleware::Stack.new
@@ -24,6 +24,7 @@ module QueueBus
24
24
  Wrap = Struct.new(:value)
25
25
 
26
26
  LOCAL_MODE_VAR = :queue_bus_local_mode
27
+ CONTEXT_VAR = :queue_bus_context
27
28
 
28
29
  # Returns the current local mode of QueueBus
29
30
  def local_mode
@@ -34,6 +35,15 @@ module QueueBus
34
35
  end
35
36
  end
36
37
 
38
+ # Returns the current context of QueueBus
39
+ def context
40
+ if Thread.current.thread_variable_get(CONTEXT_VAR).is_a?(Wrap)
41
+ Thread.current.thread_variable_get(CONTEXT_VAR).value
42
+ else
43
+ @context
44
+ end
45
+ end
46
+
37
47
  # Overrides the current local mode for the duration of a block. This is a threadsafe
38
48
  # implementation. After, the global setting will be resumed.
39
49
  #
@@ -46,6 +56,17 @@ module QueueBus
46
56
  Thread.current.thread_variable_set(LOCAL_MODE_VAR, previous)
47
57
  end
48
58
 
59
+ # Overrides the current bus context (if any) for the duration of a block, adding a
60
+ # `bus_context` attribute set to this value for all events published in this scope.
61
+ # This is a threadsafe implementation. After, the global setting will be resumed.
62
+ def in_context(context)
63
+ previous = Thread.current.thread_variable_get(CONTEXT_VAR)
64
+ Thread.current.thread_variable_set(CONTEXT_VAR, Wrap.new(context))
65
+ yield if block_given?
66
+ ensure
67
+ Thread.current.thread_variable_set(CONTEXT_VAR, previous)
68
+ end
69
+
49
70
  def adapter=(val)
50
71
  raise "Adapter already set to #{@adapter_instance.class.name}" if has_adapter?
51
72
 
@@ -30,6 +30,7 @@ module QueueBus
30
30
  bus_attr = { 'bus_published_at' => Time.now.to_i, 'bus_event_type' => event_type }
31
31
  bus_attr['bus_id'] = "#{Time.now.to_i}-#{generate_uuid}"
32
32
  bus_attr['bus_app_hostname'] = ::QueueBus.hostname
33
+ bus_attr['bus_context'] = ::QueueBus.context unless ::QueueBus.context.nil?
33
34
  if defined?(I18n) && I18n.respond_to?(:locale) && I18n.locale
34
35
  bus_attr['bus_locale'] = I18n.locale.to_s
35
36
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QueueBus
4
- VERSION = '0.10.0'
4
+ VERSION = '0.11.0'
5
5
  end
data/spec/config_spec.rb CHANGED
@@ -61,6 +61,41 @@ describe 'QueueBus config' do
61
61
  end
62
62
  end
63
63
 
64
+ describe '#in_context' do
65
+ it 'sets the context on the thread' do
66
+ QueueBus.in_context(:batch_processing) do
67
+ expect(QueueBus.context).to eq(:batch_processing)
68
+ Thread.new { expect(QueueBus.context).to eq nil }.join
69
+ end
70
+ end
71
+
72
+ it 'supports nesting' do
73
+ QueueBus.in_context(:batch_processing) do
74
+ expect(QueueBus.context).to eq :batch_processing
75
+ QueueBus.in_context(:processing) do
76
+ expect(QueueBus.context).to eq :processing
77
+ end
78
+ expect(QueueBus.context).to eq :batch_processing
79
+ end
80
+ end
81
+
82
+ it 'respects an override of nil' do
83
+ QueueBus.context = :batch_processing
84
+ QueueBus.in_context(nil) do
85
+ expect(QueueBus.context).to eq nil
86
+ end
87
+ QueueBus.context = :batch_processing
88
+ end
89
+
90
+ it 'resets to the original context after the block' do
91
+ expect(QueueBus.context).to eq nil
92
+ QueueBus.in_context(:batch_processing) do
93
+ expect(QueueBus.context).to eq :batch_processing
94
+ end
95
+ expect(QueueBus.context).to eq nil
96
+ end
97
+ end
98
+
64
99
  it 'sets the hostname' do
65
100
  expect(QueueBus.hostname).not_to eq(nil)
66
101
  QueueBus.hostname = 'whatever'
data/spec/publish_spec.rb CHANGED
@@ -71,6 +71,28 @@ describe 'Publishing an event' do
71
71
  expect(myval).to eq(1)
72
72
  end
73
73
 
74
+ it 'should add context metadata if wrapping publisher with in_context' do
75
+ expect(QueueBus.context).to be_nil
76
+
77
+ bus_context = 'batch_processing'
78
+ hash = { :one => 1, 'two' => 'here', 'bus_id' => 'app-given' }
79
+
80
+ event_name = 'event_name'
81
+
82
+ QueueBus.in_context(:batch_processing) do
83
+ QueueBus.publish(event_name, hash)
84
+ end
85
+
86
+ val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
87
+ hash = JSON.parse(val)
88
+
89
+ att = JSON.parse(hash['args'].first)
90
+ expect(att['bus_context']).to eq(bus_context)
91
+
92
+ expect(QueueBus.context).to be_nil
93
+
94
+ end
95
+
74
96
  it 'should set the timezone and locale if available' do
75
97
  expect(defined?(I18n)).to be_nil
76
98
  expect(Time.respond_to?(:zone)).to eq(false)
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.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Leonard
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-04 00:00:00.000000000 Z
11
+ date: 2021-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -159,7 +159,7 @@ files:
159
159
  homepage: ''
160
160
  licenses: []
161
161
  metadata: {}
162
- post_install_message:
162
+ post_install_message:
163
163
  rdoc_options: []
164
164
  require_paths:
165
165
  - lib
@@ -174,8 +174,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  - !ruby/object:Gem::Version
175
175
  version: '0'
176
176
  requirements: []
177
- rubygems_version: 3.0.3
178
- signing_key:
177
+ rubyforge_project: queue-bus
178
+ rubygems_version: 2.7.6.2
179
+ signing_key:
179
180
  specification_version: 4
180
181
  summary: A simple event bus on top of background queues
181
182
  test_files: