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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +22 -8
- data/lib/active_job/queue_adapters/advanced_sneakers_adapter.rb +13 -4
- data/lib/advanced_sneakers_activejob/active_job_patch.rb +8 -0
- data/lib/advanced_sneakers_activejob/publisher.rb +2 -2
- data/lib/advanced_sneakers_activejob/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0b44d61ffa5b78273ecb535f48c3281be398c0c8d5fc89a11fd64b5937482cf
|
4
|
+
data.tar.gz: cf00f7567d031d06f0554f64aa23ca4cade11eb815c44973d4baa90cfb661a30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12aab9b5cf9d2f9ce26512873cb510f1aa9a63dc29b118f1722a5e72c1690d34fefeead42b44d96a20192279c4880f274b712be9fbd5542540983195c5942b24
|
7
|
+
data.tar.gz: 8e63c9501b05e295004de85bc518883ae3c44dfbbf1067c67b9239a8adeadb39ee15be987dc704e4122f516e0f9a88722ff52ccb1a28698f3a1fbc94fc70d9d9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
# `:advanced_sneakers` adapter for 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
|
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
|
56
|
+
## Custom message options
|
56
57
|
|
57
|
-
Advanced sneakers adapter
|
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
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
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
|
-
|
41
|
+
build_publish_params(job)
|
42
42
|
]
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
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
|
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,
|
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}]"
|
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.
|
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-
|
12
|
+
date: 2020-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activejob
|