proletariat 0.0.3 → 0.0.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: 49905f9a8ecb2a3b12f565f7b6c7e07c7cac6e39
4
- data.tar.gz: 76771f1121f542f06a8ee2d9f6f7ffcf07982404
3
+ metadata.gz: f18e4d2458922ab3466f6913a079616c7ab3e5ab
4
+ data.tar.gz: 0ed0637283e0056fca8e260bbb4fe97dc26eca1e
5
5
  SHA512:
6
- metadata.gz: 5a1e573f1fd1028068f5f91dcd5625d4ddf90052d9711b8c8a85d810db0ea4b0440cb51384040c865eea540cbbf48368dc7b2dacdc770e85fa0430b5f63f887d
7
- data.tar.gz: 193ecfabdc6bb3c0d25bda8a8e57a66a77aa6ab830a552f395acc626a88803ef556019556a950201a302089a830d8328fa42690943dca4a9376a345c98200217
6
+ metadata.gz: f6e31240a83223f4d67f2f0e08ae194927f07b6671fa193b32e7ed6527d37e66de29ff7830047cbbeb23a300b19eb43b1ad5765546ac51f406bdabd0d83cecee
7
+ data.tar.gz: 431900e4e0b1f59427666ceeca3197297c3723441c0f472242ee40dda0e3afe148bbc63cde95d10818a65358130a3ea83e46006042696385d6f4d7bd6a07dd3a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.0.4
2
+
3
+ Features:
4
+
5
+ - The routing key is now sent to #work along with the message
6
+ - Workers now log when they publish a message
7
+ - Test guarantors can be used without blocks
8
+ - Amount of worker/publisher threads can be set via env variables
9
+
1
10
  ## 0.0.3
2
11
 
3
12
  Features:
data/README.md CHANGED
@@ -26,7 +26,7 @@ And run:
26
26
 
27
27
  If you aren't using default RabbitMQ connection settings, ensure the `RABBITMQ_URL` env variable is present. Here's how that might look in your `.env` if you use Foreman:
28
28
 
29
- RABBITMQ_URL=amqp://someuser:somepass@127.0.0.1/another_vhost
29
+ RABBITMQ_URL=amqp://someuser:somepass@127.0.0.1/another_vhost
30
30
 
31
31
  ### Setting up a Worker
32
32
 
@@ -38,60 +38,59 @@ The `#work` method should return `:ok` on success or `:drop` / `:requeue` on fai
38
38
 
39
39
  Here's a complete example:
40
40
 
41
- class SendUserIntroductoryEmail < Proletariat::Worker
42
- listen_on 'user.created'
41
+ class SendUserIntroductoryEmail < Proletariat::Worker
42
+ listen_on 'user.created'
43
43
 
44
- def work(message)
45
- params = JSON.parse(message)
44
+ def work(message, key)
45
+ params = JSON.parse(message)
46
46
 
47
- UserMailer.introductory_email(params).deliver!
47
+ UserMailer.introductory_email(params).deliver!
48
48
 
49
- publish 'email_sent.user.introductory', {id: params['id']}.to_json
49
+ publish 'email_sent.user.introductory', {id: params['id']}.to_json
50
50
 
51
- :ok
51
+ :ok
52
+ end
52
53
  end
53
- end
54
54
 
55
- ### Select your Workers
55
+ ### Running your Workers
56
56
 
57
- If you are using Rails just create a `proletariat.rb` file in your initializers directory.
57
+ Define the `WORKERS` env variable.
58
58
 
59
- Proletariat.configure worker_classes: [SendUserIntroductoryEmail, SomeOtherWorker]
60
- Proletariat.run!
59
+ WORKERS=SendUserIntroductoryEmail,SomeOtherWorker
61
60
 
62
- Or define the `WORKERS` env variable.
61
+ Run the rake command.
63
62
 
64
- WORKERS=SendUserIntroductoryEmail,SomeOtherWorker
63
+ bundle exec rake proletariat:run
65
64
 
66
65
  ### Deploying on Heroku
67
66
 
68
67
  It's not recommended to run your background workers in the same process as your main web process. Heroku will shutdown idle `web` processes, killing your background workers in the process. Instead create a new process type for Proletariat by adding the following to your Procfile:
