advanced-sneakers-activejob 0.2.2 → 0.2.3

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: 83290368db4ab9594638a5e8e8c33a19fbf3e25c80eda701da5e8cb8d9a7c01e
4
- data.tar.gz: 512fda41a0c8ee3cd655ca0779c5f8057b0af86b00c7f88d569a2b267c51f25e
3
+ metadata.gz: 2ae17fa99114e9d196dfb9c0597250dd67fabb59bbf5bd687a16e0ec08876457
4
+ data.tar.gz: 655068e7718b730183051637ba4a9532e906655a811721df371eb7ba2b68ad41
5
5
  SHA512:
6
- metadata.gz: 00a6507d677dabf7750080aff743bade38ecc6a2b77a8a33c7fb8db784508be114dbbc62426be14a9c26a1e44c4c9ee20fb1d319f12f631cacb8251bbf8d1f93
7
- data.tar.gz: e83397b48629df2df3461a5b6825d42ab0f51ce46fdf44444eb017cfa976b1d69b5f44d2cec56b14155827858df16ec4f3fc35e776ce2dd58c263163cfe13802
6
+ metadata.gz: 354d6f85a842408cb17e214b20ab5a0eb25815ce2e54ba727e8a4ac3309cd220e6a2e33c777f2bf13a97381d41777d8e50d63d1ab95a2b4c0a67680b3f992330
7
+ data.tar.gz: 9a3552f273d1da9ace5d808aab2b519f1c21d2dbebcecb72c272c699f8914b2381e44e4c8e023f939391aa86f6648bc8673a1a763a705d8f4141024151c8437f
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
- ## Changes Between 0.2.0 and 0.2.2
1
+ ## Changes Between 0.2.2 and 0.2.3
2
+
3
+ ### Refactored support for ActiveJob prefix https://github.com/veeqo/advanced-sneakers-activejob/pull/3
4
+ ### Support for custom adapter per job https://github.com/veeqo/advanced-sneakers-activejob/pull/4
5
+
6
+ ## Changes Between 0.2.1 and 0.2.2
2
7
 
3
8
  Cleanup of `puts` and logger mistakenly introduced in previous version
4
9
 
@@ -31,18 +31,36 @@ module AdvancedSneakersActiveJob
31
31
  end
32
32
 
33
33
  def define_consumer(queue_name:)
34
- @consumers ||= {}
35
-
36
- @consumers[queue_name] ||= begin
37
- klass = Class.new(ActiveJob::QueueAdapters::AdvancedSneakersAdapter::JobWrapper)
38
- klass.include Sneakers::Worker
39
- const_set([queue_name, 'queue_consumer'].join('_').classify, klass)
40
- klass.from_queue(queue_name, AdvancedSneakersActiveJob.config.sneakers)
41
- end
34
+ name = consumer_name(queue_name: queue_name)
35
+
36
+ return const_get(name) if const_defined?(name)
37
+
38
+ klass = Class.new(ActiveJob::QueueAdapters::AdvancedSneakersAdapter::JobWrapper)
39
+ const_set(name, klass)
40
+ klass.include Sneakers::Worker
41
+ klass.from_queue(queue_name, AdvancedSneakersActiveJob.config.sneakers)
42
+
43
+ klass
42
44
  end
43
45
 
44
46
  def publisher
45
47
  @publisher ||= AdvancedSneakersActiveJob::Publisher.new
46
48
  end
49
+
50
+ # Based on ActiveSupport::Inflector#parameterize
51
+ def consumer_name(queue_name:)
52
+ # replace accented chars with their ascii equivalents
53
+ parameterized_string = ::ActiveSupport::Inflector.transliterate(queue_name)
54
+ # Turn unwanted chars into the separator
55
+ parameterized_string.gsub!(/[^a-z0-9\-_]+/, '_')
56
+ # No more than one of the separator in a row.
57
+ parameterized_string.gsub!(/_{2,}/, '_')
58
+ # Remove leading/trailing separator.
59
+ parameterized_string.gsub!(/^_|_$/, '')
60
+ # Ruby does not allow classes with leading digits
61
+ parameterized_string.gsub!(/\A(\d)/, 'queue\1')
62
+
63
+ [parameterized_string, 'consumer'].join('_').classify
64
+ end
47
65
  end
48
66
  end
@@ -19,30 +19,11 @@ module AdvancedSneakersActiveJob
19
19
  end
20
20
  end
21
21
 
22
- def queue_as(*args)
23
- super(*args)
24
- define_consumer
25
- end
26
-
27
22
  def message_options(options)
28
23
  raise ArgumentError, 'message_options accepts Hash argument only' unless options.is_a?(Hash)
29
24
 
30
25
  self.publish_options = options.symbolize_keys
31
26
  end
