GithubHelp home page GithubHelp logo

kinpro / mongoid-locker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mongoid/mongoid-locker

0.0 2.0 0.0 441 KB

Document-level locking for MongoDB via Mongoid

License: MIT License

Ruby 98.96% CSS 1.04%

mongoid-locker's Introduction

mongoid-locker Build Status Code Climate

Document-level locking for MongoDB via Mongoid. The need arose at Jux from multiple processes on multiple servers trying to act upon the same document and stepping on each other's toes. Mongoid-Locker is an easy way to ensure only one process can perform a certain operation on a document at a time.

Tested against MRI 1.9.3, 2.0.0 and 2.1.2, Rubinius 2.x, and JRuby 1.9 with Mongoid 2, 3 and 4 (where supported).

Usage

Add to your Gemfile:

gem 'mongoid-locker', '~> 0.2'

and run bundle install. In the model you wish to lock, include Mongoid::Locker after Mongoid::Document. For example:

class QueueItem
  include Mongoid::Document
  include Mongoid::Locker

  field :completed_at, :type => Time
end

Then, execute any code you like in a block like so:

queue_item.with_lock do

  # do stuff

  queue_item.completed_at = Time.now
  queue_item.save!
end

#with_lock takes an optional handful of options around retrying, so make sure to take a look.

The default timeout can also be set on a per-class basis:

class QueueItem
  # ...
  timeout_lock_after 10
end

Note that these locks are only enforced when using #with_lock, not at the database level. It is useful for transactional operations, where you can make atomic modification of the document with checks. For exmple, you could deduct a purchase from a user's balance... unless they are broke.

More in-depth method documentation can be found at rdoc.info. Enjoy!

mongoid-locker's People

Contributors

afeld avatar dblock avatar

Watchers

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