Bushpig
A simple Redis job queue for Rails apps.
A client puts jobs in a Redis sorted set. A server process takes them out one at a time, oldest first, and runs them.
Requirements
- Ruby 3.2 or later to use
redis6 (development uses Ruby 3.3.11) - Redis server 5.0 or later (Bushpig uses
BZPOPMIN) - The
redisgem 5 or 6, and theconnection_poolgem 2 or 3 - A Rails app with Active Record, for the server
Installation
Bushpig is on the private gem server gems.zero-one.io, not on rubygems.org. The bushpig gem on rubygems.org is an old public release (0.1.15).
Add Bushpig to your Gemfile:
source 'https://gems.zero-one.io/' do
gem 'bushpig', '~> 0.1.23'
endSet the gem server credentials in BUNDLE_GEMS__ZERO___ONE__IO, then run bundle install.
Usage
Define a job
Bushpig::Job.job makes a job class from a handler and a list of fields:
ReportExport = Bushpig::Job.job(->(job) { ReportExporter.new.export(job.report_id) }, :report_id)Bushpig::Job.keyed also takes the fields that identify the job. Jobs with the same values in these fields share one key. If you submit a keyed job while an equal one is still in the queue, the new job replaces the old one:
CompanyUpdate = Bushpig::Job.keyed(
->(job) { CompanyUpdater.new.update(job) },
%i[vat_number],
:vat_number, :name
)Bushpig stores the job as JSON, so use only field values that JSON can hold.
Configure the client and the server
Put the configuration in an initializer. configure_server runs only in the bushpig server process. configure_client runs everywhere else.
# config/initializers/bushpig.rb
redis_url = ENV.fetch('BUSHPIG_REDIS_URL')
Bushpig.configure_server do |config|
pool = Bushpig::RedisPool.new(size: 1, timeout: 5, redis_options: { url: redis_url, timeout: 5 })
config.server = Bushpig::Server.new(pool) { |job| job.handle }
end
Bushpig.configure_client do |config|
pool = Bushpig::RedisPool.new(size: 1, timeout: 5, redis_options: { url: redis_url })
config.client = Bushpig::Client.new(pool)
config.client.default_ttl = 1.day.to_i
end-
Bushpig::Server.new(pool, timeout: 2)sets how many seconds the server waits for a job before it checks for a stop signal again. The default is 2. -
default_ttlsets how many seconds a job payload stays in Redis. If the server does not run a job in that time, the job expires and the server skips it. The default is no expiry. -
Bushpig::Client.new(pool) { |job, new| ... }runs the block after each submit. Use it for logging.
Submit a job
Bushpig.client.submit('company_updates', CompanyUpdate.new('4123456789', 'Acme'))submit also takes score: (default: the current time, so the oldest job runs first) and ttl: (default: default_ttl).
Run the server
Run this command from the root folder of the Rails app:
bundle exec bushpig serve company_updatesThe server loads config/environment.rb and serves 1 queue. To serve more queues, run more processes.
-
Stop: send
INTorTERM. The server finishes the current job and stops within the server timeout. - Errors: the server logs the exception and sends it to Honeybadger, if Honeybadger is loaded. It does not retry the job.
Tests
Run the jobs inline, without Redis:
Bushpig::Testing.inline! do
Bushpig.client.submit('company_updates', CompanyUpdate.new('4123456789', 'Acme'))
endDevelopment
Use Docker:
docker compose build
docker compose run --rm gem bin/setup
docker compose run --rm gem bin/consoleOr install Ruby 3.3.11 from .tool-versions, then run bin/setup and bin/console.
Bushpig has no test suite. Check each change against a real Redis server.
Releasing
Warning
Do not run bundle exec rake release. It pushes the gem to rubygems.org, not to gems.zero-one.io.
- Update the version in
lib/bushpig/version.rb. - Update
Gemfile.lock:docker compose run --rm gem bundle lock - Build the gem:
docker compose run --rm gem bundle exec rake build. This writespkg/bushpig-<version>.gem. - Upload the
.gemfile togems.zero-one.ioby hand. - Tag the release:
git tag v<version>andgit push origin v<version>.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mpowered/bushpig.
License
The gem is available as open source under the terms of the MIT License.