fastly_nsq 0.10.1 → 0.12.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: e97d45e36727661d9562ff471038e7760086e079
4
- data.tar.gz: 3061cd5fd59e767ee80419de54260728c7a2414a
3
+ metadata.gz: ec5180b8b65ede93fdbb509c673c8bb539f9010e
4
+ data.tar.gz: 58705d4fe6bd3fb791803f393db63fb53b57c3ae
5
5
  SHA512:
6
- metadata.gz: 34b7cece9416006546dd8621953964e30dcb0eec305bf25979f68b1ba41557fc67ecbc99ab99455d68535db4fe2cfa398809a6dce306fe674da95a511fe3cdba
7
- data.tar.gz: 0fa641c48320197ef521e714bb5713adcf0f98d26f17fd28ba250150b95ad65cbd9cc50ac1e4ac8c0b0bf0dfab3267cd8a453338cfa9ff99291cb5be94e005f5
6
+ metadata.gz: 787a47a30094daac20acc0db56e9a257716be62f2a212df288fa2c488646508bc49fd8589d034b661bbdf38183ddba1ad627c10967796a63cbeb29f8fc3c66de
7
+ data.tar.gz: c0cfe65d8a15bca78def1d826f98ce4d632f6b09a581298e37ebfa8e4a3fca94d2e2b5b12473015291c881886e3ffa8cab442d1276d8620c755e7b1d06d59b8a
data/lib/fastly_nsq.rb CHANGED
@@ -12,6 +12,8 @@ require 'fastly_nsq/version'
12
12
  module FastlyNsq
13
13
  module_function
14
14
 
15
+ @preprocessor = nil
16
+
15
17
  def channel=(channel)
16
18
  @channel ||= channel
17
19
  end
@@ -20,7 +22,7 @@ module FastlyNsq
20
22
  strategy.logger = logger
21
23
  end
22
24
 
23
- def self.preprocessor=(preprocessor)
25
+ def preprocessor=(preprocessor)
24
26
  @preprocessor ||= preprocessor
25
27
  end
26
28
 
@@ -32,7 +34,7 @@ module FastlyNsq
32
34
  strategy.logger
33
35
  end
34
36
 
35
- def self.preprocessor
37
+ def preprocessor
36
38
  @preprocessor
37
39
  end
38
40
 
@@ -55,11 +55,20 @@ class FastlyNsq::CLI
55
55
  end
56
56
 
57
57
  def startup
58
+ boot_rails
58
59
  require options[:require] if options[:require]
59
60
  FastlyNsq.logger.info "Running in #{RUBY_DESCRIPTION}"
60
61
  FastlyNsq.logger.info 'Starting processing, hit Ctrl-C to stop' unless options[:daemon]
61
62
  end
62
63
 
64
+ def boot_rails
65
+ return unless ENV['RAILS_ENV']
66
+
67
+ require 'rails'
68
+ require File.expand_path('./config/application.rb')
69
+ require File.expand_path('./config/environment.rb')
70
+ end
71
+
63
72
  def parse(args)
64
73
  opts = {}
65
74
 
@@ -94,8 +94,9 @@ module FastlyNsq
94
94
  end
95
95
 
96
96
  def next_message
97
- message = @consumer.pop # TODO: consumer.pop do |message|
98
- result = yield FastlyNsq::Message.new(message.body)
97
+ nsq_message = @consumer.pop # TODO: consumer.pop do |message|
98
+ message = FastlyNsq::Message.new(nsq_message)
99
+ result = yield message
99
100
  message.finish if result
100
101
  end
101
102
 
@@ -1,11 +1,16 @@
1
1
  require 'json'
2
2
 
3
3
  class FastlyNsq::Message
4
- attr_reader :raw_body
4
+ extend Forwardable
5
+
6
+ def_delegators :@nsq_message, :attempts, :touch, :timestamp
7
+
8
+ attr_reader :managed, :nsq_message, :raw_body
5
9
  alias to_s raw_body
6
10
 
7
- def initialize(raw_body)
8
- @raw_body = raw_body
11
+ def initialize(nsq_message)
12
+ @nsq_message = nsq_message
13
+ @raw_body = nsq_message.body
9
14
  end
10
15
 
11
16
  def data
@@ -15,4 +20,18 @@ class FastlyNsq::Message
15
20
  def body
16
21
  @body ||= JSON.parse(raw_body)
17
22
  end
23
+
24
+ def finish
25
+ return managed if managed
26
+
27
+ @managed = :finished
28
+ nsq_message.finish
29
+ end
30
+
31
+ def requeue(*args)
32
+ return managed if managed
33
+
34
+ @managed = :requeued
35
+ nsq_message.requeue(*args)
36
+ end
18
37
  end
