Active Batch
Active Batch allows the management and execution of batch of Active Jobs. It is useful when an action must be perform when and only when a list of tasks has been completed and you want to execute this list of tasks in parallel. It can also be seen as some kind of map / reduce on top of Active Job.
Installation
Compatible with Rails 4 only (Uses ActiveJob)
-
Add ActiveBatch to your
Gemfile
:gem 'active_batch'
-
And then execute:
$ bundle
-
Generate a migration which will add the required tables to your database.
$ rake active_batch:install:migrations
-
Run the migration.
$ bundle exec rake db:migrate
Usage
Declare a job like so:
class MyBatchJob < ActiveJob::Base
include ActiveBatch::BatchedJob
queue_as :my_queue
# Yields parameters to be used by each task
self.each_work_unit(max)
[0..max].each { |i| yield i }
end
# Actual work perform for each task
def perform(i)
result = expensive_computation(i)
save_result(result) # This saves the result that will be passed to the "reduce" part (self.after_batch)
end
# Executed only once all tasks are complete
def self.after_batch(max, results)
reduce(results)
end
end
Enqueue the whole batch like so:
MyBatchJob.perform_batch(100)
You can mount a dashboard with all the batches and their status in your config/routes.rb
:
mount ActiveBatch::Engine, at: "batches"
Contributing
- Fork it ( https://github.com/idolweb/active_batch/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request