summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWu Cheng-Han2016-09-18 16:50:20 +0800
committerWu Cheng-Han2016-09-18 16:50:20 +0800
commit79fd2d13649180c7c67a0bf37b90518f9fa5a5a3 (patch)
treef259f1fa300f8c9a678b51784f83d7ad13d0d720
parentf2347782d82e34f8029cade522952598fed554a9 (diff)
Update to add revision saving policy
Diffstat (limited to '')
-rw-r--r--app.js2
-rw-r--r--lib/models/revision.js26
-rw-r--r--lib/realtime.js4
3 files changed, 26 insertions, 6 deletions
diff --git a/app.js b/app.js
index 1d3c7a94..339ff6c2 100644
--- a/app.js
+++ b/app.js
@@ -526,7 +526,7 @@ models.sequelize.sync().then(function () {
if (realtime.isReady()) {
models.Revision.checkAllNotesRevision(function (err, notes) {
if (err) return new Error(err);
- if (notes.length <= 0) return startListen();
+ if (!notes || notes.length <= 0) return startListen();
});
}
});
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;
}