0.0
No commit activity in last 3 years
No release in over 3 years
An in-memory queue that takes data and allows you to process it, in batches, on a background thread.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

BatchQueue

BatchQueue is queue that takes jobs and runs them, in aggregate, via a callback on a background thread. You can process a “batch” of N jobs at a time or after T seconds whichever comes sooner.

Example

You want to send metrics to Amazon’s AWS CloudWatch service every 60 seconds or when the batch size reaches 20, whichever comes first. You might write code like this:

# Create the AWS CloudWatch Client
cw_client = Aws::CloudWatch::Client.new(...)

# Set up the BatchQueue
BatchQueue.new(max_batch_size: 20, max_interval_seconds: 60) do |batch_metric_data|
    cw_client.put_metric_data(:metric_data => batch_metric_data)
end

# Add to the BatchQueue
@bq << {
    metric_name: 'Widgets',
    value: 1
}

Installation

Add this line to your application's Gemfile:

gem 'batch_queue'

And then execute:

$ bundle

Or install it yourself as:

$ gem install batch_queue

Usage

1. Set up the BatchQueue

Each BatchQueue gets its own background thread that executes jobs.

bq = BatchQueue.new(max_batch_size: 20, max_interval_seconds: 60) do |batch_metric_data|
    # Put your code that you want to execute here.
end

2. Add a job to the queue

You can add any object to the queue.

bq << {
    # your object here.
}

or

bq << MyJob.new(...)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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/flivni/batch_queue.

License

The gem is available as open source under the terms of the MIT License.