API Docs for: 0.8.0
Show:

File: include/service/locks/providers/db_lock_provider.js

  1. /*
  2. Copyright (C) 2016 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. 'use strict';
  18.  
  19. //dependencies
  20. var util = require('../../../util.js');
  21.  
  22. module.exports = function(pb) {
  23.  
  24. /**
  25. * A lock provider that leverages the DB to create semaphores
  26. * @class DbLockProvider
  27. * @constructor
  28. */
  29. function DbLockProvider() {}
  30.  
  31. /**
  32. *
  33. * @private
  34. * @static
  35. * @readonly
  36. * @property LOCK_COLLECTION
  37. * @type {String}
  38. */
  39. var LOCK_COLLECTION = 'lock';
  40.  
  41. /**
  42. *
  43. * @private
  44. * @static
  45. * @readonly
  46. * @property EXPECTED_ERROR_CODE
  47. * @type {Integer}
  48. */
  49. var EXPECTED_ERROR_CODE = 11000;
  50.  
  51. /**
  52. * Attempts to acquire the lock with the given name.
  53. * @method acquire
  54. * @param {String} name
  55. * @param {Object} [options={}]
  56. * @param {Object} [options.payload]
  57. * @param {Integer} [options.timeout] Lock timeout in seconds
  58. * @param {Function} cb
  59. */
  60. DbLockProvider.prototype.acquire = function(name, options, cb) {
  61. if (util.isFunction(options)) {
  62. cb = options;
  63. options = {};
  64. }
  65.  
  66. //calculate the lock expiration
  67. var timeout = new Date(Date.now() + (options.timeout * 1000));
  68.  
  69. //try and acquire the lock
  70. var lock = {
  71. object_type: LOCK_COLLECTION,
  72. name: name,
  73. payload: options.payload,
  74. timeout: timeout
  75. };
  76. var dao = new pb.DAO();
  77. dao.save(lock, function(err, result) {
  78. if (util.isError(err)) {
  79. pb.log.silly('DbLockProvider: Failed to insert lock document: CODE=%s\n%s', err.code, err.stack);
  80. //when unique constraint error occurs send no error. It means
  81. //the lock exists
  82. return cb(err.code === EXPECTED_ERROR_CODE ? null : err, false);
  83. }
  84. cb(null, true);
  85. });
  86. };
  87.  
  88. /**
  89. * Retrieves the payload for the lock
  90. * @method get
  91. * @param {String} name
  92. * @param {Function} cb
  93. */
  94. DbLockProvider.prototype.get = function(name, cb) {
  95. var dao = new pb.DAO();
  96. dao.loadByValue('name', name, LOCK_COLLECTION, function(err, result) {
  97. if (util.isObject(result)) {
  98. result = result.payload;
  99. }
  100. cb(err, result);
  101. });
  102. };
  103.  
  104. /**
  105. * Releases the lock
  106. * @method release
  107. * @param {String} name
  108. * @param {Object} [options={}]
  109. * @param {Function} cb
  110. */
  111. DbLockProvider.prototype.release = function(name, options, cb) {
  112. if (util.isFunction(options)) {
  113. cb = options;
  114. options = {};
  115. }
  116.  
  117. var where = {
  118. name: name
  119. };
  120. var dao = new pb.DAO();
  121. dao.delete(where, LOCK_COLLECTION, cb);
  122. };
  123.  
  124. return DbLockProvider;
  125. };
  126.