A simple wrapper around Redis streams. Motivation:
Features:
- Publish/Subscribe
- Trim events (only keep last
x
events to save memory) - Consumer Groups
- JSON handling
- Retry on error
Add this line to your application's Gemfile:
gem 'schooling', git: 'git@github.com:saleswhale/schooling_rb.git'
And then execute:
$ bundle
You need to
- Subscribe to the topic in a group as a consumer, with an unique name
- Call
process
with a proc/lambda
require 'schooling/consumer'
require 'schooling/producer'
require 'redis'
processor = proc { |event| rand > 0.5 ? (raise 'I fail') : (sleep rand * 5) }
# or: -> (event) { puts event }
# or: Class.new { def call(event); puts event; end }.new
config1 = { topic: 't', group: 'g', consumer: 'c1' }
c = Schooling::Consumer.new(config1, redis: Redis.new)
t1 = Thread.new { 10.times { c.process(processor) } }
config2 = { topic: 't', group: 'g', consumer: 'c2' }
c2 = Schooling::Consumer.new(config2, redis: Redis.new)
t2 = Thread.new { 10.times { c2.process(processor) } }
p = Schooling::Producer.new(:t)
t3 = Thread.new { 100.times { |i| p.publish(i); sleep rand * 2 } }
[t1, t2, t3].each(&:join)
After checking out the repo, run bin/setup
to install dependencies. 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.