coney_island 0.12.3 → 0.12.4

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
  SHA1:
3
- metadata.gz: 8a235d6cee30feec0ec69bcf5fcb2595cd5a3e8b
4
- data.tar.gz: e01a71e0cd83955f1e017831cd58fd30051bcf44
3
+ metadata.gz: 3ebf4aa13f64b3592f798c759e5a28a5e17c84ab
4
+ data.tar.gz: b22573d2f20af9c3e212351632615e38e2b39aae
5
5
  SHA512:
6
- metadata.gz: 2c940a0f31e66999046c685dc75512de76dcf154542a5c9370ecdfbc631880a9fb7b6733b39da76019930c3407d42951201016dbb1cbef57850b52ef3d7412f4
7
- data.tar.gz: ff0eeb784f63e3e9c6745133b9e5d23afe46400f96078a005942fedffbfa0ff9812334ab3fd27348cce33aec1745028fc23687e4ebfa5d9b28f4d0401194ad86
6
+ metadata.gz: e31f447e805176a230c56540099b49dfc5f348287a9cfd957398cd8ebd7017debf9a5c605d650cbb6999d6acdd10d8b134642763d981f5febd30d9ef8da811ee
7
+ data.tar.gz: 767d5f3ef2575904e57cf6b53f2da1f48e6aba5e6e74efa5e476064ef4f601812ca017ac0dbbe533f4274306669699f67f3b28fde51abe98fcd66f09faf57e0f
@@ -10,7 +10,7 @@ module ConeyIsland
10
10
  end
11
11
 
12
12
  def self.running_inline?
13
- @run_inline
13
+ !!@run_inline
14
14
  end
15
15
 
16
16
  def self.tcp_connection_retries=(number)
@@ -149,53 +149,49 @@ module ConeyIsland
149
149
  end
150
150
 
151
151
  def self.publish_job(args, job_id = nil)
152
- if (args.first.is_a? Class or args.first.is_a? Module) and (args[1].is_a? String or args[1].is_a? Symbol) and args.last.is_a? Hash and 3 == args.length
153
- klass = args.shift
154
- klass_name = klass.name
155
-
156
- method_name = args.shift
157
- job_args = args.shift
158
- job_args ||= {}
159
- job_args['klass'] = klass_name
160
- job_args['method_name'] = method_name
161
- job_args.stringify_keys!
162
- # Extract non job args
163
- delay = job_args.delete 'delay'
164
- work_queue = job_args.delete 'work_queue'
165
- # Set class defaults if they exist
166
- if klass.included_modules.include?(Performer)
167
- delay ||= klass.get_coney_settings[:delay]
168
- work_queue ||= klass.get_coney_settings[:work_queue]
169
- end
170
- # Set our own defaults if we still don't have any
171
- work_queue ||= ConeyIsland.default_settings[:work_queue]
172
- delay ||= ConeyIsland.default_settings[:delay]
173
-
174
- if @run_inline
175
- job = ConeyIsland::Job.new(nil, job_args)
176
- job.handle_job
177
- else
178
- if delay && delay.to_i > 0
179
- @delay_queue[work_queue] ||= {}
180
- unless @delay_queue[work_queue][delay].present?
181
- @delay_queue[work_queue][delay] ||= self.channel.queue(
182
- work_queue + '_delayed_' + delay.to_s, auto_delete: false, durable: true,
183
- arguments: {'x-dead-letter-exchange' => 'coney_island', 'x-message-ttl' => delay * 1000})
184
- @delay_queue[work_queue][delay].bind(self.delay_exchange, routing_key: 'carousels.' + work_queue + ".#{delay}")
185
- end
186
- self.delay_exchange.publish(job_args.to_json, {routing_key: "carousels.#{work_queue}.#{delay}"}) do
187
- RequestStore.store[:jobs].delete job_id if RequestStore.store[:jobs] && job_id.present?
188
- end
189
- else
190
- self.exchange.publish(job_args.to_json, {routing_key: "carousels.#{work_queue}"}) do
191
- RequestStore.store[:jobs].delete job_id if RequestStore.store[:jobs] && job_id.present?
192
- end
193
- end
194
- end
195
- true
152
+ # Map arguments
153
+ klass, method_name, job_args = *args
154
+ # Job args is optional
155
+ job_args ||= {}
156
+
157
+ # Check arguments
158
+ # Break if klass isn't a Class or a Module
159
+ raise ConeyIsland::JobArgumentError.new "Expected #{klass} to be a Class or Module" unless [Class, Module].any? {|k| klass.is_a?(k)}
160
+ # Break if method_name isn't a String or a Symbol
161
+ raise ConeyIsland::JobArgumentError.new "Expected #{method_name} to be a String or a Symbol" unless [String,Symbol].any? {|k| method_name.is_a?(k)}
162
+
163
+ # Set defaults
164
+ job_args['klass'] = klass.name
165
+ job_args['method_name'] = method_name
166
+ job_args.stringify_keys!
167
+
168
+ # Extract non job args
169
+ delay = job_args.delete 'delay'
170
+ work_queue = job_args.delete 'work_queue'
171
+
172
+ # Set class defaults if they exist
173
+ if klass.included_modules.include?(Performer)
174
+ delay ||= klass.get_coney_settings[:delay]
175
+ work_queue ||= klass.get_coney_settings[:work_queue]
176
+ end
177
+
178
+ # Set our own defaults if we still don't have any
179
+ work_queue ||= ConeyIsland.default_settings[:work_queue]
180
+ delay ||= ConeyIsland.default_settings[:delay]
181
+
182
+ if self.running_inline?
183
+ # Just run this inline if we're not threaded
184
+ ConeyIsland::Job.new(nil, job_args).handle_job
185
+ elsif delay && delay.to_i > 0
186
+ # Is this delayed?
187
+ # Publish to the delay exchange
188
+ publish_to_delay_queue(job_id, job_args, work_queue, delay)
196
189
  else
