diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/models/revision.js | 26 | ||||
-rw-r--r-- | lib/realtime.js | 4 |
2 files changed, 25 insertions, 5 deletions
diff --git a/lib/models/revision.js b/lib/models/revision.js index 5d66f9fe..b497f0ed 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -167,7 +167,7 @@ module.exports = function (sequelize, DataTypes) { checkAllNotesRevision: function (callback) { Revision.saveAllNotesRevision(function (err, notes) { if (err) return callback(err, null); - if (notes.length <= 0) { + if (!notes || notes.length <= 0) { return callback(null, notes); } else { Revision.checkAllNotesRevision(callback); @@ -176,6 +176,7 @@ module.exports = function (sequelize, DataTypes) { }, saveAllNotesRevision: function (callback) { sequelize.models.Note.findAll({ + // query all notes that need to save for revision where: { $and: [ { @@ -201,11 +202,30 @@ module.exports = function (sequelize, DataTypes) { } }).then(function (notes) { if (notes.length <= 0) return callback(null, notes); + var savedNotes = []; async.each(notes, function (note, _callback) { - Revision.saveNoteRevision(note, _callback); + // revision saving policy: note not been modified for 5 mins or not save for 10 mins + if (note.lastchangeAt && note.savedAt) { + var lastchangeAt = moment(note.lastchangeAt); + var savedAt = moment(note.savedAt); + if (moment().isAfter(lastchangeAt.add(1, 'minutes'))) { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } else if (lastchangeAt.isAfter(savedAt.add(2, 'minutes'))) { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } else { + return _callback(null, null); + } + } else { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } }, function (err) { if (err) return callback(err, null); - return callback(null, notes); + // return null when no notes need saving at this moment but have delayed tasks to be done + var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes; + return callback(null, result); }); }).catch(function (err) { return callback(err, null); diff --git a/lib/realtime.js b/lib/realtime.js index 2f5bbe34..0c68a256 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -187,13 +187,13 @@ var cleaner = setInterval(function () { if (err) return logger.error('cleaner error', err); }); }, 60000); -var saverSleep = true; +var saverSleep = false; // save note revision in interval var saver = setInterval(function () { if (saverSleep) return; models.Revision.saveAllNotesRevision(function (err, notes) { if (err) return logger.error('revision saver failed: ' + err); - if (notes.length <= 0) { + if (notes && notes.length <= 0) { saverSleep = true; return; } |