summaryrefslogtreecommitdiff
path: root/lib/workers
diff options
context:
space:
mode:
authorWu Cheng-Han2016-11-07 21:30:40 +0800
committerWu Cheng-Han2016-11-07 21:30:40 +0800
commit4ccfdfa538d2fd7e9ee63e937435a7f6fc622c12 (patch)
tree855ecaa3a0312a9f9e2de753160c9f3130a8a2ac /lib/workers
parent793aef0e2e949ca542f7570a82823af20cb34ace (diff)
Add workers for notes to leverage CPU intensive work loading
Diffstat (limited to 'lib/workers')
-rw-r--r--lib/workers/noteRevisionSaver.js19
-rw-r--r--lib/workers/noteUpdater.js101
2 files changed, 120 insertions, 0 deletions
diff --git a/lib/workers/noteRevisionSaver.js b/lib/workers/noteRevisionSaver.js
new file mode 100644
index 00000000..b6b117a3
--- /dev/null
+++ b/lib/workers/noteRevisionSaver.js
@@ -0,0 +1,19 @@
+// core
+var logger = require("../logger.js");
+var models = require("../models");
+
+process.on('message', function (data) {
+ if (!data || !data.msg || data.msg !== 'save note revision') return process.exit();
+ models.Revision.saveAllNotesRevision(function (err, notes) {
+ if (err) {
+ logger.error('note revision saver failed: ' + err);
+ return process.exit();
+ }
+ if (notes && notes.length <= 0) {
+ process.send({
+ msg: 'empty'
+ });
+ }
+ process.exit();
+ });
+}); \ No newline at end of file
diff --git a/lib/workers/noteUpdater.js b/lib/workers/noteUpdater.js
new file mode 100644
index 00000000..3fc4b1eb
--- /dev/null
+++ b/lib/workers/noteUpdater.js
@@ -0,0 +1,101 @@
+// external modules
+var async = require('async');
+var moment = require('moment');
+var LZString = require('lz-string');
+
+// core
+var config = require("../config.js");
+var logger = require("../logger.js");
+var models = require("../models");
+
+process.on('message', function (data) {
+ if (!data || !data.msg || data.msg !== 'update note' || !data.notes) return process.exit();
+ var notes = data.notes;
+ async.each(Object.keys(notes), function (key, callback) {
+ var note = notes[key];
+ if (config.debug) logger.info("note updater found dirty note: " + key);
+ updateNote(note, function(err, _note) {
+ if (!_note) {
+ process.send({
+ msg: 'note not found',
+ note: note
+ });
+ logger.error('note not found: ', note.id);
+ }
+ if (err || !_note) {
+ process.send({
+ msg: 'error',
+ note: note
+ });
+ return callback(err, null);
+ }
+ note.updatetime = moment(_note.lastchangeAt).valueOf();
+ process.send({
+ msg: 'check',
+ note: note
+ });
+ return callback(null, null);
+ });
+ }, function (err) {
+ if (err) logger.error('note updater error', err);
+ process.exit();
+ });
+});
+
+function updateNote(note, callback) {
+ models.Note.findOne({
+ where: {
+ id: note.id
+ }
+ }).then(function (_note) {
+ if (!_note) return callback(null, null);
+ if (note.lastchangeuser) {
+ if (_note.lastchangeuserId != note.lastchangeuser) {
+ models.User.findOne({
+ where: {
+ id: note.lastchangeuser
+ }
+ }).then(function (user) {
+ if (!user) return callback(null, null);
+ note.lastchangeuserprofile = models.User.parseProfile(user.profile);
+ return finishUpdateNote(note, _note, callback);
+ }).catch(function (err) {
+ logger.error(err);
+ return callback(err, null);
+ });
+ } else {
+ return finishUpdateNote(note, _note, callback);
+ }
+ } else {
+ note.lastchangeuserprofile = null;
+ return finishUpdateNote(note, _note, callback);
+ }
+ }).catch(function (err) {
+ logger.error(err);
+ return callback(err, null);
+ });
+}
+
+function finishUpdateNote(note, _note, callback) {
+ var body = note.document;
+ var title = note.title = models.Note.parseNoteTitle(body);
+ title = LZString.compressToBase64(title);
+ body = LZString.compressToBase64(body);
+ var values = {
+ title: title,
+ content: body,
+ authorship: LZString.compressToBase64(JSON.stringify(note.authorship)),
+ lastchangeuserId: note.lastchangeuser,
+ lastchangeAt: Date.now()
+ };
+ _note.update(values).then(function (_note) {
+ return callback(null, _note);
+ }).catch(function (err) {
+ logger.error(err);
+ return callback(err, null);
+ });
+}
+
+module.exports = {
+ updateNote: updateNote
+}; \ No newline at end of file