BackRun
BackRun is a backround job system that is compatible with ActiveJob interface of Rails and allows developers to easily enqueue jobs to a Google Pub Sub Backend. BackRun uses threads to handle many jobs at the same time in the same process.
Features
- Transparent enqueuing with ActiveJob to Google Pub Sub
- Enqueue jobs with delay
- Each worker can be configured with an amount of threads that will be the responsibles for running the enqueued jobs. If there are no available threads the job is enqueued again.
- If a job fails, it is retried at most two times at least five minutes apart.
- Collect metrics using ActiveSupport::Notifications
Installation
Add this line to your application's Gemfile:
gem 'back_run'
And then execute:
$ bundle
Or install it yourself as:
$ gem install back_run
Usage
Configuration
The ActiveJob adapter must be set to :back_run
. This can be done in config/application.rb
.
Add the Google Pub Sub project id and the path to the credentials file.
class Application < Rails::Application
# ...
config.active_job.queue_adapter = :back_run
config.back_run = {
project_id: 'project_id',
credentials_path: 'path/to/credentials.json'
}
end
Job Creation
Create a job to be processed asynchronously:
rails g job Example
This will generate automatically the following file in app/jobs/example_job.rb
class ExampleJob < ApplicationJob
queue_as :default
def perform(*args)
# Do something later
end
end
Jobs can be added to the job queue from anywhere. We can add a job to the queue by:
ExampleJob.perform_later(args)
This arguments are serialized using to_json
method and deserialized with JSON.parse
. So you should use primitive objects as arguments.
Worker configuration
In order to start consuming the Google Pub Sub topics, you should start a worker by running the following command:
bundle exec back_run -q queue-1 queue-2 -c 2
You can specify the queues that the worker should listen to (in this case queue-1 and queue-2) and the number of threads that the worker will use to execute the jobs.
Metrics
The system provides some information about the jobs performed (the success ones). You should subscribe to the back_run.event_executed' event using
ActiveSupport::Notification`
ActiveSupport::Notifications.subscribe 'back_run.event_executed' do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
# Do something with the event
end
This is an example of the event payload:
{
duration_seconds: 10,
job: 'ExampleJob'
}
Development
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/alanhala/back_run. 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.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the BackRun project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.