32
-
33
- private
34
-
35
- def define_consumer
36
- AdvancedSneakersActiveJob.define_consumer(queue_name: queue_name_without_prefix)
37
- end
38
-
39
- def queue_name_without_prefix
40
- name = queue_name.respond_to?(:call) ? queue_name.call : queue_name
41
-
42
- return name if queue_name_prefix.blank?
43
-
44
- name.to_s.sub([queue_name_prefix, queue_name_delimiter].join, '')
45
- end
46
27
  end
47
28
  end
48
29
  end
@@ -170,9 +170,9 @@ module AdvancedSneakersActiveJob
170
170
 
171
171
  def delayed_queue_name(delay:)
172
172
  [
173
- config_delayed_queue_prefix,
174
- delay
175
- ].join(':')
173
+ ::ActiveJob::Base.queue_name_prefix,
174
+ [config_delayed_queue_prefix, delay].join(':')
175
+ ].compact.join(::ActiveJob::Base.queue_name_delimiter)
176
176
  end
177
177
 
178
178
  def create_delayed_queue_and_binding(queue_name:, delay:)
@@ -12,7 +12,6 @@ module AdvancedSneakersActiveJob
12
12
  initializer 'advanced_sneakers_activejob.discover_default_job' do
13
13
  ActiveSupport.on_load(:active_job) do
14
14
  ActiveJob::Base.include AdvancedSneakersActiveJob::ActiveJobPatch
15
- ActiveJob::Base.queue_as # Enforce definition of ActiveJob::Base::Consumer (default queue)
16
15
  end
17
16
  end
18
17
 
@@ -5,7 +5,7 @@ require 'sneakers/tasks'
5
5
  task :environment
6
6
 
7
7
  namespace :sneakers do
8
- desc 'Start work for ActiveJob only (set $WORKERS=AdvancedSneakersActiveJob::FooQueueConsumer for processing of :foo queue)'
8
+ desc 'Start work for ActiveJob only (set $WORKERS=AdvancedSneakersActiveJob::FooConsumer for processing of :foo queue)'
9
9
  task :active_job do
10
10
  Rake::Task['environment'].invoke
11
11
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdvancedSneakersActiveJob
4
- VERSION = '0.2.2'
4
+ VERSION = '0.2.3'
5
5
  end
@@ -4,7 +4,7 @@ module AdvancedSneakersActiveJob
4
4
  # Sneakers uses Sneakers::Worker::Classes array to track all workers.
5
5
  # WorkersRegistry mocks original array to track ActiveJob workers separately.
6
6
  class WorkersRegistry
7
- attr_reader :sneakers_workers, :activejob_workers
7
+ attr_reader :sneakers_workers
8
8
 
9
9
  delegate :activejob_workers_strategy, to: :'AdvancedSneakersActiveJob.config'
10
10
 
@@ -17,7 +17,7 @@ module AdvancedSneakersActiveJob
17
17
 
18
18
  def <<(worker)
19
19
  if worker <= ActiveJob::QueueAdapters::AdvancedSneakersAdapter::JobWrapper
20
- activejob_workers << worker
20
+ @activejob_workers << worker
21
21
  else
22
22
  sneakers_workers << worker
23
23
  end
@@ -36,12 +36,37 @@ module AdvancedSneakersActiveJob
36
36
  end
37
37
  end
38
38
 
39
- # For cleaner output on inspecting Sneakers::Worker::Classes in console.
40
- def inspect
39
+ def to_hash
41
40
  {
42
41
  sneakers_workers: sneakers_workers,
43
42
  activejob_workers: activejob_workers
44
43
  }
45
44
  end
45
+
46
+ alias to_h to_hash
47
+
48
+ # For cleaner output on inspecting Sneakers::Worker::Classes in console.
49
+ alias inspect to_hash
50
+
51
+ def activejob_workers
52
+ define_active_job_consumers
53
+
54
+ @activejob_workers
55
+ end
56
+
57
+ private
58
+
59
+ def define_active_job_consumers
60
+ active_job_classes_with_matching_adapter.each do |worker|
61
+ AdvancedSneakersActiveJob.define_consumer(queue_name: worker.new.queue_name)
62
+ end
63
+ end
64
+
65
+ def active_job_classes_with_matching_adapter
66
+ ([ActiveJob::Base] + ActiveJob::Base.descendants).select do |klass|
67
+ klass.queue_adapter == ::ActiveJob::QueueAdapters::AdvancedSneakersAdapter ||
68
+ klass.queue_adapter.is_a?(::ActiveJob::QueueAdapters::AdvancedSneakersAdapter)
69
+ end
70
+ end
46
71
  end
47
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: advanced-sneakers-activejob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Sharshenov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-05 00:00:00.000000000 Z
12
+ date: 2020-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activejob