rabbit_jobs 0.11.0 → 0.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rabbit_jobs/publisher/amqp.rb +77 -0
- data/lib/rabbit_jobs/publisher/base.rb +23 -0
- data/lib/rabbit_jobs/publisher/test.rb +43 -0
- data/lib/rabbit_jobs/publisher.rb +27 -58
- data/lib/rabbit_jobs/version.rb +1 -1
- metadata +27 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3b674193fb517cd1f0f7c316e6b1fd24ff9bcc1
|
4
|
+
data.tar.gz: 27c398776f9bca3adefe436052eda826b5e8f443
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 692636217286fc981d082d2376779cf8646fdcb97259bd026158949ecaf76afcaf008458d58ffbe6faa9bc4f6ccea683e9ddc25d886c67ef3b59551f1fdf81fc
|
7
|
+
data.tar.gz: ae1c39fa5306d0ad1c7d6863f90757cb419f424735e3087fe865362c2961a4fe0be9c0d5a1b4d1edbe5302e2c2d0f43d7751b41bffd3e6563596d49d081459a8
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rabbit_jobs/publisher/base'
|
2
|
+
|
3
|
+
module RabbitJobs
|
4
|
+
class Publisher
|
5
|
+
class Amqp < Base
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def cleanup
|
9
|
+
conn = Thread.current[:rj_publisher_connection]
|
10
|
+
conn.close if conn && conn.status != :not_connected
|
11
|
+
Thread.current[:rj_publisher_connection] = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def publish_to(routing_key, klass, *params)
|
15
|
+
raise ArgumentError.new("klass=#{klass.inspect}") unless klass.is_a?(Class) || klass.is_a?(String)
|
16
|
+
routing_key = routing_key.to_sym unless routing_key.is_a?(Symbol)
|
17
|
+
raise ArgumentError.new("routing_key=#{routing_key}") unless RabbitJobs.config[:queues][routing_key]
|
18
|
+
|
19
|
+
payload = Job.serialize(klass, *params)
|
20
|
+
direct_publish_to(routing_key, payload)
|
21
|
+
end
|
22
|
+
|
23
|
+
def direct_publish_to(routing_key, payload, ex = {})
|
24
|
+
ex = {name: ex.to_s} unless ex.is_a?(Hash)
|
25
|
+
begin
|
26
|
+
exchange_opts = Configuration::DEFAULT_MESSAGE_PARAMS.merge(ex || {})
|
27
|
+
exchange_name = exchange_opts.delete(:name).to_s
|
28
|
+
|
29
|
+
exchange = connection.default_channel.exchange(exchange_name, passive: true)
|
30
|
+
exchange.on_return do |basic_deliver, properties, payload|
|
31
|
+
RJ.logger.error full_message: caller.join("\r\n"),
|
32
|
+
short_message: "AMQP ERROR: (#{basic_deliver[:reply_code]}) #{basic_deliver[:reply_text].to_s}. exchange: #{basic_deliver[:exchange]}, key: #{basic_deliver[:routing_key]}.",
|
33
|
+
_basic_deliver: basic_deliver.inspect, _properties: properties.inspect, _payload: payload.inspect
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
unless connection.default_channel.basic_publish(payload, exchange_name, routing_key, exchange_opts).connection.connected?
|
38
|
+
raise "Disconnected from #{RJ.config.server}. Connection status: #{connection.try(:status).inspect}"
|
39
|
+
end
|
40
|
+
rescue
|
41
|
+
RabbitJobs.logger.error $!.message
|
42
|
+
raise $!
|
43
|
+
end
|
44
|
+
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
def purge_queue(*routing_keys)
|
49
|
+
raise ArgumentError unless routing_keys.present?
|
50
|
+
|
51
|
+
messages_count = 0
|
52
|
+
routing_keys.map(&:to_sym).each do |routing_key|
|
53
|
+
queue = connection.default_channel.queue(routing_key, RabbitJobs.config[:queues][routing_key])
|
54
|
+
messages_count += queue.status[:message_count].to_i
|
55
|
+
connection.default_channel.queue_purge(routing_key)
|
56
|
+
end
|
57
|
+
|
58
|
+
messages_count
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def settings
|
64
|
+
Thread.current[:rj_publisher] ||= {}
|
65
|
+
end
|
66
|
+
|
67
|
+
def connection
|
68
|
+
unless settings[:connection]
|
69
|
+
settings[:connection] = Bunny.new(RabbitJobs.config.server)
|
70
|
+
settings[:connection].start
|
71
|
+
end
|
72
|
+
settings[:connection]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RabbitJobs
|
2
|
+
class Publisher
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
def cleanup
|
6
|
+
raise NotImplementedError
|
7
|
+
end
|
8
|
+
|
9
|
+
def publish_to(routing_key, klass, *params)
|
10
|
+
raise NotImplementedError
|
11
|
+
end
|
12
|
+
|
13
|
+
def direct_publish_to(routing_key, payload, ex = {})
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
def purge_queue(*routing_keys)
|
18
|
+
raise NotImplementedError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rabbit_jobs/publisher/base'
|
2
|
+
|
3
|
+
module RabbitJobs
|
4
|
+
class Publisher
|
5
|
+
class Test < Base
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def cleanup
|
9
|
+
messages.clear
|
10
|
+
end
|
11
|
+
|
12
|
+
def publish_to(routing_key, klass, *params)
|
13
|
+
raise ArgumentError.new("klass=#{klass.inspect}") unless klass.is_a?(Class) || klass.is_a?(String)
|
14
|
+
routing_key = routing_key.to_sym unless routing_key.is_a?(Symbol)
|
15
|
+
raise ArgumentError.new("routing_key=#{routing_key}") unless RabbitJobs.config[:queues][routing_key]
|
16
|
+
|
17
|
+
payload = Job.serialize(klass, *params)
|
18
|
+
direct_publish_to(routing_key, payload)
|
19
|
+
end
|
20
|
+
|
21
|
+
def direct_publish_to(routing_key, payload, ex = {})
|
22
|
+
exchange_name = ex.is_a?(Hash) ? ex[:name] : ex
|
23
|
+
messages.push payload: payload, exchange_name: exchange_name.to_s, routing_key: routing_key.to_s
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
def purge_queue(*routing_keys)
|
28
|
+
raise ArgumentError unless routing_keys.present?
|
29
|
+
|
30
|
+
ret = messages.count
|
31
|
+
messages.clear
|
32
|
+
ret
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def messages
|
38
|
+
@messages ||= []
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,74 +1,43 @@
|
|
1
|
-
|
1
|
+
require 'rabbit_jobs/publisher/amqp'
|
2
|
+
require 'rabbit_jobs/publisher/test'
|
2
3
|
|
3
4
|
module RabbitJobs
|
4
|
-
|
5
|
-
|
5
|
+
class Publisher
|
6
|
+
class << self
|
6
7
|
|
7
|
-
|
8
|
-
conn = Thread.current[:rj_publisher_connection]
|
9
|
-
conn.close if conn && conn.status != :not_connected
|
10
|
-
Thread.current[:rj_publisher_connection] = nil
|
11
|
-
end
|
12
|
-
|
13
|
-
def publish_to(routing_key, klass, *params)
|
14
|
-
raise ArgumentError.new("klass=#{klass.inspect}") unless klass.is_a?(Class) || klass.is_a?(String)
|
15
|
-
routing_key = routing_key.to_sym unless routing_key.is_a?(Symbol)
|
16
|
-
raise ArgumentError.new("routing_key=#{routing_key}") unless RabbitJobs.config[:queues][routing_key]
|
17
|
-
|
18
|
-
payload = Job.serialize(klass, *params)
|
19
|
-
direct_publish_to(routing_key, payload)
|
20
|
-
end
|
8
|
+
@publisher_type = Amqp
|
21
9
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
exchange = connection.default_channel.exchange(exchange_name, passive: true)
|
29
|
-
exchange.on_return do |basic_deliver, properties, payload|
|
30
|
-
RJ.logger.error full_message: caller.join("\r\n"),
|
31
|
-
short_message: "AMQP ERROR: (#{basic_deliver[:reply_code]}) #{basic_deliver[:reply_text].to_s}. exchange: #{basic_deliver[:exchange]}, key: #{basic_deliver[:routing_key]}.",
|
32
|
-
_basic_deliver: basic_deliver.inspect, _properties: properties.inspect, _payload: payload.inspect
|
33
|
-
true
|
10
|
+
def mode
|
11
|
+
case @publisher_type
|
12
|
+
when Amqp
|
13
|
+
:amqp
|
14
|
+
when Test
|
15
|
+
:test
|
34
16
|
end
|
17
|
+
end
|
35
18
|
|
36
|
-
|
37
|
-
|
19
|
+
def mode=(value)
|
20
|
+
@publisher_type = case value.to_s
|
21
|
+
when 'amqp'
|
22
|
+
Amqp
|
23
|
+
when 'test'
|
24
|
+
Test
|
25
|
+
else
|
26
|
+
raise ArgumentError.new("value must be :amqp or :test. Passed: #{value.inspect}")
|
38
27
|
end
|
39
|
-
rescue
|
40
|
-
RabbitJobs.logger.error $!.message
|
41
|
-
raise $!
|
42
28
|
end
|
43
29
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def purge_queue(*routing_keys)
|
48
|
-
raise ArgumentError unless routing_keys.present?
|
49
|
-
|
50
|
-
messages_count = 0
|
51
|
-
routing_keys.map(&:to_sym).each do |routing_key|
|
52
|
-
queue = connection.default_channel.queue(routing_key, RabbitJobs.config[:queues][routing_key])
|
53
|
-
messages_count += queue.status[:message_count].to_i
|
54
|
-
connection.default_channel.queue_purge(routing_key)
|
30
|
+
%i(cleanup publish_to direct_publish_to purge_queue).each do |api_method|
|
31
|
+
delegate api_method, to: :publisher_type
|
55
32
|
end
|
56
33
|
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
34
|
+
private
|
61
35
|
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
def connection
|
67
|
-
unless settings[:connection]
|
68
|
-
settings[:connection] = Bunny.new(RabbitJobs.config.server)
|
69
|
-
settings[:connection].start
|
36
|
+
def publisher_type
|
37
|
+
@publisher_type
|
70
38
|
end
|
71
|
-
settings[:connection]
|
72
39
|
end
|
73
40
|
end
|
74
41
|
end
|
42
|
+
|
43
|
+
RabbitJobs::Publisher.mode ||= :amqp
|
data/lib/rabbit_jobs/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabbit_jobs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Lazureykis
|
@@ -14,126 +14,126 @@ dependencies:
|
|
14
14
|
name: bunny
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rufus-scheduler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '4.0'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '4.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: bundler
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: timecop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rspec
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rr
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - '>='
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: simplecov
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
128
128
|
requirements:
|
129
|
-
- -
|
129
|
+
- - '>='
|
130
130
|
- !ruby/object:Gem::Version
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
|
-
- -
|
136
|
+
- - '>='
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
description: Background jobs on RabbitMQ
|
@@ -143,8 +143,8 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
-
-
|
147
|
-
-
|
146
|
+
- .gitignore
|
147
|
+
- .rspec
|
148
148
|
- Gemfile
|
149
149
|
- LICENSE
|
150
150
|
- LICENSE.txt
|
@@ -162,6 +162,9 @@ files:
|
|
162
162
|
- lib/rabbit_jobs/job.rb
|
163
163
|
- lib/rabbit_jobs/main_loop.rb
|
164
164
|
- lib/rabbit_jobs/publisher.rb
|
165
|
+
- lib/rabbit_jobs/publisher/amqp.rb
|
166
|
+
- lib/rabbit_jobs/publisher/base.rb
|
167
|
+
- lib/rabbit_jobs/publisher/test.rb
|
165
168
|
- lib/rabbit_jobs/scheduler.rb
|
166
169
|
- lib/rabbit_jobs/tasks.rb
|
167
170
|
- lib/rabbit_jobs/version.rb
|
@@ -190,17 +193,17 @@ require_paths:
|
|
190
193
|
- lib
|
191
194
|
required_ruby_version: !ruby/object:Gem::Requirement
|
192
195
|
requirements:
|
193
|
-
- -
|
196
|
+
- - '>='
|
194
197
|
- !ruby/object:Gem::Version
|
195
198
|
version: '0'
|
196
199
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
200
|
requirements:
|
198
|
-
- -
|
201
|
+
- - '>='
|
199
202
|
- !ruby/object:Gem::Version
|
200
203
|
version: '0'
|
201
204
|
requirements: []
|
202
205
|
rubyforge_project:
|
203
|
-
rubygems_version: 2.
|
206
|
+
rubygems_version: 2.0.14
|
204
207
|
signing_key:
|
205
208
|
specification_version: 4
|
206
209
|
summary: Background jobs on RabbitMQ
|