69
68
 
70
- workers: bundle exec rake proletariat:run
69
+ workers: bundle exec rake proletariat:run
71
70
 
72
71
  And run:
73
72
 
74
- heroku ps:scale workers=1
73
+ heroku ps:scale workers=1
75
74
 
76
75
  ### Testing with Cucumber
77
76
 
78
77
  Add the following to your `env.rb`:
79
78
 
80
- require 'proletariat/cucumber'
79
+ require 'proletariat/cucumber'
81
80
 
82
81
  Use the provided helpers in your step definitions to synchronize your test suite with your workers without sacrificing the ability to test in production-like environment:
83
82
 
84
- When(/^I submit a valid 'register user' form$/) do
85
- wait_for message.on_topic('email_sent.user.introductory') do
86
- visit ...
87
- fill_in ...
88
- submit ...
83
+ When(/^I submit a valid 'register user' form$/) do
84
+ wait_for message.on_topic('email_sent.user.introductory') do
85
+ visit ...
86
+ fill_in ...
87
+ submit ...
88
+ end
89
89
  end
90
- end
91
90
 
92
- Then(/^the user should receive an introductory email$/) do
93
- expect(unread_emails_for(new_user_email).size).to eq 1
94
- end
91
+ Then(/^the user should receive an introductory email$/) do
92
+ expect(unread_emails_for(new_user_email).size).to eq 1
93
+ end
95
94
 
96
95
  ## FAQ
97
96
 
@@ -5,10 +5,10 @@ module Proletariat
5
5
  DEFAULT_EXCHANGE_NAME = 'proletariat'
6
6
 
7
7
  # Public: The default number of threads to use for publishers.
8
- DEFAULT_PUBLISHER_THREADS = 2
8
+ DEFAULT_PUBLISHER_THREADS = (ENV['PUBLISHER_THREADS'] || 2).to_i
9
9
 
10
10
  # Public: The default number of threads to use for each worker class.
11
- DEFAULT_WORKER_THREADS = 3
11
+ DEFAULT_WORKER_THREADS = (ENV['WORKER_THREADS'] || 3).to_i
12
12
 
13
13
  # Internal: Sets the RabbitMQ connection.
14
14
  attr_writer :connection
@@ -117,7 +117,7 @@ module Proletariat
117
117
  # Returns nil.
118
118
  def start_consumer
119
119
  @consumer = bunny_queue.subscribe ack: true do |info, properties, body|
120
- future = listener.post?(body)
120
+ future = listener.post?(body, info.routing_key)
121
121
  acknowledgers << Acknowledger.new(future, info.delivery_tag)
122
122
 
123
123
  nil
@@ -30,6 +30,9 @@ module Proletariat
30
30
  end
31
31
 
32
32
  @block = block
33
+ @noop = true if expectations.length == 0
34
+
35
+ run_subscribers
33
36
  end
34
37
 
35
38
  # Public: Execute the blocks and waits for the expectations to be met.
@@ -37,10 +40,10 @@ module Proletariat
37
40
  # Returns nil if expectations are met within timeout.
38
41
  # Raises MessageTimeoutError if expectations are not met within timeout.
39
42
  def guarantee
40
- run_subscribers
41
-
42
43
  block.call if block
43
44
 
45
+ return nil if noop
46
+
44
47
  timer = 0.0
45
48
 
46
49
  until passed?
@@ -49,9 +52,9 @@ module Proletariat
49
52
  timer += MESSAGE_CHECK_INTERVAL
50
53
  end
51
54
 
52
- stop_subscribers
53
-
54
55
  nil
56
+ ensure
57
+ stop_subscribers
55
58
  end
56
59
 
57
60
  private
@@ -63,6 +66,9 @@ module Proletariat
63
66
  # Internal: Returns an array of MessageCounter instances.
64
67
  attr_reader :counters
65
68
 
69
+ # Internal: Returns true if there aren't any expectations.
70
+ attr_reader :noop
71
+
66
72
  # Internal: Returns an array of Subscriber instances.
67
73
  attr_reader :subscribers
68
74
 
@@ -122,7 +128,7 @@ module Proletariat
122
128
  # message - The contents of the message.
123
129
  #
