attention 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: b48770e4d6245a630343d966765eac85af863ea0
4
+ data.tar.gz: 2be0077411ce85b73a2cf2f4aac3438dad9b657d
5
+ SHA512:
6
+ metadata.gz: 48bfb49370698125f8cddac048e6fb7416377c146e26ec17ade58dd2ec182ec1758459dd1a94432267c1734ccbe10df74ee000a9ea754645ca54ac3f88947977
7
+ data.tar.gz: 3707fda7d1a9d014db0f4314ff0c8dd02929d4dff7efb1662cc289a371496d4539b6bc0d4d951f643d1d9c782093822de906eb4f8679256e903069ba5fd296fc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.2.3
7
+ - jruby-19mode
8
+ before_install: gem install bundler -v 1.11.2
9
+ services:
10
+ - redis-server
11
+ jruby_before_script:
12
+ - bundle exec jbundle install
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at michael@zooniverse.org. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}){ |m| "spec/#{ m[1] }_spec.rb" }
4
+ watch('spec/spec_helper.rb'){ 'spec' }
5
+ watch(%r{^spec/support/(.+)\.rb$}){ 'spec' }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Parrish
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Attention
2
+
3
+ Redis-based server awareness for distributed applications
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'attention'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install attention
20
+
21
+ ## Usage
22
+
23
+
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/parrish/attention. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new :spec
5
+ task :default => :spec
data/attention.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attention/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'attention'
8
+ spec.version = Attention::VERSION
9
+ spec.authors = ['Michael Parrish']
10
+ spec.email = ['michael@zooniverse.org']
11
+
12
+ spec.summary = 'Redis-based server awareness'
13
+ spec.description = 'Redis-based server awareness for distributed applications'
14
+ spec.homepage = 'https://github.com/parrish/attention'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'redis', '~> 3'
23
+ spec.add_runtime_dependency 'redis-namespace', '~> 1.5'
24
+ spec.add_runtime_dependency 'connection_pool', '~> 2'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.4'
29
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
30
+ spec.add_development_dependency 'pry', '~> 0.10'
31
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
32
+ end
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'attention'
5
+ require 'pry'
6
+
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,14 @@
1
+ require 'redis'
2
+ require 'redis-namespace'
3
+
4
+ module Attention
5
+ module Connection
6
+ def self.new
7
+ connection = Redis.new url: Attention.options[:redis_url],
8
+ connect_timeout: Attention.options[:timeout],
9
+ timeout: Attention.options[:timeout]
10
+
11
+ Redis::Namespace.new Attention.options[:namespace], redis: connection
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,55 @@
1
+ require 'socket'
2
+ require 'attention/publisher'
3
+ require 'attention/timer'
4
+
5
+ module Attention
6
+ class Instance
7
+ attr_reader :id, :publisher
8
+
9
+ def initialize(ip: nil, port: nil)
10
+ @id = Attention.redis.call.incr('instances').to_s
11
+ @ip = ip
12
+ @port = port
13
+ @publisher = Publisher.new 'instance'
14
+ end
15
+
16
+ def publish
17
+ redis = Attention.redis.call
18
+ redis.setex "instance_#{ @id }", Attention.options[:ttl], JSON.dump(info)
19
+ publisher.publish added: info
20
+ heartbeat
21
+ end
22
+
23
+ def unpublish
24
+ return unless @heartbeat
25
+ Attention.redis.call.del "instance_#{ @id }"
26
+ publisher.publish removed: info
27
+ @heartbeat.stop
28
+ @heartbeat = nil
29
+ end
30
+
31
+ def info
32
+ { id: @id, ip: ip }.tap do |h|
33
+ h[:port] = @port if @port
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def heartbeat
40
+ @heartbeat ||= Timer.new(heartbeat_frequency) do
41
+ Attention.redis.call.expire "instance_#{ @id }", Attention.options[:ttl]
42
+ end
43
+ end
44
+
45
+ def heartbeat_frequency
46
+ [1, Attention.options[:ttl] - 5].max
47
+ end
48
+
49
+ def ip
50
+ return @ip if @ip
51
+ address = Socket.ip_address_list.find &:ipv4_private?
52
+ @ip = address && address.ip_address
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ module Attention
2
+ class Publisher
3
+ attr_reader :key
4
+
5
+ def initialize(key)
6
+ @key = key
7
+ end
8
+
9
+ def publish(value)
10
+ redis = Attention.redis.call
11
+ redis.publish key, payload_for(value)
12
+ yield redis if block_given?
13
+ end
14
+
15
+ def payload_for(value)
16
+ case value
17
+ when Array, Hash
18
+ JSON.dump value
19
+ else
20
+ value
21
+ end
22
+ rescue
23
+ value
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ require 'redis'
2
+ require 'redis-namespace'
3
+ require 'connection_pool'
4
+
5
+ module Attention
6
+ class RedisPool
7
+ attr_reader :pool
8
+
9
+ def self.instance
10
+ @instance ||= new
11
+ @pool ||= ->{ @instance.pool.with{ |redis| redis } }
12
+ end
13
+
14
+ private
15
+
16
+ def initialize
17
+ pool_config = {
18
+ size: Attention.options[:pool_size],
19
+ timeout: Attention.options[:timeout]
20
+ }
21
+
22
+ @pool = ConnectionPool.new(pool_config){ Connection.new }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ require 'thread'
2
+ require 'json'
3
+ require 'attention/connection'
4
+ require 'attention/publisher'
5
+
6
+ module Attention
7
+ class Subscriber
8
+ attr_reader :key, :redis
9
+
10
+ class AlreadySubscribedError < StandardError; end
11
+
12
+ def initialize(key, &callback)
13
+ @key = key
14
+ @redis = Connection.new
15
+ subscribe &callback
16
+ end
17
+
18
+ def subscribe(&callback)
19
+ raise AlreadySubscribedError.new if @thread
20
+ @thread = Thread.new do
21
+ redis.subscribe(key) do |on|
22
+ on.message do |channel, payload|
23
+ data = JSON.parse(payload) rescue payload
24
+ if data == 'unsubscribe'
25
+ redis.unsubscribe
26
+ else
27
+ callback.call key, data
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def publisher
35
+ @publisher ||= Publisher.new(key)
36
+ end
37
+
38
+ def unsubscribe
39
+ publisher.publish 'unsubscribe'
40
+ @thread = nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
1
+ require 'thread'
2
+
3
+ module Attention
4
+ class Timer
5
+ attr_reader :frequency, :callback, :lock, :thread
6
+
7
+ def initialize(frequency, &callback)
8
+ @frequency = frequency
9
+ @callback = callback
10
+ @lock = Mutex.new
11
+ start
12
+ end
13
+
14
+ def start
15
+ @thread ||= Thread.new do
16
+ loop do
17
+ sleep @frequency
18
+ lock.synchronize do
19
+ callback.call
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def started?
26
+ !!thread
27
+ end
28
+
29
+ def stop
30
+ return if stopped?
31
+ lock.synchronize do
32
+ thread.kill
33
+ @thread = nil
34
+ end
35
+ end
36
+
37
+ def stopped?
38
+ !started?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Attention
2
+ VERSION = '0.0.1'
3
+ end
data/lib/attention.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'redis'
2
+ require 'attention/version'
3
+ require 'attention/redis_pool'
4
+ require 'attention/subscriber'
5
+ require 'attention/instance'
6
+ require 'attention/timer'
7
+
8
+ module Attention
9
+ class << self
10
+ attr_accessor :options
11
+ attr_reader :instance
12
+ end
13
+
14
+ self.options = {
15
+ namespace: 'attention', # Redis key namespace
16
+ ttl: 60, # Heartbeat TTL in seconds
17
+ redis_url: 'redis://localhost:6379/0', # Redis connection string
18
+ pool_size: 5, # Size of the publishing Redis pool
19
+ timeout: 5 # Redis connection timeout
20
+ }
21
+
22
+ def self.redis
23
+ RedisPool.instance
24
+ end
25
+
26
+ def self.activate(ip: nil, port: nil)
27
+ return if @instance
28
+ @instance = Instance.new ip: ip, port: port
29
+ instance.publish
30
+ end
31
+
32
+ def self.deactivate
33
+ @instance.unpublish if @instance
34
+ end
35
+
36
+ def self.on_change(&callback)
37
+ Subscriber.new('instance') do |channel, change|
38
+ callback.call change, instances
39
+ end
40
+ end
41
+
42
+ def self.instances
43
+ resolve info_for instance_keys
44
+ end
45
+
46
+ def self.instance_keys
47
+ redis.call.keys 'instance_*'
48
+ end
49
+
50
+ def self.info_for(keys)
51
+ [].tap do |list|
52
+ redis.call.multi do |multi|
53
+ keys.each do |key|
54
+ list << multi.get(key)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.resolve(list)
61
+ list.map do |future|
62
+ JSON.parse future.value
63
+ end
64
+ end
65
+
66
+ at_exit{ deactivate }
67
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attention
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Parrish
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis-namespace
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: connection_pool
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-its
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.5'
139
+ description: Redis-based server awareness for distributed applications
140
+ email:
141
+ - michael@zooniverse.org
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - Guardfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - attention.gemspec
156
+ - bin/console
157
+ - bin/setup
158
+ - lib/attention.rb
159
+ - lib/attention/connection.rb
160
+ - lib/attention/instance.rb
161
+ - lib/attention/publisher.rb
162
+ - lib/attention/redis_pool.rb
163
+ - lib/attention/subscriber.rb
164
+ - lib/attention/timer.rb
165
+ - lib/attention/version.rb
166
+ homepage: https://github.com/parrish/attention
167
+ licenses:
168
+ - MIT
169
+ metadata: {}
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubyforge_project:
186
+ rubygems_version: 2.4.8
187
+ signing_key:
188
+ specification_version: 4
189
+ summary: Redis-based server awareness
190
+ test_files: []