Update to add revision saving policy
This commit is contained in:
parent
f2347782d8
commit
79fd2d1364
3 changed files with 26 additions and 6 deletions
2
app.js
2
app.js
|
@ -526,7 +526,7 @@ models.sequelize.sync().then(function () {
|
||||||
if (realtime.isReady()) {
|
if (realtime.isReady()) {
|
||||||
models.Revision.checkAllNotesRevision(function (err, notes) {
|
models.Revision.checkAllNotesRevision(function (err, notes) {
|
||||||
if (err) return new Error(err);
|
if (err) return new Error(err);
|
||||||
if (notes.length <= 0) return startListen();
|
if (!notes || notes.length <= 0) return startListen();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -167,7 +167,7 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
checkAllNotesRevision: function (callback) {
|
checkAllNotesRevision: function (callback) {
|
||||||
Revision.saveAllNotesRevision(function (err, notes) {
|
Revision.saveAllNotesRevision(function (err, notes) {
|
||||||
if (err) return callback(err, null);
|
if (err) return callback(err, null);
|
||||||
if (notes.length <= 0) {
|
if (!notes || notes.length <= 0) {
|
||||||
return callback(null, notes);
|
return callback(null, notes);
|
||||||
} else {
|
} else {
|
||||||
Revision.checkAllNotesRevision(callback);
|
Revision.checkAllNotesRevision(callback);
|
||||||
|
@ -176,6 +176,7 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
},
|
},
|
||||||
saveAllNotesRevision: function (callback) {
|
saveAllNotesRevision: function (callback) {
|
||||||
sequelize.models.Note.findAll({
|
sequelize.models.Note.findAll({
|
||||||
|
// query all notes that need to save for revision
|
||||||
where: {
|
where: {
|
||||||
$and: [
|
$and: [
|
||||||
{
|
{
|
||||||
|
@ -201,11 +202,30 @@ module.exports = function (sequelize, DataTypes) {
|
||||||
}
|
}
|
||||||
}).then(function (notes) {
|
}).then(function (notes) {
|
||||||
if (notes.length <= 0) return callback(null, notes);
|
if (notes.length <= 0) return callback(null, notes);
|
||||||
|
var savedNotes = [];
|
||||||
async.each(notes, function (note, _callback) {
|
async.each(notes, function (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);
|
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) {
|
}, function (err) {
|
||||||
if (err) return callback(err, null);
|
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) {
|
}).catch(function (err) {
|
||||||
return callback(err, null);
|
return callback(err, null);
|
||||||
|
|
|
@ -187,13 +187,13 @@ var cleaner = setInterval(function () {
|
||||||
if (err) return logger.error('cleaner error', err);
|
if (err) return logger.error('cleaner error', err);
|
||||||
});
|
});
|
||||||
}, 60000);
|
}, 60000);
|
||||||
var saverSleep = true;
|
var saverSleep = false;
|
||||||
// save note revision in interval
|
// save note revision in interval
|
||||||
var saver = setInterval(function () {
|
var saver = setInterval(function () {
|
||||||
if (saverSleep) return;
|
if (saverSleep) return;
|
||||||
models.Revision.saveAllNotesRevision(function (err, notes) {
|
models.Revision.saveAllNotesRevision(function (err, notes) {
|
||||||
if (err) return logger.error('revision saver failed: ' + err);
|
if (err) return logger.error('revision saver failed: ' + err);
|
||||||
if (notes.length <= 0) {
|
if (notes && notes.length <= 0) {
|
||||||
saverSleep = true;
|
saverSleep = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue