advanced-sneakers-activejob 0.1.0 → 0.2.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: b1a41d60fe8a0f6041331ada53384c54f3316a3f9c08fd94cf21949fd5467073
4
- data.tar.gz: 16f1bd88c90e7edde21a1231420f0415c8e07d466226b51270ae4085f6d3b215
3
+ metadata.gz: d0b44d61ffa5b78273ecb535f48c3281be398c0c8d5fc89a11fd64b5937482cf
4
+ data.tar.gz: cf00f7567d031d06f0554f64aa23ca4cade11eb815c44973d4baa90cfb661a30
5
5
  SHA512:
6
- metadata.gz: c68cc556832c0433e137a399e43011b7e9e884ca50c12beed290b1cc065d84c31ba5e9fe620527c1b6adfb6eca49b251d4e5c4322b6dd34186194283b7ffcee2
7
- data.tar.gz: 5b18d980cf4ee656803dfafb82213fb9a1edb91ba283228a375d963cc8ea5cc5fc93a790472f6d10d37731df6be86b36595fda34f07bf7521564ea8c046f9338
6
+ metadata.gz: 12aab9b5cf9d2f9ce26512873cb510f1aa9a63dc29b118f1722a5e72c1690d34fefeead42b44d96a20192279c4880f274b712be9fbd5542540983195c5942b24
7
+ data.tar.gz: 8e63c9501b05e295004de85bc518883ae3c44dfbbf1067c67b9239a8adeadb39ee15be987dc704e4122f516e0f9a88722ff52ccb1a28698f3a1fbc94fc70d9d9
data/CHANGELOG.md CHANGED
@@ -1 +1,7 @@
1
+ ## Changes Between 0.1.0 and 0.2.0
2
+
3
+ ### `message_options` https://github.com/veeqo/advanced-sneakers-activejob/pull/1
4
+
5
+ Customizable options for message publishing (`routing_key`, `headers`, etc)
6
+
1
7
  ## Original Release: 0.1.0
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
- # `:advanced_sneakers` adapter for ActiveJob [![Build Status](https://travis-ci.com/veeqo/advanced-sneakers-activejob.svg?branch=master)](https://travis-ci.com/veeqo/advanced-sneakers-activejob)
1
+ # `:advanced_sneakers` adapter for ActiveJob
2
+ [![Build Status](https://travis-ci.com/veeqo/advanced-sneakers-activejob.svg?branch=master)](https://travis-ci.com/veeqo/advanced-sneakers-activejob) [![Gem Version](https://badge.fury.io/rb/advanced-sneakers-activejob.svg)](https://badge.fury.io/rb/advanced-sneakers-activejob)
2
3
 
3
4
  Drop-in replacement for `:sneakers` adapter of ActiveJob. Extra features:
4
5
 
5
6
  1. Tries to [handle unrouted messages](#unrouted-messages)
6
7
  2. Respects `queue_as` of ActiveJob and defines consumer class per RabbitMQ queue
7
- 3. Supports [custom routing keys](#custom-routing-keys)
8
+ 3. Supports [custom message options](#custom-message-options)
8
9
  4. Allows to run ActiveJob consumers [separately](#how-to-separate-activejob-consumers) from native Sneakers consumers
9
10
  5. Support for [`delayed jobs`](https://edgeguides.rubyonrails.org/active_job_basics.html#enqueue-the-job) `GuestsCleanupJob.set(wait: 1.week).perform_later(guest)`
10
11
  6. [Exponential backoff\*](#exponential-backoff)
@@ -52,27 +53,40 @@ Take into accout that **this process is asynchronous**. It means that in case of
52
53
 
53
54
  **Delayed messages are not handled!** If job is delayed `GuestsCleanupJob.set(wait: 1.week).perform_later(guest)` and there is no proper routing defined at the moment of job execution, it would be lost.
54
55
 
55
- ## Custom routing keys
56
+ ## Custom message options
56
57
 
57
- Advanced sneakers adapter supports customizable [routing keys](https://www.rabbitmq.com/tutorials/tutorial-four-ruby.html).
58
+ Advanced sneakers adapter allows to set custom message options (e.g. [routing keys](https://www.rabbitmq.com/tutorials/tutorial-four-ruby.html)).
58
59
 
59
60
  ```ruby
60
61
  class MyJob < ActiveJob::Base
61
62
 
62
63
  queue_as :some_name
63
64
 
65
+ message_options routing_key: 'my.custom.routing.key',
66
+ headers: { 'foo' => 'bar' }
67
+
64
68
  def perform(params)
65
69
  # ProcessData.new(params).call
66
70
  end
71
+ end
72
+ ```
73
+
74
+ Procs are also supported
75
+ ```ruby
76
+ class MyJob < ActiveJob::Base
77
+
78
+ queue_as :some_name
67
79
 
68
- def routing_key
69
- # we have instance of job here (including #arguments)
70
- 'my.custom.routing.key'
80
+ message_options routing_key: ->(job) { "process_user_data.#{job.arguments.first.vip? ? 'urgent' : 'regular' }" }
81
+
82
+ def perform(user)
83
+ # ProcessUserData.new(user).call
71
84
  end
72
85
  end
73
86
  ```
74
87
 
75
- Take into accout that **custom routing key is used for publishing only**.
88
+
89
+ Take into accout that **custom message options are used for publishing only**.
76
90
 
77
91
  ## How to separate ActiveJob consumers
78
92
 
@@ -38,14 +38,23 @@ module ActiveJob
38
38
  @monitor.synchronize do
39
39
  [
40
40
  Sneakers::ContentType.serialize(job.serialize, AdvancedSneakersActiveJob::CONTENT_TYPE),
41
- { routing_key: routing_key(job) }
41
+ build_publish_params(job)
42
42
  ]
43
43
  end
44
44
  end
45
45
 
46
- def routing_key(job)
47
- queue_name = job.queue_name.respond_to?(:call) ? job.queue_name.call : job.queue_name
48
- job.respond_to?(:routing_key) ? job.routing_key : queue_name
46
+ def build_publish_params(job)
47
+ params = job.class.publish_options.dup || {}
48
+
49
+ params.each do |key, value|
50
+ params[key] = value.call(job) if value.respond_to?(:call)
51
+ end
52
+
53
+ unless params.key?(:routing_key)
54
+ params[:routing_key] = job.queue_name.respond_to?(:call) ? job.queue_name.call : job.queue_name
55
+ end
56
+
57
+ params
49
58
  end
50
59
  end
51
60
 
@@ -7,6 +7,8 @@ module AdvancedSneakersActiveJob
7
7
  included do
8
8
  # AMQP message contains metadata which might be helpful for consumer (e.g. job.delivery_info.routing_key)
9
9
  attr_accessor :delivery_info, :headers
10
+
11
+ class_attribute :publish_options, instance_accessor: false
10
12
  end
11
13
 
12
14
  module ClassMethods
@@ -22,6 +24,12 @@ module AdvancedSneakersActiveJob
22
24
  define_consumer
23
25
  end
24
26
 
27
+ def message_options(options)
28
+ raise ArgumentError, 'message_options accepts Hash argument only' unless options.is_a?(Hash)
29
+
30
+ self.publish_options = options.symbolize_keys
31
+ end
32
+
25
33
  private
26
34
 
27
35
  def define_consumer
@@ -20,7 +20,7 @@ module AdvancedSneakersActiveJob
20
20
  at_exit { wait_for_unrouted_messages_processing(timeout: WAIT_FOR_UNROUTED_MESSAGES_AT_EXIT_TIMEOUT) }
21
21
  end
22
22
 
23
- def publish(message, routing_key:, headers: {}, **properties)
23
+ def publish(message, routing_key: nil, headers: {}, **properties)
24
24
  ensure_connection!
25
25
 
26
26
  logger.debug "Publishing <#{message}> to [#{publish_exchange.name}] with routing_key [#{routing_key}]"
@@ -35,7 +35,7 @@ module AdvancedSneakersActiveJob
35
35
  publish_exchange.publish(message, params)
36
36
  end
37
37
 
38
- def publish_delayed(message, routing_key:, delay:, headers: {}, **properties)
38
+ def publish_delayed(message, delay:, routing_key: nil, headers: {}, **properties)
39
39
  ensure_connection!
40
40
 
41
41
  logger.debug "Publishing <#{message}> to [#{publish_delayed_exchange.name}] with routing_key [#{routing_key}] and delay [#{delay}]"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdvancedSneakersActiveJob
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  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.1.0
4
+ version: 0.2.0
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-03-19 00:00:00.000000000 Z
12
+ date: 2020-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activejob