watership 0.7.4 → 0.8.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +13 -5
  2. data/lib/watership/version.rb +1 -1
  3. data/lib/watership.rb +75 -28
  4. metadata +12 -12
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f426f32fd102d92fe6af3346c5a097e7fc2b8f09
4
- data.tar.gz: 79283ca32d717adc9592faf08ed75006208666f8
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MThkNjNkYjBiNDBmMGQ4NzllNWE2YjgwMWJjMzc3MjMwMmQ2N2JhMw==
5
+ data.tar.gz: !binary |-
6
+ ZjA3OGE1MjdhOWM3M2ViZDlhN2FlN2Q4ZjRjMzkzZDlmNmZhNGQ3MQ==
5
7
  SHA512:
6
- metadata.gz: 7e3452318d4a2da31b92b78dad4b69718e9e94a555160eb28fb24c990b3e8f0ab35d28a10d64491ff81311f003702e649cb0911821c807c190f4884380d3e72a
7
- data.tar.gz: cc9b786b7814e45ebbdbe5ff605e700b9f43d8a565e7649fbdc9be2f3c799be1249ddae5380bd1890b45c6a36b49fd155378b95e5cbafdd48b15d735841c5169
8
+ metadata.gz: !binary |-
9
+ OGQ4YmU0NTU0YTIxMWIwYzU2ZTY0MDdlMGM0MjZhODU1N2U3NWU4ZTg5MmNm
10
+ MTA5ZTBhNWE0YzJjNTM3OTU2YmU5Y2Q2NTlhYjYxY2MxNjdlZjcxYmQ0YmZl
11
+ NmU3OGRkYjRlNGQ5MTBlMjgyOTM3NzgxOTVkZTlkNmE3OGIzNzY=
12
+ data.tar.gz: !binary |-
13
+ NGE5ZDlkNjllMDg4MDFiYTc5ZWE2NjVkMWExNDFhMzE0MzgzNWYwYWU3ODFl
14
+ NTljNTU1ZTYwZDA4NDRmYmNjNGM4NDkxNmYyNzdmNDRiY2Q3MDA4MjExOTYz
15
+ MzBiYjFjYjVlMjFiNmE4MzY0OGViYzRiMDk3NzIwMTUzNGJlNjI=
@@ -1,3 +1,3 @@
1
1
  module Watership
2
- VERSION = '0.7.4'
2
+ VERSION = '0.8.0.pre'
3
3
  end
data/lib/watership.rb CHANGED
@@ -10,54 +10,105 @@ module Watership
10
10
  Bunny::TCPConnectionFailed
11
11
  ]
12
12
 
13
- class << self
14
- def environment=(env)
15
- @env = env
16
- end
13
+ def self.register(name, uri, env = 'production')
14
+ registered[name.to_sym] = Client.new(uri, env)
15
+ end
17
16
 
18
- def config=(uri)
19
- @config = uri
20
- end
17
+ def self.registered
18
+ @registered_instances ||= {}
19
+ end
20
+
21
+ def self.logger=(logger)
22
+ @logger = logger
23
+ end
24
+
25
+ def self.logger
26
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
27
+ end
28
+
29
+ def self.enqueue(name, opts)
30
+ registered.fetch(name.to_sym).enqueue(opts)
31
+ end
21
32
 
22
- def enqueue!(options = {})
23
- enqueue(options, true)
33
+ class Client
34
+ def initialize(uri, env)
35
+ @uri = uri
36
+ @env = env
24
37
  end
25
38
 
26
- def enqueue(options = {}, reraise = false)
39
+ def enqueue(options = {})
27
40
  options = options.dup
28
41
  message = options.delete(:message)
29
- name = options.delete(:name)
42
+ name = options.delete(:queue)
30
43
  fallback = options.delete(:fallback)
31
44
 
32
45
  queue = connect_with_queue(name, options)
33
46
  queue.publish(JSON.generate(message))
47
+
48
+ rescue *CONNECTION_EXCEPTIONS => exception
49
+ fallback.call if fallback
50
+ notify(exception)
51
+ clear_channel
52
+ logger.error(exception.class.name)
53
+ raise exception
34
54
  rescue StandardError => exception
35
55
  fallback.call if fallback
36
56
  notify(exception)
37
57
  logger.error(exception.class.name)
38
- raise exception if reraise
58
+ raise exception
39
59
  end
40
60
 
41
61
  def connect_with_queue(name, options = {})
42
- channel.queue(name, { durable: true }.merge(options)) if channel
62
+ with_channel do |channel|
63
+ channel.queue(name, { durable: true }.merge(options)) if channel
64
+ end
65
+ end
66
+
67
+ def thread
68
+ Thread.current[:"watership_#{self.object_id}"] ||= {}
43
69
  end
44
70
 
45
71
  def reconnect
46
- Thread.current[:bunny_channel] = nil
47
- $bunny_connection = nil
48
- channel
72
+ clear_channel
73
+ clear_connection
74
+ create_channel
49
75
  true
50
76
  end
51
77
 
52
- def channel
53
- Thread.current[:bunny_channel] ||= connection.create_channel
54
- rescue *CONNECTION_EXCEPTIONS => exception
55
- notify(exception)
56
- Thread.current[:bunny_channel] = nil
78
+ def connection
79
+ @bunny_connection ||= Bunny.new(@uri).tap { |bunny| bunny.start }
57
80
  end
81
+ alias create_connection connection
58
82
 
59
- def connection
60
- $bunny_connection ||= Bunny.new(@config).tap { |bunny| bunny.start }
83
+ def clear_connection
84
+ if @bunny_connection.respond_to?(:close)
85
+ begin
86
+ @bunny_connection.close
87
+ rescue Bunny::ConnectionError # the connection is already closed
88
+ end
89
+ end
90
+ ensure
91
+ @bunny_connection = nil
92
+ end
93
+
94
+ def with_channel
95
+ channel = create_channel
96
+ yield(channel) if channel
97
+ end
98
+
99
+ def create_channel
100
+ thread[:bunny_channel] ||= connection.create_channel
101
+ end
102
+
103
+ def clear_channel
104
+ if thread[:bunny_channel].respond_to?(:close)
105
+ begin
106
+ thread[:bunny_channel].close
107
+ rescue Bunny::ChannelError # the channel is already closed
108
+ end
109
+ end
110
+ ensure
111
+ thread[:bunny_channel] = nil
61
112
  end
62
113
 
63
114
  def notify(exception)
@@ -65,12 +116,8 @@ module Watership
65
116
  Airbrake.notify_or_ignore(exception) if defined?(Airbrake) && @env == 'production'
66
117
  end
67
118
 
68
- def logger=(logger)
69
- @logger = logger
70
- end
71
-
72
119
  def logger
73
- @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
120
+ Watership.logger
74
121
  end
75
122
  end
76
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watership
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.8.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Scofield
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,42 +28,42 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bunny
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: json
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Wrapper around Bunny to better handle connection issues
@@ -94,17 +94,17 @@ require_paths:
94
94
  - lib
95
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - '>='
97
+ - - ! '>='
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '>='
102
+ - - ! '>'
103
103
  - !ruby/object:Gem::Version
104
- version: '0'
104
+ version: 1.3.1
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.0.14
107
+ rubygems_version: 2.2.2
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Wrapper around Bunny to better handle connection issues