philotic 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/simple_singleton.rb +2 -2
- data/examples/subscribing/acks.rb +3 -3
- data/examples/subscribing/consumer.rb +4 -4
- data/lib/philotic/consumer.rb +18 -14
- data/lib/philotic/version.rb +1 -1
- data/spec/philotic/consumer_spec.rb +25 -11
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 9801556e2c18c1147b0de999eeef96c1b3b00cf4
         | 
| 4 | 
            +
              data.tar.gz: d43caf8f1c8aa98e67aec1b0ed11edee9aea22e2
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3acc57d92689ff4dd1de133e968c26d7321391aab11ff532a1cd3d997f483302ae7920f387cbb7dd351a10516ccee67c596375e485d3188f5feccdf4bf8ae0e0
         | 
| 7 | 
            +
              data.tar.gz: 2101fd35dceb87a98ef72e9327bec7497d271428bda282f8c7e58fc61dc2644362f5ebf293f847dd4b649bf58f7f340d94b2f2672bb45af6d5f85642c7e0680c
         | 
| @@ -11,7 +11,7 @@ Philotic.config.message_return_handler = lambda do |basic_return, metadata, mess | |
| 11 11 | 
             
            end
         | 
| 12 12 |  | 
| 13 13 | 
             
            Philotic.subscribe(header_key: 'header_1') do |message|
         | 
| 14 | 
            -
              ap  | 
| 14 | 
            +
              ap message.attributes
         | 
| 15 15 | 
             
            end
         | 
| 16 16 |  | 
| 17 17 | 
             
            # normally we'd do:
         | 
| @@ -23,5 +23,5 @@ end | |
| 23 23 | 
             
            loop do
         | 
| 24 24 | 
             
              Philotic::Message.publish({header_key: "header_#{[1, 2].sample}"}, {payload_key: 'payload_value'})
         | 
| 25 25 | 
             
              # only send a message every two seconds so we can see whats going on
         | 
| 26 | 
            -
              sleep  | 
| 26 | 
            +
              sleep 0.1
         | 
| 27 27 | 
             
            end
         | 
| @@ -6,19 +6,19 @@ require 'philotic' | |
| 6 6 | 
             
            require 'awesome_print'
         | 
| 7 7 |  | 
| 8 8 | 
             
            # sometimes ack
         | 
| 9 | 
            -
            Philotic.subscribe('flaky_queue',  | 
| 9 | 
            +
            Philotic.subscribe('flaky_queue', manual_ack: true) do |message|
         | 
| 10 10 | 
             
              ap message.attributes
         | 
| 11 11 | 
             
               [true, false].sample ? acknowledge(message) : reject(message)
         | 
| 12 12 | 
             
            end
         | 
| 13 13 |  | 
| 14 14 | 
             
            # always ack
         | 
| 15 | 
            -
            Philotic.subscribe('flaky_queue',  | 
| 15 | 
            +
            Philotic.subscribe('flaky_queue', manual_ack: true) do |message|
         | 
| 16 16 | 
             
              ap message.attributes
         | 
| 17 17 | 
             
              acknowledge(message, true)
         | 
| 18 18 | 
             
            end
         | 
| 19 19 |  | 
| 20 20 | 
             
            # always reject
         | 
| 21 | 
            -
            Philotic.subscribe('flaky_queue',  | 
| 21 | 
            +
            Philotic.subscribe('flaky_queue', manual_ack: true) do |message|
         | 
| 22 22 | 
             
              ap message.attributes
         | 
| 23 23 | 
             
              reject message
         | 
| 24 24 | 
             
            end
         | 
| @@ -10,16 +10,16 @@ class NamedQueueConsumer < Philotic::Consumer | |
| 10 10 | 
             
              # subscribe to an existing named queue
         | 
| 11 11 | 
             
              subscribe_to :test_queue
         | 
| 12 12 |  | 
| 13 | 
            -
              #use acknowledgements
         | 
| 14 | 
            -
               | 
| 13 | 
            +
              # use acknowledgements
         | 
| 14 | 
            +
              auto_acknowledge
         | 
| 15 15 |  | 
| 16 16 | 
             
              # REQUEUE the message with RabbitMQ if consume throws these errors. I.e., something went wrong with the consumer
         | 
| 17 17 | 
             
              # Only valid with ack_messages
         | 
| 18 | 
            -
              requeueable_errors PossiblyTransientErrorOne, PossiblyTransientErrorTwo
         | 
| 18 | 
            +
              # requeueable_errors PossiblyTransientErrorOne, PossiblyTransientErrorTwo
         | 
| 19 19 |  | 
| 20 20 | 
             
              # REJECT the message with RabbitMQ if consume throws these errors. I.e., The message is malformed/invalid
         | 
| 21 21 | 
             
              # Only valid with ack_messages
         | 
| 22 | 
            -
              rejectable_errors BadMessageError
         | 
| 22 | 
            +
              # rejectable_errors BadMessageError
         | 
| 23 23 |  | 
| 24 24 | 
             
              def consume(message)
         | 
| 25 25 | 
             
                ap named: message.attributes
         | 
    
        data/lib/philotic/consumer.rb
    CHANGED
    
    | @@ -5,7 +5,6 @@ module Philotic | |
| 5 5 | 
             
              class Consumer < Philotic::Subscriber
         | 
| 6 6 |  | 
| 7 7 | 
             
                class << self
         | 
| 8 | 
            -
             | 
| 9 8 | 
             
                  def subscribe_to(subscription)
         | 
| 10 9 | 
             
                    @subscription = subscription
         | 
| 11 10 | 
             
                  end
         | 
| @@ -14,16 +13,24 @@ module Philotic | |
| 14 13 | 
             
                    @subscription
         | 
| 15 14 | 
             
                  end
         | 
| 16 15 |  | 
| 17 | 
            -
                  def  | 
| 18 | 
            -
                    @ | 
| 16 | 
            +
                  def manually_acknowledge
         | 
| 17 | 
            +
                    @manually_acknowledge = true
         | 
| 19 18 | 
             
                  end
         | 
| 20 19 |  | 
| 21 | 
            -
                  def  | 
| 22 | 
            -
                     | 
| 20 | 
            +
                  def manually_acknowledge?
         | 
| 21 | 
            +
                    !!@manually_acknowledge
         | 
| 23 22 | 
             
                  end
         | 
| 24 23 |  | 
| 25 | 
            -
                  def  | 
| 26 | 
            -
                     | 
| 24 | 
            +
                  def auto_acknowledge
         | 
| 25 | 
            +
                    @auto_acknowledge = true
         | 
| 26 | 
            +
                  end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def auto_acknowledge?
         | 
| 29 | 
            +
                    !!@auto_acknowledge
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  def exclusive
         | 
| 33 | 
            +
                    @exclusive = true
         | 
| 27 34 | 
             
                  end
         | 
| 28 35 |  | 
| 29 36 | 
             
                  def exclusive?
         | 
| @@ -50,11 +57,11 @@ module Philotic | |
| 50 57 |  | 
| 51 58 | 
             
                  def subscription_options
         | 
| 52 59 | 
             
                    {
         | 
| 53 | 
            -
                      manual_ack:  | 
| 60 | 
            +
                      manual_ack: auto_acknowledge? || manually_acknowledge?,
         | 
| 54 61 | 
             
                      exclusive:  exclusive?,
         | 
| 55 62 | 
             
                    }
         | 
| 56 63 | 
             
                  end
         | 
| 57 | 
            -
                end
         | 
| 64 | 
            +
                end # end class methods
         | 
| 58 65 |  | 
| 59 66 | 
             
                def subscribe
         | 
| 60 67 | 
             
                  super(self.class.subscription, self.class.subscription_options) do |message|
         | 
| @@ -69,22 +76,19 @@ module Philotic | |
| 69 76 | 
             
                private
         | 
| 70 77 |  | 
| 71 78 | 
             
                def _consume(message)
         | 
| 72 | 
            -
                  if self.class. | 
| 79 | 
            +
                  if self.class.auto_acknowledge?
         | 
| 73 80 | 
             
                    begin
         | 
| 74 81 | 
             
                      consume(message)
         | 
| 82 | 
            +
                      acknowledge(message)
         | 
| 75 83 | 
             
                    rescue => e
         | 
| 76 | 
            -
             | 
| 77 84 | 
             
                      if self.class.requeueable_errors.include? e.class
         | 
| 78 85 | 
             
                        reject(message, true)
         | 
| 79 | 
            -
                        return
         | 
| 80 86 | 
             
                      elsif self.class.rejectable_errors.include? e.class
         | 
| 81 87 | 
             
                        reject(message, false)
         | 
| 82 | 
            -
                        return
         | 
| 83 88 | 
             
                      else
         | 
| 84 89 | 
             
                        raise e
         | 
| 85 90 | 
             
                      end
         | 
| 86 91 | 
             
                    end
         | 
| 87 | 
            -
                    acknowledge(message)
         | 
| 88 92 | 
             
                  else
         | 
| 89 93 | 
             
                    consume(message)
         | 
| 90 94 | 
             
                  end
         | 
    
        data/lib/philotic/version.rb
    CHANGED
    
    
| @@ -27,11 +27,19 @@ describe Philotic::Consumer do | |
| 27 27 | 
             
                end
         | 
| 28 28 | 
             
              end
         | 
| 29 29 |  | 
| 30 | 
            -
              describe '. | 
| 31 | 
            -
                it 'sets the class variable @ | 
| 32 | 
            -
                  expect(subject | 
| 33 | 
            -
                  subject. | 
| 34 | 
            -
                  expect(subject | 
| 30 | 
            +
              describe '.manually_acknowledge' do
         | 
| 31 | 
            +
                it 'sets the class variable @manually_acknowledge' do
         | 
| 32 | 
            +
                  expect(subject).not_to be_manually_acknowledge
         | 
| 33 | 
            +
                  subject.manually_acknowledge
         | 
| 34 | 
            +
                  expect(subject).to be_manually_acknowledge
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              describe '.auto_acknowledge' do
         | 
| 39 | 
            +
                it 'sets the class variable @auto_acknowledge' do
         | 
| 40 | 
            +
                  expect(subject).not_to be_auto_acknowledge
         | 
| 41 | 
            +
                  subject.auto_acknowledge
         | 
| 42 | 
            +
                  expect(subject).to be_auto_acknowledge
         | 
| 35 43 | 
             
                end
         | 
| 36 44 | 
             
              end
         | 
| 37 45 |  | 
| @@ -91,7 +99,13 @@ describe Philotic::Consumer do | |
| 91 99 | 
             
                it 'returns a hash with the exclusive and manual_ack options' do
         | 
| 92 100 | 
             
                  expect(subject.subscription_options).to match({manual_ack: false, exclusive: false})
         | 
| 93 101 |  | 
| 94 | 
            -
                  subject. | 
| 102 | 
            +
                  subject.auto_acknowledge
         | 
| 103 | 
            +
                  expect(subject.subscription_options).to match({manual_ack: true, exclusive: false})
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                  subject.instance_variable_set(:@auto_acknowledge, nil)
         | 
| 106 | 
            +
                  expect(subject.subscription_options).to match({manual_ack: false, exclusive: false})
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                  subject.manually_acknowledge
         | 
| 95 109 | 
             
                  expect(subject.subscription_options).to match({manual_ack: true, exclusive: false})
         | 
| 96 110 |  | 
| 97 111 | 
             
                  subject.exclusive
         | 
| @@ -102,7 +116,7 @@ describe Philotic::Consumer do | |
| 102 116 | 
             
              describe '#subscribe' do
         | 
| 103 117 | 
             
                it 'proxies to Philotic::Subscriber#subscribe' do
         | 
| 104 118 | 
             
                  subject.subscribe_to named_queue
         | 
| 105 | 
            -
                  subject. | 
| 119 | 
            +
                  subject.auto_acknowledge
         | 
| 106 120 | 
             
                  subject.exclusive
         | 
| 107 121 |  | 
| 108 122 | 
             
                  expect_any_instance_of(Philotic::Subscriber).to receive(:subscribe).with(named_queue, manual_ack: true, exclusive: true)
         | 
| @@ -136,7 +150,7 @@ describe Philotic::Consumer do | |
| 136 150 | 
             
                end
         | 
| 137 151 |  | 
| 138 152 | 
             
                it 'acknowledges messages when @ack_messages is set' do
         | 
| 139 | 
            -
                  subject.class. | 
| 153 | 
            +
                  subject.class.auto_acknowledge
         | 
| 140 154 |  | 
| 141 155 | 
             
                  subject.define_singleton_method :consume do |message|
         | 
| 142 156 | 
             
                    # no op
         | 
| @@ -148,7 +162,7 @@ describe Philotic::Consumer do | |
| 148 162 | 
             
                end
         | 
| 149 163 |  | 
| 150 164 | 
             
                it 'requeues messages when @ack_messages is set and a requeueable error is thrown' do
         | 
| 151 | 
            -
                  subject.class. | 
| 165 | 
            +
                  subject.class.auto_acknowledge
         | 
| 152 166 | 
             
                  subject.class.requeueable_errors(RuntimeError)
         | 
| 153 167 |  | 
| 154 168 | 
             
                  subject.define_singleton_method :consume do |message|
         | 
| @@ -161,7 +175,7 @@ describe Philotic::Consumer do | |
| 161 175 | 
             
                end
         | 
| 162 176 |  | 
| 163 177 | 
             
                it 'rejects messages when @ack_messages is set and a rejectable error is thrown' do
         | 
| 164 | 
            -
                  subject.class. | 
| 178 | 
            +
                  subject.class.auto_acknowledge
         | 
| 165 179 | 
             
                  subject.class.rejectable_errors(RuntimeError)
         | 
| 166 180 |  | 
| 167 181 | 
             
                  subject.define_singleton_method :consume do |message|
         | 
| @@ -174,7 +188,7 @@ describe Philotic::Consumer do | |
| 174 188 | 
             
                end
         | 
| 175 189 |  | 
| 176 190 | 
             
                it 'raises all non-requeueable and non-rejectable errors' do
         | 
| 177 | 
            -
                  subject.class. | 
| 191 | 
            +
                  subject.class.auto_acknowledge
         | 
| 178 192 |  | 
| 179 193 | 
             
                  subject.define_singleton_method :consume do |message|
         | 
| 180 194 | 
             
                    raise RuntimeError.new 'oops'
         |