@@ -1,5 +1,10 @@
1
1
  module FastlyNsq::Messenger
2
- def self.deliver(message:, on_topic:, originating_service: nil)
2
+ DEFAULT_ORIGIN = 'Unknown'.freeze
3
+ @originating_service = DEFAULT_ORIGIN
4
+
5
+ module_function
6
+
7
+ def deliver(message:, on_topic:, originating_service: nil)
3
8
  payload = {
4
9
  data: message,
5
10
  meta: {
@@ -12,11 +17,11 @@ module FastlyNsq::Messenger
12
17
  end
13
18
  end
14
19
 
15
- def self.originating_service=(service)
20
+ def originating_service=(service)
16
21
  @originating_service = service
17
22
  end
18
23
 
19
- def self.producer_for(topic:)
24
+ def producer_for(topic:)
20
25
  producer = producers[topic]
21
26
 
22
27
  yield producer if block_given?
@@ -24,25 +29,23 @@ module FastlyNsq::Messenger
24
29
  producer
25
30
  end
26
31
 
27
- def self.producers
32
+ def producers
28
33
  @producers ||= Hash.new { |hash, topic| hash[topic] = FastlyNsq::Producer.new(topic: topic) }
29
34
  end
30
35
 
31
- def self.terminate_producer(topic:)
36
+ def terminate_producer(topic:)
32
37
  producer_for(topic: topic).terminate
33
38
  producers.delete(topic)
34
39
  end
35
40
 
36
- def self.terminate_all_producers
41
+ def terminate_all_producers
37
42
  producers.each do |topic, producer|
38
43
  producer.terminate
39
44
  producers.delete(topic)
40
45
  end
41
46
  end
42
47
 
43
- private_class_method
44
-
45
- def self.originating_service
46
- @originating_service || 'Unknown'.freeze
48
+ def originating_service
49
+ @originating_service || DEFAULT_ORIGIN
47
50
  end
48
51
  end
@@ -8,8 +8,6 @@ module FastlyNsq
8
8
  real_queue || fake_queue || error
9
9
  end
10
10
 
11
- private_class_method
12
-
13
11
  ERR_MESSAGE = "You must set ENV['FAKE_QUEUE'] to either true or false".freeze
14
12
 
15
13
  def error
@@ -1,3 +1,3 @@
1
1
  module FastlyNsq
2
- VERSION = '0.10.1'.freeze
2
+ VERSION = '0.12.0'.freeze
3
3
  end
@@ -2,9 +2,10 @@ require 'spec_helper'
2
2
  require 'json'
3
3
 
4
4
  RSpec.describe FastlyNsq::Message do
5
- let(:body) { { 'data' => 'goes here', 'other_field' => 'is over here' } }
6
- let(:json_body) { body.to_json }
7
- subject { FastlyNsq::Message.new json_body }
5
+ let(:nsq_message) { double 'Nsq::Message', body: json_body, attempts: nil, finish: nil, requeue: nil, touch: nil, timestamp: nil }
6
+ let(:body) { { 'data' => 'goes here', 'other_field' => 'is over here' } }
7
+ let(:json_body) { body.to_json }
8
+ subject { FastlyNsq::Message.new nsq_message }
8
9
 
9
10
  it 'preserves original message body as raw_body' do
10
11
  expect(subject.raw_body).to eq(json_body)
@@ -21,4 +22,33 @@ RSpec.describe FastlyNsq::Message do
21
22
  it 'aliases raw_body to to_s' do
22
23
  expect(subject.to_s).to eq(json_body)
23
24
  end
25
+
26
+ it 'delegates methods to the nsq_message object' do
27
+ %w(attempts finish requeue touch timestamp).each do |method|
28
+ subject = FastlyNsq::Message.new nsq_message
29
+ expect(nsq_message).to receive(method)
30
+
31
+ subject.send(method)
32
+ end
33
+ end
34
+
35
+ it 'does not finish if the message was requeued' do
36
+ expect(nsq_message).to receive(:requeue).with(1000)
37
+ expect(nsq_message).not_to receive(:finish)
38
+
39
+ subject.requeue(1000)
40
+ subject.finish
41
+
42
+ expect(subject.managed).to eq(:requeued)
43
+ end
44
+
45
+ it 'does not requeue if the message was finished' do
46
+ expect(nsq_message).to receive(:finish)
47
+ expect(nsq_message).not_to receive(:requeue)
48
+
49
+ subject.finish
50
+ subject.requeue
51
+
52
+ expect(subject.managed).to eq(:finished)
53
+ end
24
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly_nsq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy O'Neil
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-08-08 00:00:00.000000000 Z
14
+ date: 2017-08-22 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: awesome_print