124
130
  # Returns a future-like object holding an :ok Symbol.
125
- def post?(message)
131
+ def post?(message, routing_key)
126
132
  self.count = count + 1
127
133
 
128
134
  Concurrent::Future.new { :ok }
@@ -31,11 +31,12 @@ module Proletariat
31
31
  # # ... [Time passes]
32
32
  # # => MessageTimeoutError
33
33
  #
34
- # Returns nil.
34
+ # Returns ExpectationGuarantor.
35
35
  def wait_for(*expectations, &block)
36
- ExpectationGuarantor.new(expectations, &block).guarantee
36
+ guarantor = ExpectationGuarantor.new(expectations, &block)
37
+ guarantor.guarantee if block.present?
37
38
 
38
- nil
39
+ guarantor
39
40
  end
40
41
  end
41
42
  end
@@ -1,4 +1,4 @@
1
1
  # Public: Adds a constant for the current version number.
2
2
  module Proletariat
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
@@ -38,7 +38,7 @@ module Proletariat
38
38
  # message - The incoming message.
39
39
  #
40
40
  # Raises NotImplementedError unless implemented in subclass.
41
- def work(message)
41
+ def work(message, routing_key)
42
42
  fail NotImplementedError
43
43
  end
44
44
 
@@ -74,6 +74,7 @@ module Proletariat
74
74
  #
75
75
  # Returns nil.
76
76
  def publish(to, message = '')
77
+ log "Publishing to: #{to}"
77
78
  Proletariat.publish to, message
78
79
 
79
80
  nil
data/lib/proletariat.rb CHANGED
@@ -44,7 +44,7 @@ module Proletariat
44
44
 
45
45
  def method_missing(method_sym, *arguments, &block)
46
46
  if config.respond_to? method_sym
47
- config.send method_sym
47
+ config.send(method_sym, *arguments, &block)
48
48
  else
49
49
  super
50
50
  end
@@ -8,7 +8,7 @@ class PingWorker < Proletariat::Worker
8
8
  attr_accessor :pinged
9
9
  end
10
10
 
11
- def work(message)
11
+ def work(message, routing_key)
12
12
  self.class.pinged = true
13
13
 
14
14
  log 'PING'
@@ -27,7 +27,7 @@ class PongWorker < Proletariat::Worker
27
27
  attr_accessor :ponged
28
28
  end
29
29
 
30
- def work(message)
30
+ def work(message, routing_key)
31
31
  self.class.ponged = true
32
32
 
33
33
  log 'PONG'
@@ -34,19 +34,19 @@ module Proletariat
34
34
 
35
35
  it 'should increment the count' do
36
36
  counter = ExpectationGuarantor::MessageCounter.new(1)
37
- counter.post?('message')
37
+ counter.post?('message', 'key')
38
38
  expect(counter.expected_messages_received?).to be_truthy
39
39
  end
40
40
 
41
41
  it 'should return a future containing :ok' do
42
42
  counter = ExpectationGuarantor::MessageCounter.new(1)
43
43
  expect(Concurrent::Future).to receive(:new)
44
- counter.post?('message')
44
+ counter.post?('message', 'key')
45
45
  end
46
46
 
47
47
  it 'should ensure the returned future contains :ok' do
48
48
  counter = ExpectationGuarantor::MessageCounter.new(1)
49
- future = counter.post?('message')
49
+ future = counter.post?('message', 'key')
50
50
  expect(future.block.call).to eq :ok
51
51
  end
52
52
  end
@@ -2,7 +2,7 @@ require 'proletariat/worker'
2
2
 
3
3
  module Proletariat
4
4
  describe Worker do
5
- let(:logger) { double }
5
+ let(:logger) { double.as_null_object }
6
6
 
7
7
  before do
8
8
  allow(Proletariat).to receive(:logger).and_return(logger)
@@ -31,7 +31,7 @@ module Proletariat
31
31
 
32
32
  describe '#work' do
33
33
  it 'should raise NotImplementedError' do
34
- expect { Worker.new.work('message') }.to \
34
+ expect { Worker.new.work('message', 'key') }.to \
35
35
  raise_exception NotImplementedError
36
36
  end
37
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proletariat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Edwards
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-25 00:00:00.000000000 Z
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec