sqewer 3.0.1 → 4.0.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
  SHA1:
3
- metadata.gz: edded56859437d9b7bce9424d2c00b4a8d999756
4
- data.tar.gz: aaa87e0f5ab8a4fe1b0c73682e4282bd7b52534b
3
+ metadata.gz: 3df44f7b0b7b55d48c513d4573c16c22051a6452
4
+ data.tar.gz: 2967480bcb890b11fa117135e7a4f3541d9724be
5
5
  SHA512:
6
- metadata.gz: 8c0394391564b80cd5148dfb4446561ae29902e4e05fc62360b0d0fa454effb25d6bc62978f3b2a81fcd3021c7cc59afbad8031c6bb5fcef1cfd18965e87f233
7
- data.tar.gz: 7daa39ca0ee05abf18c65f9bfbe59d334160522243a3a1fb0589ba413070893d89f0896e57597506678459951337bb29a4fa686fb6442d99ae47aef8f7f65d35
6
+ metadata.gz: 8fd9e0949bf88525d352b70b46899e91ecef4865d2b60a64348d5ea3587052e18fa6d3d05d12f62a46f31420eac0a80920ebbe4273242d1f2f85a834e9e249f5
7
+ data.tar.gz: 2f06335925254b2e923fce2bb979dc10cfa10232a61f16d6ffd47d745678204889167504f008b45c3bdd38419595b7b51d135306ea0f0782548c9472f122e6d0
@@ -0,0 +1,50 @@
1
+ require 'thread'
2
+
3
+ # A recorder for send_message and delete_message calls.
4
+ # Will buffer those calls as if it were a Connection, and then execute
5
+ # them within a synchronized mutex lock, to prevent concurrent submits
6
+ # from the Connection object
7
+ class Sqewer::ConnectionMessagebox
8
+ class MethodCall < Struct.new(:method_name, :posargs, :kwargs)
9
+ def perform(on)
10
+ if kwargs && posargs
11
+ on.public_send(method_name, *posargs, **kwargs)
12
+ elsif kwargs
13
+ on.public_send(method_name, **kwargs)
14
+ elsif posargs
15
+ on.public_send(method_name, *posargs)
16
+ else
17
+ on.public_send(method_name)
18
+ end
19
+ end
20
+ end
21
+
22
+ def initialize(connection)
23
+ @connection = connection
24
+ @queue = Queue.new
25
+ @mux = Mutex.new
26
+ end
27
+
28
+ def receive_messages
29
+ @connection.receive_messages
30
+ end
31
+
32
+ def send_message(message_body, **kwargs_for_send)
33
+ @queue << MethodCall.new(:send_message, [message_body], kwargs_for_send)
34
+ end
35
+
36
+ def delete_message(message_identifier)
37
+ @queue << MethodCall.new(:delete_message, [message_identifier], nil)
38
+ end
39
+
40
+ def flush!
41
+ @mux.synchronize do
42
+ executed = 0
43
+ while @queue.length.nonzero?
44
+ @queue.pop.perform(@connection)
45
+ executed += 1
46
+ end
47
+ executed
48
+ end
49
+ end
50
+ end
@@ -39,23 +39,31 @@ class Sqewer::Isolator
39
39
  worker.submitter_class, worker.execution_context_class,
40
40
  worker.middleware_stack, worker.connection, worker.serializer, worker.logger
41
41
 
42
+ box = Sqewer::ConnectionMessagebox.new(connection)
43
+
42
44
  job = middleware_stack.around_deserialization(serializer, message.receipt_handle, message.body) do
43
45
  serializer.unserialize(message.body)
44
46
  end
45
47
  return unless job
46
48
 
47
- submitter = submitter_class.new(connection, serializer)
49
+ submitter = submitter_class.new(box, serializer)
48
50
  context = execution_context_class.new(submitter, {'logger' => logger})
49
51
 
50
52
  t = Time.now
