Git Product home page Git Product logo

chewy_kiqqer's Introduction

ChewyKiqqer

Build Status Code Climate Test Coverage

This is an alternative udpate/callback mechanism for Chewy. It queues the updates as Sidekiq jobs.

You can pass backrefs like with the standard chewy mechanism, but the job itself will always receive an array of ids.

It is possible to install more multiple update hooks.

Installation

Add this line to your application's Gemfile:

gem 'chewy_kiqqer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chewy_kiqqer

Usage

Just add the module and set it up:

class User < ActiveRecord::Base
  include ChewyKiqqer::Mixin
  
  async_update_index index: 'users#user', queue: :other_than_default, backref: :something
end

You can also include the mixin into ActiveRecord::Base in an initializer if it should be generally available. The queue name is optional. You can also set a default queue name for your application with:

ChewyKiqqer.default_queue = :my_queue

Giving a backref is also optional (also check the chewy docs for the concept). The backref is the element which will be indexed. The default is to use the current record.

# use the current record for the backref
... backref: :self
# call a method on the current record to get the backref
... backref: :some_method
# Pass a proc. It will be called with the current record to get the backref
... backref: -> (rec) { |rec| rec.do_something }

Update handling

The kiqqer does not use Chewy's atomic update, since that functionality is deeply linked with Chewy's syncronous update mechanism.

Instead, ChewyKiqqer will bind itself to the #after_commit callback, which means that it will only trigger a new job after a complete database transaction. This behaviour also ensures that only one job is enqueued per transaction.

However, if you have multiple database transactions, the kiqqer will still queue multiple jobs. The same is true when you enqueue jobs manually.

ChewyKiqqer uses locking via redis to ensure that all updates for one database record are run sequentially. This prevents race conditions which could lead to outdated data being written to the index otherwise.

Logging

Logging is disabled by default, but you can set ChewyKiqqer.logger if you need log output (e.g. ChewyKiqqer.logger = Rails.logger). ChewyKiqqer uses ActiveSupport notifications, which you can also subscribe to. See log_subscriber.rb for more info.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

chewy_kiqqer's People

Contributors

averell23 avatar xijo avatar

Watchers

James Cloos 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.