Skip to content

ResultadosDigitais/upperkut

Repository files navigation

Upperkut

CircleCI Maintainability Test Coverage

[Docs]

Background processing framework for Ruby applications.

Installation

Add this line to your application's Gemfile:

gem 'upperkut'

And then execute:

$ bundle

Or install it yourself as:

$ gem install upperkut

Usage

Example 1 - Buffered Queue:

  1. Create a Worker class and the define how to process the batch;
class MyWorker
  include Upperkut::Worker

  def perform(batch_items)
    heavy_processing(batch_items)
    process_metrics(batch_items)
  end
end
  1. Start pushing items;
MyWorker.push_items(
  [
    {
      'id' => SecureRandom.uuid,
      'name' => 'Robert C Hall',
      'action' => 'EMAIL_OPENNED'
    }
  ]
)
  1. Start Upperkut;
$ bundle exec upperkut --worker MyWorker --concurrency 10

Example 2 - Scheduled Queue:

  1. Create a Worker class and the define how to process the batch;
require 'upperkut/strategies/scheduled_queue'
class MyWorker
  include Upperkut::Worker

  setup_upperkut do |config|
    config.strategy = Upperkut::Strategies::ScheduledQueue.new(self)
  end

  def perform(batch_items)
    heavy_processing(batch_items)
    process_metrics(batch_items)
  end
end
  1. Start pushing items with timestamp parameter;
# timestamp is 'Thu, 10 May 2019 23:43:58 GMT'
MyWorker.push_items(
  [
    {
      'timestamp' => '1557531838',
      'id' => SecureRandom.uuid,
      'name' => 'Robert C Hall',
      'action' => 'SEND_NOTIFICATION'
    }
  ]
)
  1. Start Upperkut;
$ bundle exec upperkut --worker MyWorker --concurrency 10

Example 3 - Priority Queue:

Note: priority queues requires redis 5.0.0+ as it uses ZPOP* commands.

  1. Create a Worker class and the define how to process the batch;
require 'upperkut/strategies/priority_queue'

class MyWorker
  include Upperkut::Worker

  setup_upperkut do |config|
    config.strategy = Upperkut::Strategies::PriorityQueue.new(
      self,
      priority_key: -> { |item| item['tenant_id'] }
    )
  end

  def perform(items)
    items.each do |item|
      puts "event dispatched: #{item.inspect}"
    end
  end
end
  1. So you can enqueue items from different tenants;
MyWorker.push_items(
  [
    { 'tenant_id' => 1, 'id' => 1 },
    { 'tenant_id' => 1, 'id' => 2 },
    { 'tenant_id' => 1, 'id' => 3 },
    { 'tenant_id' => 2, 'id' => 4 },
    { 'tenant_id' => 3, 'id' => 5 },
  ]
)

The code above will enqueue items as follows 1, 4, 5, 2, 3

  1. Start Upperkut;
$ bundle exec upperkut --worker MyWorker --concurrency 10

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rspec to run the tests.

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/ResultadosDigitais/upperkut. 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 Upperkut project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.