{"_id":"@akeating-redhat/mongodb-lock","_rev":"2-b0aa825e6c0d1eee226a0e50cfaead20","name":"@akeating-redhat/mongodb-lock","description":"Locks which uses MongoDB's atomic operations.","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@akeating-redhat/mongodb-lock","version":"1.0.0","description":"Locks which uses MongoDB's atomic operations.","main":"mongodb-lock.js","scripts":{"test":"set -e; for FILE in test/*.js; do node $FILE; done"},"dependencies":{},"devDependencies":{"mongodb":"^2.1.18","tape":"^3.0.1"},"author":{"name":"Andrew Chilton","email":"andychilton@gmail.com","url":"http://chilts.org/"},"repository":{"type":"git","url":"git://github.com/chilts/mongodb-lock.git"},"bugs":{"url":"http://github.com/chilts/mongodb-lock/issues"},"homepage":"https://github.com/chilts/mongodb-lock","license":"MIT","keywords":["mongodb","lock"],"gitHead":"7cc7a1c0daab4f007351cc8c01b5ce4930f22847","_id":"@akeating-redhat/mongodb-lock@1.0.0","_shasum":"9a0e1a950f1f845710a8de2a3ee36b84fbde3ea8","_from":".","_npmVersion":"2.15.1","_nodeVersion":"4.4.3","_npmUser":{"name":"akeating-redhat","email":"akeating@redhat.com"},"dist":{"shasum":"9a0e1a950f1f845710a8de2a3ee36b84fbde3ea8","tarball":"https://registry.npmjs.org/@akeating-redhat/mongodb-lock/-/mongodb-lock-1.0.0.tgz","integrity":"sha512-DDXEAagpDLQqW1bSS4lR79avMyVXqBMekt0j0DOF7BpHKOhxQnf/X0teqlo4fBzh2grfk+QIqPMgagkLkYUOgw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFbIPCahDbu9sV4w15MfdX1Ts7b3Ps+is80GiW2nyVoIAiEA2rboqFfDePLkpqBnGUCTuNG/nsX8czjg0QuOxvTSVp4="}]},"maintainers":[{"name":"akeating-redhat","email":"akeating@redhat.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/mongodb-lock-1.0.0.tgz_1488191042652_0.5326134019996971"}}},"readme":"# mongodb-lock #\n\n[![Build Status](https://travis-ci.org/chilts/mongodb-lock.png)](https://travis-ci.org/chilts/mongodb-lock)\n[![NPM](https://nodei.co/npm/mongodb-lock.png?mini=true)](https://nodei.co/npm/mongodb-lock/)\n\nA really light-weight way to get distributed locks with a nice API if you're already using MongoDB.\n## Synopsis ##\n\nCreate a connection to your MongoDB database, and use it to create a lock object:\n\n```js\nvar mongodb = require('mongodb')\nvar mongoDbLock = require('mongodb-lock')\n\nvar con = 'mongodb://localhost:27017/test'\n\nmongodb.MongoClient.connect(con, function(err, db) {\n  // supply the database, the collection to use and the lock name\n  var lock = mongoDbLock(db, 'locks', 'database-backup')\n})\n```\n\nNow, acquire the lock:\n\n```js\nlock.acquire(function(err, code) {\n  if (err) {\n    return console.error(code)\n  }\n\n  if ( code ) {\n    // lock was acquired\n    console.log('code=' + code)\n  }\n  else {\n    // lock was not acquired\n  }\n})\n```\n\nOnce you have a lock, you have a 30 second timeout until the lock is released. You can release it earlier by supplying the code:\n\n```js\nlock.release(code, function(err, ok) {\n  if (err) {\n    return console.error(err)\n  }\n\n  if (ok) {\n    console.log('Lock released ok')\n  }\n  else {\n    console.log(\"Lock was not released, perhaps it's already been released or timed out\")\n  }\n})\n```\n\n## MongoDB Indexes ##\n\nYou should make sure any indexes have been added to the collection to make the queries faster:\n\n```js\nlock.ensureIndexes(function(err) {\n  if (err) {\n    return console.error(err)\n  }\n  // all ok\n})\n```\n\n## Multiple Locks ##\n\nMultiple locks can use the same collection and operate quite independently:\n\n```js\nvar dbBackupLock = mongoDbLock(db, 'locks', 'database-backup')\nvar hourlyStats = mongoDbLock(db, 'locks', 'hourly-stats')\nvar sendInvoices = mongoDbLock(db, 'locks', 'send-invoices')\n```\n\n## Options ##\n\nCurrently there is only the option of the timeout. Currently the default is 30 seconds, but you can change it (in milliseconds):\n\n```js\n// lock for 60 seconds\nvar uploadFiles = mongoDbLock(db, 'locks', 'upload-files', { timeout : 60 * 1000})\n\nuploadFiles.lock(function(err, code) {\n  // locked for 60s\n})\n```\n\n### 0.2.0 (2015-04-17) ###\n\n* [FIX] made sure that a 2nd .release() doesn't return ok (ie. it didn't do anything)\n\n### 0.1.0 (2015-04-17) ###\n\n* [NEW] added ability to add indexes to MongoDB\n* [NEW] added lock()\n* [NEW] added release()\n\n## Author ##\n\nWritten by [Andrew Chilton](http://chilts.org/) -\n[Twitter](https://twitter.com/andychilton).\n\n## License ##\n\nMIT - http://chilts.mit-license.org/2014/\n\n(Ends)\n","maintainers":[{"name":"akeating-redhat","email":"akeating@redhat.com"}],"time":{"modified":"2022-06-12T14:26:57.430Z","created":"2017-02-27T10:24:03.205Z","1.0.0":"2017-02-27T10:24:03.205Z"},"homepage":"https://github.com/chilts/mongodb-lock","keywords":["mongodb","lock"],"repository":{"type":"git","url":"git://github.com/chilts/mongodb-lock.git"},"author":{"name":"Andrew Chilton","email":"andychilton@gmail.com","url":"http://chilts.org/"},"bugs":{"url":"http://github.com/chilts/mongodb-lock/issues"},"license":"MIT","readmeFilename":"README.md"}