Coder Social home page Coder Social logo

sidekiq-bulk's Introduction

sidekiq-bulk

Build Status Code Climate

Give your workers more to do!

Augments Sidekiq job classes with a push_bulk method for easier and faster bulk pushing.

Let's say you want to enqueue a bunch of jobs. You might do this:

big_array.each do |e|
  FooJob.perform_async(e)
end

If big_array has lots of elements, this can be quite slow because of repeated Redis calls per job.

If, instead, you can push all of big_array to Redis in one go, it's more efficient.

Sidekiq comes with Sidekiq::Client.push_bulk which does let you push in bulk. This gem provides a wrapper around that method so that instead of:

Sidekiq::Client.push_bulk("class" => FooJob, "args" => [[1], [2], [3]])

You can write:

FooJob.push_bulk([1, 2, 3])

By default, jobs are sent in batches of 10,000 as a trade-off. Pushing N jobs is not O(1).

Installing

With Bundler:

# in Gemfile
gem "sidekiq-bulk"

Either require "sidekiq-bulk" or require "sidekiq/bulk".

To use

To enqueue a job for each element of an array:

# enqueues 3 jobs for FooJob, each with 1 argument
FooJob.push_bulk([1, 2, 3])

# equivalent to:
Sidekiq::Client.push_bulk("class" => FooJob, "args" => [[1], [2], [3]])

# which is a more efficient version of
[1, 2, 3].each do |i|
  FooJob.perform_async(i)
end

To enqueue jobs with more than one argument:

FooJob.push_bulk(all_users) do |user|
  [user.id, "foobar"]
end

# enqueues one job for each element of `all_users`, where each job
# has two arguments: the user ID and the string "foobar".
#
# equivalent to, but faster than:

all_users.each do |user|
  FooJob.perform_async(user.id, "foobar")
end

Job count splitting

push_bulk will only enqueue at most 10,000 jobs at a time. That is, if items has 20,000 elements, push_bulk(items) will push the first 10,000, then the second 20,000. You can control the threshold with limit:.

# push in groups of 50,000 jobs at a time
FooJob.push_bulk(items, limit: 50_000)

# equivalent to FooJob.push_bulk(items, limit: 10_000)
FooJob.push_bulk(items)

This also works with a block.

# this results in 5 pushes

users.length # => 100_000
FooJob.push_bulk(users, limit: 20_000) do |user|
  [user.id, "some-value"]
end

And to disable push splitting, use push_bulk!.

# one single push of 500,000 jobs, no splitting

users.length # => 500_000
FooJob.push_bulk!(users) do |user|
  [user.id, "some-value"]
end

License

Copyright (c) 2015 Adam Prescott, licensed under the MIT license. See LICENSE.

Development

Issues (bugs, questions, etc.) should be opened with the GitHub project.

To contribute changes:

  1. Visit the GitHub repository for sidekiq-bulk.
  2. Fork the repository.
  3. Make new feature branch: git checkout -b master new-feature (do not add on top of master!)
  4. Implement the feature, along with tests.
  5. Send a pull request.

Make sure to bundle install.

Tests live in spec/. Run them with bundle exec rspec.

To run tests against various Sidekiq versions, use appraisal rspec, after appraisal bundle if necessary. (See the Appraisal project and the Appraisals file for more details.)

sidekiq-bulk's People

Contributors

aprescott avatar ttasanen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.