ruby_doozer 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3820705ac3d8550a61d136be5b7134c806dab86e
4
+ data.tar.gz: 35612961ac704ed090141234b65ab42490f1998c
5
+ SHA512:
6
+ metadata.gz: 86f04267b25ba51ef9c7a935b45b41265c8faa124df655d7d10c626e93bc3d462637ab6a6b5f0d7c85678ce935a1e995897379c07ef723f5476daaafbe430ff2
7
+ data.tar.gz: 7890530ad60348d8483ee03208b67a10a89a76e86144618742ab0dccd09399bb23aad84fd2a5ae8357bef7d83bf56a8e32c072ee9ada47a50c8241a475531793
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ group :test do
4
+ gem "shoulda"
5
+ gem "mocha", :require => false
6
+ end
7
+
8
+ gem "rake"
9
+ gem "semantic_logger"
10
+ gem "resilient_socket"
11
+ gem "ruby_protobuf"
@@ -0,0 +1,42 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.13)
5
+ i18n (= 0.6.1)
6
+ multi_json (~> 1.0)
7
+ atomic (1.0.1)
8
+ bourne (1.4.0)
9
+ mocha (~> 0.13.2)
10
+ i18n (0.6.1)
11
+ metaclass (0.0.1)
12
+ mocha (0.13.3)
13
+ metaclass (~> 0.0.1)
14
+ multi_json (1.7.2)
15
+ rake (10.0.4)
16
+ resilient_socket (0.4.0)
17
+ semantic_logger
18
+ ruby_protobuf (0.4.11)
19
+ semantic_logger (2.0.0)
20
+ sync_attr
21
+ thread_safe
22
+ shoulda (3.4.0)
23
+ shoulda-context (~> 1.0, >= 1.0.1)
24
+ shoulda-matchers (~> 1.0, >= 1.4.1)
25
+ shoulda-context (1.0.2)
26
+ shoulda-matchers (1.5.4)
27
+ activesupport (>= 3.0.0)
28
+ bourne (~> 1.3)
29
+ sync_attr (0.1.1)
30
+ thread_safe (0.1.0)
31
+ atomic
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ mocha
38
+ rake
39
+ resilient_socket
40
+ ruby_protobuf
41
+ semantic_logger
42
+ shoulda
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2012 Clarity Services, Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,111 @@
1
+ ruby_doozer
2
+ ===========
3
+
4
+ Ruby Client calling [doozerd](https://github.com/skynetservices/doozerd)
5
+
6
+ * http://github.com/skynetservices/ruby_doozer
7
+
8
+ ### Example
9
+
10
+ ```ruby
11
+ require 'rubygems'
12
+ require 'ruby_doozer'
13
+
14
+ client = RubyDoozer::Client.new(:server => '127.0.0.1:8046')
15
+ client.set('/test/foo', 'value')
16
+ result = client.get('/test/foo')
17
+ client.close
18
+ ```
19
+
20
+ ### Logging
21
+
22
+ Since ruby_skynet uses SemanticLogger, trace level logging of all TCP/IP
23
+ calls can be enabled as follows:
24
+
25
+ ```ruby
26
+ require 'rubygems'
27
+ require 'ruby_skynet'
28
+
29
+ SemanticLogger::Logger.default_level = :trace
30
+ SemanticLogger::Logger.appenders << SemanticLogger::Appender::File.new('skynet.log')
31
+
32
+ class EchoService
33
+ include RubySkynet::Base
34
+ end
35
+
36
+ client = EchoService.new
37
+ p client.echo(:hello => 'world')
38
+ ```
39
+
40
+ ### Notes
41
+
42
+ ruby_doozer has been tested against the doozer fork https://github.com/skynetservices/doozerd
43
+ which was originally forked from: https://github.com/4ad/doozerd.
44
+
45
+ ### Dependencies
46
+
47
+ - Ruby MRI 1.8.7 (or above), Ruby 1.9.3, Or JRuby 1.6.3 (or above)
48
+ - [SemanticLogger](http://github.com/ClarityServices/semantic_logger)
49
+ - [ResilientSocket](https://github.com/ClarityServices/resilient_socket)
50
+ - [ruby_protobuf](https://github.com/macks/ruby-protobuf)
51
+ - [multi_json](https://github.com/intridea/multi_json)
52
+
53
+ ### Install
54
+
55
+ gem install ruby_doozer
56
+
57
+ Development
58
+ -----------
59
+
60
+ Want to contribute to Ruby Doozer?
61
+
62
+ First clone the repo and run the tests:
63
+
64
+ git clone git://github.com/skynetservices/ruby_doozer.git
65
+ cd ruby_doozer
66
+ rake test
67
+
68
+ Feel free to submit an issue and we'll try to resolve it.
69
+
70
+ Contributing
71
+ ------------
72
+
73
+ Once you've made your great commits:
74
+
75
+ 1. [Fork](http://help.github.com/forking/) ruby_doozer
76
+ 2. Create a topic branch - `git checkout -b my_branch`
77
+ 3. Push to your branch - `git push origin my_branch`
78
+ 4. Create an [Issue](http://github.com/skynetservices/ruby_doozer/issues) with a link to your branch
79
+ 5. That's it!
80
+
81
+ Meta
82
+ ----
83
+
84
+ * Code: `git clone git://github.com/skynetservices/ruby_doozer.git`
85
+ * Home: <https://github.com/skynetservices/ruby_doozer>
86
+ * Bugs: <http://github.com/skynetservices/ruby_doozer/issues>
87
+ * Gems: <http://rubygems.org/gems/ruby_doozer>
88
+
89
+ This project uses [Semantic Versioning](http://semver.org/).
90
+
91
+ Authors
92
+ -------
93
+
94
+ Reid Morrison :: reidmo@gmail.com :: @reidmorrison
95
+
96
+ License
97
+ -------
98
+
99
+ Copyright 2013 Clarity Services, Inc.
100
+
101
+ Licensed under the Apache License, Version 2.0 (the "License");
102
+ you may not use this file except in compliance with the License.
103
+ You may obtain a copy of the License at
104
+
105
+ http://www.apache.org/licenses/LICENSE-2.0
106
+
107
+ Unless required by applicable law or agreed to in writing, software
108
+ distributed under the License is distributed on an "AS IS" BASIS,
109
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
110
+ See the License for the specific language governing permissions and
111
+ limitations under the License.
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'rubygems'
5
+ require 'rubygems/package'
6
+ require 'rake/clean'
7
+ require 'rake/testtask'
8
+ require 'date'
9
+ require 'semantic_logger'
10
+ require 'ruby_doozer/version'
11
+
12
+ desc "Build gem"
13
+ task :gem do |t|
14
+ gemspec = Gem::Specification.new do |spec|
15
+ spec.name = 'ruby_doozer'
16
+ spec.version = RubyDoozer::VERSION
17
+ spec.platform = Gem::Platform::RUBY
18
+ spec.authors = ['Reid Morrison']
19
+ spec.email = ['reidmo@gmail.com']
20
+ spec.homepage = 'https://github.com/ClarityServices/ruby_doozer'
21
+ spec.date = Date.today.to_s
22
+ spec.summary = "Doozer Ruby Client"
23
+ spec.description = "Ruby Client for doozer"
24
+ spec.files = FileList["./**/*"].exclude(/\.gem$/, /\.log$/,/nbproject/).map{|f| f.sub(/^\.\//, '')}
25
+ spec.license = "Apache License V2.0"
26
+ spec.has_rdoc = true
27
+ spec.add_dependency 'semantic_logger'
28
+ spec.add_dependency 'resilient_socket'
29
+ spec.add_dependency 'ruby_protobuf'
30
+ end
31
+ Gem::Package.build gemspec
32
+ end
33
+
34
+ desc "Run Test Suite"
35
+ task :test do
36
+ Rake::TestTask.new(:functional) do |t|
37
+ t.test_files = FileList['test/*_test.rb']
38
+ t.verbose = true
39
+ end
40
+
41
+ Rake::Task['functional'].invoke
42
+ end
@@ -0,0 +1,3 @@
1
+ module RubyDoozer
2
+ autoload :Client, 'ruby_doozer/client'
3
+ end
@@ -0,0 +1,206 @@
1
+ require 'semantic_logger'
2
+ require 'resilient_socket'
3
+ require 'ruby_doozer/exceptions'
4
+ require 'ruby_doozer/msg.pb'
5
+
6
+ module RubyDoozer
7
+ class Client
8
+
9
+ include SemanticLogger::Loggable
10
+
11
+ # Create a resilient client connection to a Doozer server
12
+ def initialize(params={})
13
+ # User configurable options
14
+ params[:read_timeout] ||= 5
15
+ params[:connect_timeout] ||= 3
16
+ params[:connect_retry_interval] ||= 0.1
17
+ params[:connect_retry_count] ||= 3
18
+
19
+ # Server name and port where Doozer is running
20
+ # Defaults to 127.0.0.1:8046
21
+ params[:server] ||= '127.0.0.1:8046' unless params[:servers]
22
+
23
+ # Disable buffering the send since it is a RPC call
24
+ params[:buffered] = false
25
+
26
+ logger.trace "Socket Connection parameters", params.dup
27
+
28
+ # For each new connection
29
+ params[:on_connect] = Proc.new do |socket|
30
+ # Reset user_data on each connection
31
+ socket.user_data = 0
32
+ end
33
+
34
+ @socket = ResilientSocket::TCPClient.new(params)
35
+ end
36
+
37
+ # Close this client connection to doozer
38
+ def close
39
+ @socket.close if @socket
40
+ end
41
+
42
+ # Returns the current Doozer revision
43
+ def current_revision
44
+ invoke(Request.new(:verb => Request::Verb::REV)).rev
45
+ end
46
+
47
+ # Set a value in Doozer
48
+ # path: Path to the value to be set
49
+ # value: Value to set
50
+ # rev: Revision at which to set the value
51
+ # If not supplied it will replace the latest version on the server
52
+ #
53
+ # Returns the new revision of the updated value
54
+ #
55
+ # It is recommended to set the revision so that multiple clients do not
56
+ # attempt to update the value at the same time.
57
+ # Setting the revision also allows the call to be retried automatically
58
+ # in the event of a network failure
59
+ def set(path, value, rev=-1)
60
+ invoke(Request.new(:path => path, :value => value, :rev => rev, :verb => Request::Verb::SET), false).rev
61
+ end
62
+
63
+ # Sets the current value at the supplied path
64
+ def []=(path,value)
65
+ set(path, value)
66
+ end
67
+
68
+ # Return the value at the supplied path and revision
69
+ def get(path, rev = nil)
70
+ invoke(Request.new(:path => path, :rev => rev, :verb => Request::Verb::GET))
71
+ end
72
+
73
+ # Returns just the value at the supplied path, not the revision
74
+ def [](path)
75
+ get(path).value
76
+ end
77
+
78
+ # Deletes the file at path if rev is greater than or equal to the file's revision.
79
+ # Returns nil when the file was removed
80
+ # Raises an exception if an attempt to remove the file and its revision
81
+ # is greater than that supplied
82
+ def delete(path, rev=-1)
83
+ invoke(Request.new(:path => path, :rev => rev, :verb => Request::Verb::DEL))
84
+ nil
85
+ end
86
+
87
+ # Returns the directory in the supplied path
88
+ # Use offset to get the next
89
+ # returns nil if no further paths are available
90
+ def directory(path, offset = 0, rev = nil)
91
+ begin
92
+ invoke(Request.new(:path => path, :rev => rev, :offset => offset, :verb => Request::Verb::GETDIR))
93
+ rescue RubyDoozer::ResponseError => exc
94
+ raise exc unless exc.message.include?('RANGE')
95
+ nil
96
+ end
97
+ end
98
+
99
+ def stat(path, rev = nil)
100
+ invoke(Request.new(:path => path, :rev => rev, :verb => Request::Verb::STAT))
101
+ end
102
+
103
+ def access(secret)
104
+ invoke(Request.new(:path => secret, :verb => Request::Verb::ACCESS))
105
+ end
106
+
107
+ # Returns every entry in the supplied path
108
+ # path can also contain wildcard characters such as '*'
109
+ # Example:
110
+ # hosts = []
111
+ # walk('/ctl/node/*/addr', current_revision).each do |node|
112
+ # hosts << node.value unless hosts.include? node.value
113
+ # end
114
+ def walk(path, rev = nil, offset = 0)
115
+ paths = []
116
+ revision = rev || current_revision
117
+ # Resume walk on network connection failure
118
+ @socket.retry_on_connection_failure do
119
+ while true
120
+ send(Request.new(:path => path, :rev => revision , :offset => offset, :verb => Request::Verb::WALK))
121
+ response = read
122
+ if response.err_code
123
+ break if response.err_code == Response::Err::RANGE
124
+ else
125
+ raise ResponseError.new("#{Response::Err.name_by_value(response.err_code)}: #{response.err_detail}") if response.err_code != 0
126
+ end
127
+ paths << response
128
+ offset += 1
129
+ end
130
+ end
131
+ paths
132
+ end
133
+
134
+ # Returns [Array] of hostname [String] with each string
135
+ # representing another Doozer server that can be connected to
136
+ def doozer_hosts
137
+ hosts = []
138
+ walk('/ctl/node/*/addr', current_revision).each do |node|
139
+ hosts << node.value unless hosts.include? node.value
140
+ end
141
+ end
142
+
143
+ # Wait for changes to the supplied path
144
+ # Returns the next change to the supplied path
145
+ def wait(path, rev=current_revision, timeout=-1)
146
+ invoke(Request.new(:path => path, :rev => rev, :verb => Request::Verb::WAIT), true, timeout)
147
+ end
148
+
149
+ # Watch for any changes to the supplied path, calling the supplied block
150
+ # for every change
151
+ # Runs until an exception is thrown
152
+ #
153
+ # If a connection error occurs it will create a new connection to doozer
154
+ # and resubmit the wait. I.e. Will continue from where it left off
155
+ # without any noticeable effect to the supplied block
156
+ def watch(path, rev=current_revision)
157
+ loop do
158
+ result = wait(path, rev, -1)
159
+ yield result
160
+ rev = result.rev + 1
161
+ end
162
+ end
163
+
164
+ #####################
165
+ #protected
166
+
167
+ # Call the Doozer server
168
+ #
169
+ # When readonly ==> true the request is always retried on network failure
170
+ # When readonly ==> false the request is retried on network failure
171
+ # _only_ if a rev has been supplied
172
+ #
173
+ # When modifier is true
174
+ def invoke(request, readonly=true, timeout=nil)
175
+ retry_read = readonly || !request.rev.nil?
176
+ response = nil
177
+ @socket.retry_on_connection_failure do
178
+ send(request)
179
+ response = read(timeout) if retry_read
180
+ end
181
+ # Network error on read must be sent back to caller since we do not
182
+ # know if the modification was made
183
+ response = read(timeout) unless retry_read
184
+ raise ResponseError.new("#{Response::Err.name_by_value(response.err_code)} (#{response.err_code}): #{response.err_detail}") if response.err_code != 0
185
+ response
186
+ end
187
+
188
+ # Send the protobuf Request to Doozer
189
+ def send(request)
190
+ request.tag = 0
191
+ data = request.serialize_to_string
192
+ # An additional header is added to the request indicating the size of the request
193
+ head = [data.length].pack("N")
194
+ @socket.write(head+data)
195
+ end
196
+
197
+ # Read the protobuf Response from Doozer
198
+ def read(timeout=nil)
199
+ # First strip the additional header indicating the size of the subsequent response
200
+ head = @socket.read(4,nil,timeout)
201
+ length = head.unpack("N")[0]
202
+ Response.new.parse_from_string(@socket.read(length))
203
+ end
204
+
205
+ end
206
+ end
@@ -0,0 +1,3 @@
1
+ module RubyDoozer
2
+ class ResponseError < ::RuntimeError; end
3
+ end
@@ -0,0 +1,116 @@
1
+ ### Generated by rprotoc. DO NOT EDIT!
2
+ ### <proto file: doozerd/server/msg.proto>
3
+ # package server;
4
+ #
5
+ # // see doc/proto.md
6
+ # message Request {
7
+ # optional int32 tag = 1;
8
+ #
9
+ # enum Verb {
10
+ # GET = 1;
11
+ # SET = 2;
12
+ # DEL = 3;
13
+ # REV = 5;
14
+ # WAIT = 6;
15
+ # NOP = 7;
16
+ # WALK = 9;
17
+ # GETDIR = 14;
18
+ # STAT = 16;
19
+ # ACCESS = 99;
20
+ # }
21
+ # optional Verb verb = 2;
22
+ #
23
+ # optional string path = 4;
24
+ # optional bytes value = 5;
25
+ # optional int32 other_tag = 6;
26
+ #
27
+ # optional int32 offset = 7;
28
+ #
29
+ # optional int64 rev = 9;
30
+ # }
31
+ #
32
+ # // see doc/proto.md
33
+ # message Response {
34
+ # optional int32 tag = 1;
35
+ # optional int32 flags = 2;
36
+ #
37
+ # optional int64 rev = 3;
38
+ # optional string path = 5;
39
+ # optional bytes value = 6;
40
+ # optional int32 len = 8;
41
+ #
42
+ # enum Err {
43
+ # // don't use value 0
44
+ # OTHER = 127;
45
+ # TAG_IN_USE = 1;
46
+ # UNKNOWN_VERB = 2;
47
+ # READONLY = 3;
48
+ # TOO_LATE = 4;
49
+ # REV_MISMATCH = 5;
50
+ # BAD_PATH = 6;
51
+ # MISSING_ARG = 7;
52
+ # RANGE = 8;
53
+ # NOTDIR = 20;
54
+ # ISDIR = 21;
55
+ # NOENT = 22;
56
+ # }
57
+ # optional Err err_code = 100;
58
+ # optional string err_detail = 101;
59
+ # }
60
+
61
+ require 'protobuf/message/message'
62
+ require 'protobuf/message/enum'
63
+ require 'protobuf/message/service'
64
+ require 'protobuf/message/extend'
65
+
66
+ module RubyDoozer
67
+ class Request < ::Protobuf::Message
68
+ defined_in __FILE__
69
+ optional :int32, :tag, 1
70
+ class Verb < ::Protobuf::Enum
71
+ defined_in __FILE__
72
+ GET = value(:GET, 1)
73
+ SET = value(:SET, 2)
74
+ DEL = value(:DEL, 3)
75
+ REV = value(:REV, 5)
76
+ WAIT = value(:WAIT, 6)
77
+ NOP = value(:NOP, 7)
78
+ WALK = value(:WALK, 9)
79
+ GETDIR = value(:GETDIR, 14)
80
+ STAT = value(:STAT, 16)
81
+ ACCESS = value(:ACCESS, 99)
82
+ end
83
+ optional :Verb, :verb, 2
84
+ optional :string, :path, 4
85
+ optional :bytes, :value, 5
86
+ optional :int32, :other_tag, 6
87
+ optional :int32, :offset, 7
88
+ optional :int64, :rev, 9
89
+ end
90
+ class Response < ::Protobuf::Message
91
+ defined_in __FILE__
92
+ optional :int32, :tag, 1
93
+ optional :int32, :flags, 2
94
+ optional :int64, :rev, 3
95
+ optional :string, :path, 5
96
+ optional :bytes, :value, 6
97
+ optional :int32, :len, 8
98
+ class Err < ::Protobuf::Enum
99
+ defined_in __FILE__
100
+ OTHER = value(:OTHER, 127)
101
+ TAG_IN_USE = value(:TAG_IN_USE, 1)
102
+ UNKNOWN_VERB = value(:UNKNOWN_VERB, 2)
103
+ READONLY = value(:READONLY, 3)
104
+ TOO_LATE = value(:TOO_LATE, 4)
105
+ REV_MISMATCH = value(:REV_MISMATCH, 5)
106
+ BAD_PATH = value(:BAD_PATH, 6)
107
+ MISSING_ARG = value(:MISSING_ARG, 7)
108
+ RANGE = value(:RANGE, 8)
109
+ NOTDIR = value(:NOTDIR, 20)
110
+ ISDIR = value(:ISDIR, 21)
111
+ NOENT = value(:NOENT, 22)
112
+ end
113
+ optional :Err, :err_code, 100
114
+ optional :string, :err_detail, 101
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module RubyDoozer #:nodoc
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,70 @@
1
+ # Allow test to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'ruby_doozer/client'
8
+
9
+ # NOTE:
10
+ # This test assumes that doozerd is running locally on the default port of 8046
11
+
12
+ # Unit Test for RubyDoozer::Client
13
+ class ClientTest < Test::Unit::TestCase
14
+ context RubyDoozer::Client do
15
+
16
+ context "without server" do
17
+ should "raise exception when cannot reach doozer server after 5 retries" do
18
+ exception = assert_raise ResilientSocket::ConnectionFailure do
19
+ RubyDoozer::Client.new(
20
+ # Bad server address to test exception is raised
21
+ :server => 'localhost:9999',
22
+ :connect_retry_interval => 0.1,
23
+ :connect_retry_count => 5)
24
+ end
25
+ assert_match /After 5 connection attempts to host 'localhost:9999': Errno::ECONNREFUSED/, exception.message
26
+ end
27
+
28
+ end
29
+
30
+ context "with client connection" do
31
+ setup do
32
+ @client = RubyDoozer::Client.new(:server => 'localhost:8046')
33
+ end
34
+
35
+ def teardown
36
+ if @client
37
+ @client.close
38
+ @client.delete('/test/foo')
39
+ end
40
+ end
41
+
42
+ should "return current revision" do
43
+ assert @client.current_revision >= 0
44
+ end
45
+
46
+ should "successfully set and get data" do
47
+ new_revision = @client.set('/test/foo', 'value')
48
+ result = @client.get('/test/foo')
49
+ assert_equal 'value', result.value
50
+ assert_equal new_revision, result.rev
51
+ end
52
+
53
+ should "successfully set and get data using array operators" do
54
+ @client['/test/foo'] = 'value2'
55
+ result = @client['/test/foo']
56
+ assert_equal 'value2', result
57
+ end
58
+
59
+ should "fetch directories in a path" do
60
+ @path = '/'
61
+ count = 0
62
+ until @client.directory(@path, count).nil?
63
+ count += 1
64
+ end
65
+ assert count > 0
66
+ end
67
+
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_doozer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Reid Morrison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: semantic_logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: resilient_socket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby_protobuf
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ruby Client for doozer
56
+ email:
57
+ - reidmo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ruby_doozer.rb
68
+ - lib/ruby_doozer/client.rb
69
+ - lib/ruby_doozer/exceptions.rb
70
+ - lib/ruby_doozer/msg.pb.rb
71
+ - lib/ruby_doozer/version.rb
72
+ - test/client_test.rb
73
+ homepage: https://github.com/ClarityServices/ruby_doozer
74
+ licenses:
75
+ - Apache License V2.0
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Doozer Ruby Client
97
+ test_files: []