197
- raise ConeyIsland::JobArgumentError.new
190
+ # Publish to the normal exchange
191
+ publish_to_queue(self.exchange, job_id, job_args, work_queue)
198
192
  end
193
+
194
+ true
199
195
  end
200
196
 
201
197
  def self.cache_jobs
@@ -211,6 +207,30 @@ module ConeyIsland
211
207
  RequestStore.store[:cache_jobs] = false
212
208
  end
213
209
 
210
+ protected
211
+
212
+ # Publishes a job to a delayed queue exchange
213
+ def self.publish_to_delay_queue(job_id, job_args, work_queue, delay)
214
+ @delay_queue[work_queue] ||= {}
215
+
216
+ # TODO: Should this be in a different little method, say, bind_delay?
217
+ unless @delay_queue[work_queue][delay].present?
218
+ @delay_queue[work_queue][delay] ||= self.channel.queue(
219
+ work_queue + '_delayed_' + delay.to_s, auto_delete: false, durable: true,
220
+ arguments: {'x-dead-letter-exchange' => 'coney_island', 'x-message-ttl' => delay * 1000})
221
+ @delay_queue[work_queue][delay].bind(self.delay_exchange, routing_key: 'carousels.' + work_queue + ".#{delay}")
222
+ end
223
+
224
+ publish_to_queue(self.delay_exchange, job_id, job_args, "#{work_queue}.#{delay}")
225
+ end
226
+
227
+ # Publishes a job to a given exchange
228
+ def self.publish_to_queue(exchange, job_id, job_args, queue)
229
+ exchange.publish(job_args.to_json, {routing_key: "carousels.#{queue}"}) do
230
+ RequestStore.store[:jobs].delete job_id if RequestStore.store[:jobs] && job_id.present?
231
+ end
232
+ end
233
+
214
234
  end
215
235
  end
216
236
 
@@ -1,3 +1,3 @@
1
1
  module ConeyIsland
2
- VERSION = "0.12.3"
2
+ VERSION = "0.12.4"
3
3
  end
@@ -138,3 +138,33 @@ ConeyIsland::Submitter.submit! about to iterate over this many jobs: 1
138
138
  ConeyIsland::Submitter.handle_connection connecting...
139
139
  Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
140
140
  ConeyIsland::Submitter.handle_connection connecting...
141
+ ConeyIsland::Submitter.submit! about to iterate over this many jobs: 1
142
+ ConeyIsland::Submitter.handle_connection connecting...
143
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
144
+ ConeyIsland::Submitter.handle_connection connecting...
145
+ ConeyIsland::Submitter.handle_connection connecting...
146
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
147
+ ConeyIsland::Submitter.handle_connection connecting...
148
+ ConeyIsland::Submitter.submit! about to iterate over this many jobs: 1
149
+ ConeyIsland::Submitter.handle_connection connecting...
150
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
151
+ ConeyIsland::Submitter.handle_connection connecting...
152
+ ConeyIsland::Submitter.handle_connection connecting...
153
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
154
+ ConeyIsland::Submitter.handle_connection connecting...
155
+ ConeyIsland::Submitter.handle_connection connecting...
156
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
157
+ ConeyIsland::Submitter.handle_connection connecting...
158
+ ConeyIsland::Submitter.handle_connection connecting...
159
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
160
+ ConeyIsland::Submitter.handle_connection connecting...
161
+ ConeyIsland::Submitter.handle_connection connecting...
162
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
163
+ ConeyIsland::Submitter.handle_connection connecting...
164
+ ConeyIsland::Submitter.handle_connection connecting...
165
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
166
+ ConeyIsland::Submitter.handle_connection connecting...
167
+ ConeyIsland::Submitter.handle_connection connecting...
168
+ Failed to connecto to RabbitMQ Attempt #1 time(s), trying again in 0 seconds...
169
+ ConeyIsland::Submitter.handle_connection connecting...
170
+ ConeyIsland::Submitter.submit! about to iterate over this many jobs: 1
@@ -27,6 +27,20 @@ class SubmitterTest < MiniTest::Test
27
27
  end
28
28
  end
29
29
  describe "error handling" do
30
+ it "breaks if klass is not a class or a Module" do
31
+ error = assert_raises(ConeyIsland::JobArgumentError) do
32
+ ConeyIsland::Submitter.publish_job([:not_a_class, :method])
33
+ end
34
+ assert_match /to be a Class or Module/, error.message
35
+ end
36
+
37
+ it "breaks if the method_name is not a String or a Symbol" do
38
+ error = assert_raises(ConeyIsland::JobArgumentError) do
39
+ ConeyIsland::Submitter.publish_job([Class, 1])
40
+ end
41
+ assert_match /to be a String or a Symbol/, error.message
42
+ end
43
+
30
44
  it "handles argument errors for jobs" do
31
45
  assert_raises(ConeyIsland::JobArgumentError) do
32
46
  ConeyIsland::Submitter.publish_job([:not_a_class, :add_to_list, args: [[]]])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coney_island
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.3
4
+ version: 0.12.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Draut