syncrony 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dffadb4614ad54b53c65613f08a76d12614ab5a1
4
+ data.tar.gz: 5c9202982c7068d1c321e438026bd67c0b2eab5e
5
+ SHA512:
6
+ metadata.gz: b01a40144dec8e02860303c7002a84beb3dfc096db36d1106c702d930d1280ddb72862d927b819f17aaf7d65d6e2924d3b5237dd2ee8c733230d8cacf08a14f3
7
+ data.tar.gz: c49c62a2a6c8d28ba9d43cf3b9ac908d678282324a71f2db0312c41b6b8b7c17158bd37d0e756ea48181883f680df1bba13adbc986943b5f092d75a41c9ab397
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ bundle
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'etcd-rb', :git => "git@github.com:josephglanville/etcd-rb.git"
4
+
5
+ # Specify your gem's dependencies in syncrony.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joseph Glanville, Michael Lang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Syncrony
2
+
3
+ Syncrony is a set of distributed systems primitives built with Celluloid and Etcd.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'syncrony'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install syncrony
18
+
19
+ ## Usage
20
+
21
+ ### Observer
22
+ The Observer is a simple primitive that allows you to execute a block every time a key changes in etcd.
23
+ Upon calling Observer.run callback will be executed immediately and every time thereafter they key changes.
24
+
25
+ ```ruby
26
+ require 'syncrony'
27
+ require 'etcd'
28
+ client = Etcd::Client.new
29
+ client.connect
30
+ obs = Syncrony::Observer.new(client, 'test_key')
31
+ obs.run do |value, key, info|
32
+ puts value, key, info
33
+ end
34
+ ```
35
+
36
+ ### Election
37
+ Election is a higher level primitive that implements basic leader election.
38
+ The initial call to Election.run will block until we become the leader, at which point it will then run in the background.
39
+ We can step down or cancel leader election by running Election.cancel.
40
+
41
+ ```ruby
42
+ require 'syncrony'
43
+
44
+ election = Syncrony::Election.new(:path => 'test_key')
45
+ election.run
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/syncrony.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "syncrony/version"
2
+ require 'syncrony/election'
3
+ require 'syncrony/observer'
4
+
5
+ module Syncrony
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,71 @@
1
+ require 'syncrony'
2
+ require 'celluloid'
3
+ require 'etcd'
4
+ require 'securerandom'
5
+
6
+ module Syncrony
7
+ class Election
8
+ include Celluloid
9
+
10
+ attr_accessor :is_leader
11
+
12
+ DEFAULT_OPTS = {
13
+ :servers => ["127.0.0.1:4001"],
14
+ :ttl => 15,
15
+ :interval => 5,
16
+ }
17
+
18
+ def initialize(options={})
19
+ options = DEFAULT_OPTS.merge(options)
20
+ raise if not options[:path]
21
+ @path = options[:path]
22
+ @ttl = options[:ttl]
23
+ @interval = options[:interval]
24
+ @identifier = options[:identifier] || SecureRandom.uuid
25
+ end
26
+
27
+ def run
28
+ @client = Etcd::Client.new(:uris => @servers)
29
+ @client.connect
30
+ @is_leader = false
31
+ request_election
32
+ return
33
+ end
34
+
35
+ def become_leader
36
+ @is_leader = true
37
+ @timer = every(@interval) do
38
+ update
39
+ end
40
+ end
41
+
42
+ # Stop being leader, or stop trying to become leader.
43
+ def cancel
44
+ @observer.cancel if @observer
45
+ # TODO race cdn here? Depends how Celluloid works. What if we're in the moddible of becoming leader?
46
+ if @is_leader
47
+ @timer.cancel
48
+ @is_leader = false
49
+ @client.delete(@path)
50
+ end
51
+ return
52
+ end
53
+
54
+ def request_election
55
+ @observer = Syncrony::Observer.new(@client, @path)
56
+ @observer.run do |value, path, info|
57
+ if value.nil?
58
+ if @client.set(@path, @identifier, :prevValue => nil, :ttl => @ttl)
59
+ @observer.cancel
60
+ become_leader
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def update
67
+ @client.set(@path, @identifier, :prevValue => @identifier, :ttl => @ttl)
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,37 @@
1
+ require 'celluloid'
2
+ require 'etcd'
3
+
4
+ module Syncrony
5
+ class Observer
6
+
7
+ attr_accessor :running
8
+
9
+ def initialize(client, path)
10
+ @client = client
11
+ @path = path
12
+ @running = true
13
+ end
14
+
15
+ def run(&handler)
16
+ info = @client.info(@path)
17
+ value = info ? info[:value] : nil
18
+ index = info ? info[:index] : nil
19
+
20
+ yield value, @path, info
21
+
22
+ while @running
23
+ @client.watch(@path, :index => index ? index + 1 : nil) do |w_value, w_key, w_info|
24
+ if @running
25
+ index = w_info[:index]
26
+ yield w_value, w_key, w_info
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def cancel
33
+ @running = false
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Syncrony
2
+ VERSION = "0.0.1"
3
+ end
data/syncrony.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'syncrony/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "syncrony"
8
+ spec.version = Syncrony::VERSION
9
+ spec.authors = ["Joseph Glanville"]
10
+ spec.email = ["joseph@cloudscaling.com"]
11
+ spec.description = %q{Syncrony is a set of distributed systems primitives built with Celluloid and Etcd.}
12
+ spec.summary = %q{Syncrony distributed systems toolkit}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ # Development dependencies
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "pry"
25
+
26
+ # Runtime dependencies
27
+ spec.add_runtime_dependency "celluloid"
28
+ # spec.add_runtime_dependency "etcd-rb", "~> 1.0.0.pre1"
29
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syncrony
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Glanville
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: celluloid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Syncrony is a set of distributed systems primitives built with Celluloid
70
+ and Etcd.
71
+ email:
72
+ - joseph@cloudscaling.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/syncrony.rb
83
+ - lib/syncrony/election.rb
84
+ - lib/syncrony/observer.rb
85
+ - lib/syncrony/version.rb
86
+ - syncrony.gemspec
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Syncrony distributed systems toolkit
111
+ test_files: []