51
53
  middleware_stack.around_execution(job, context) do
52
54
  job.method(:run).arity.zero? ? job.run : job.run(context)
53
55
  end
54
- # FIXME: do not mix templating and format strings
56
+
57
+ # Perform two flushes, one for any possible jobs the job has spawned,
58
+ # and one for the job delete afterwards
59
+ box.delete_message(message.receipt_handle)
60
+
55
61
  delta = Time.now - t
56
62
  logger.info { "[worker] Finished %s in %0.2fs" % [job.inspect, delta] }
57
63
  rescue => e
58
- logger.error { "[worker] Failed #{job.inspect} with a #{e}" } if job
59
64
  raise e
65
+ ensure
66
+ n_flushed = box.flush!
67
+ logger.info { "[worker] Flushed %d messages" % n_flushed } if n_flushed.nonzero?
60
68
  end
61
69
  end
@@ -1,3 +1,3 @@
1
1
  module Sqewer
2
- VERSION = '3.0.1'
2
+ VERSION = '4.0.0'
3
3
  end
data/lib/sqewer/worker.rb CHANGED
@@ -95,8 +95,8 @@ class Sqewer::Worker
95
95
  consumers = (1..@num_threads).map do
96
96
  Thread.new do
97
97
  loop {
98
- break if @state.in_state?(:stopping)
99
98
  take_and_execute
99
+ break if stopping?
100
100
  }
101
101
  end
102
102
  end
@@ -180,7 +180,7 @@ class Sqewer::Worker
180
180
  return unless message.receipt_handle
181
181
  return @connection.delete_message(message.receipt_handle) unless message.has_body?
182
182
  @isolator.perform(self, message)
183
- @connection.delete_message(message.receipt_handle)
183
+ # The message delete happens within the Isolator
184
184
  end
185
185
 
186
186
  def take_and_execute
@@ -190,11 +190,7 @@ class Sqewer::Worker
190
190
  sleep SLEEP_SECONDS_ON_EMPTY_QUEUE
191
191
  Thread.pass
192
192
  rescue => e # anything else, at or below StandardError that does not need us to quit
193
- @logger.error { "[worker] Failed #{message.inspect} with #{e}" }
194
- @logger.error(e.class)
195
- @logger.error(e.message)
193
+ @logger.error { "[worker] Failed %s... with %s: %s" % [message.inspect[0..64], e.class, e.message] }
196
194
  e.backtrace.each { |s| @logger.error{"\t#{s}"} }
197
195
  end
198
-
199
- STR_logger = 'logger'
200
196
  end
data/sqewer.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: sqewer 3.0.1 ruby lib
5
+ # stub: sqewer 4.0.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "sqewer"
9
- s.version = "3.0.1"
9
+ s.version = "4.0.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Julik Tarkhanov"]
14
- s.date = "2016-02-03"
14
+ s.date = "2016-02-04"
15
15
  s.description = "Process jobs from SQS"
16
16
  s.email = "me@julik.nl"
17
17
  s.extra_rdoc_files = [
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/sqewer/atomic_counter.rb",
32
32
  "lib/sqewer/cli.rb",
33
33
  "lib/sqewer/connection.rb",
34
+ "lib/sqewer/connection_messagebox.rb",
34
35
  "lib/sqewer/contrib/appsignal_wrapper.rb",
35
36
  "lib/sqewer/contrib/performable.rb",
36
37
  "lib/sqewer/execution_context.rb",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqewer
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julik Tarkhanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-03 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -212,6 +212,7 @@ files:
212
212
  - lib/sqewer/atomic_counter.rb
213
213
  - lib/sqewer/cli.rb
214
214
  - lib/sqewer/connection.rb
215
+ - lib/sqewer/connection_messagebox.rb
215
216
  - lib/sqewer/contrib/appsignal_wrapper.rb
216
217
  - lib/sqewer/contrib/performable.rb
217
218
  - lib/sqewer/execution_context.rb