Example #1
0
Batch.prototype.lock = function(userId, query, callback) {
  var filter = query instanceof ReportQuery ? query.toMongooseFilter() : {};
  filter = _.extend(filter, {
    checkedOutAt: null,
    checkedOutBy: null,
    read: false
  });

  lock.writeLock(function(release) {
    Report
      .find(filter)
      .sort({ storedAt: -1 })
      .limit(ITEMS_PER_BATCH)
      .exec(function(err, reports) {
        if (err) {
          release();
          return callback(err);
        }
        var ids = _.map(reports, '_id');
        var update = { checkedOutBy: userId, checkedOutAt: new Date() };
        Report.update({ _id: { $in: ids } }, update, { multi: true }, function() {
          release();
          callback();
        });

      });
  });
},
Example #2
0
var updateIndex = exports.updateIndex = function(release, longId, shortId, status, callback) {
  var now = Date.now();
  lock.writeLock(function(unlock) {
    var indexPath = getIndexPath(release);
    fse.readJson(indexPath, function(err, index) {
      if (err) {
        index = newIndex();
      }
      index.ids[longId] = shortId;
      if (!(shortId in index.jobs)) {
        index.jobs[shortId] = {
          id: shortId,
          status: status,
          created: now,
          updated: now
        };
      } else {
        index.jobs[shortId].status = status;
        index.jobs[shortId].updated = now;
      }
      fse.outputJson(indexPath, index, function(err2) {
        unlock();
        if (err2) {
          callback(new Error('Failed to write build index: ' + err2.message));
          return;
        }
        callback(null, index);
      });
    });
  });
};
Example #3
0
client.addListener('message', function (from, to, message) {
    lock.writeLock(function(unlock) {
        // make sure only valid characters exist in the mooseme command
        var mooseMe = message.match(/^\.?moose(?:me)? ([A-z0-9 -_]+)/),
            bots = /^\.bots/.test(message),
            remaining;

        // message not from channel
        if (!/#/.test(to) || !(mooseMe || bots)) {
            unlock();
            return;
        }

        remaining = Math.round((Date.now() - lastMessage) / 1000);

        // moose was called to recently
        if (remaining < 25) {
            client.say(from, 'please wait another ' + (25 - remaining) +
                             ' second' + (remaining < 24 ? 's' : ''));
            unlock();
            return;
        }

        if (bots) {
            client.say(to, 'CaptMoose [NodeJS], create moose pictures at ' + url);
            unlock();
            return;
        }

        mooseMe = mooseMe[1].trim();

        findMoose(mooseMe, function (err, moose) {
            var shrunk;

            if (err) {
                client.say(to, c.bold.red('moose parsing error'));
                unlock();
                return console.error(err.stack);
            }

            if (!moose) {
                unlock();
                return client.say(to, c.bold.red('moose not found.') + ' create ' +
                                      'him at ' + url + '/edit/' +
                                      encodeURIComponent(mooseMe));
            }

            lastMessage = Date.now();
            shrunk = formatMoose(shrinkMoose(moose.moose));
            sayMoose(client.say.bind(client, to), shrunk, function () {
                unlock();
                if (mooseMe === 'random') {
                    client.say(to, 'a lovely ' + moose.name + ' moose');
                }
            });
        });
    });
});
 return new Promise((resolve, reject) => {
     let logger = Logger.instance()
     this.lock.writeLock(release => {
         func().then(obj => {
             release()
             resolve(obj)
         }).catch(err => {
             logger.err(err)
             reject(err)
         })
     })
 })