Redis-based server awareness for distributed applications
Add this line to your application's Gemfile:
gem 'attention'
And then execute:
$ bundle
Or install it yourself as:
$ gem install attention
Activate the instance:
# Autodiscover the ip and exclude the port
Attention.activate
# Or specify them explicitly
Attention.activate ip: '1.2.3.4', port: 9000
The current instance is accessible at:
Attention.instance
Deactivate the instance:
Attention.deactivate
Subscribe to instance availability changes:
Attention.on_change do |change, instances|
# This block is asynchronously called on each change
end
Or get the list of available instances:
Attention.instances
Options can be set on Attention.options
Attention.options = {
namespace: 'attention', # Redis key namespace
ttl: 60, # Instance heartbeat TTL in seconds
redis_url: 'redis://localhost:6379/0', # Redis connection string
pool_size: 5, # Size of the publishing Redis connection pool
timeout: 5 # Redis connection timeout
}
The top-level API provides a simple way to keep track of instance availability. More complex schemes of communication could be implemented by using the Subscriber
and Publisher
classes directly.
Instances attempt to deactivate themselves when the program terminates(at_exit
). If the instance crashes in a dramatic fashion (or a kill -9
), the instance will continue to be listed as available until the TTL (Attention.options[:ttl]
) expires.
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.
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.
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 code of conduct.
The gem is available as open source under the terms of the MIT License.