refactor exportcontext to taskcontext

This commit is contained in:
zadam 2019-10-18 22:27:38 +02:00
parent 157bd3816d
commit 56e4f4f5ac
9 changed files with 37 additions and 93 deletions

View File

@ -57,7 +57,6 @@
<index id="24" parent="6" name="sqlite_autoindex_api_tokens_1"> <index id="24" parent="6" name="sqlite_autoindex_api_tokens_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>apiTokenId</ColNames> <ColNames>apiTokenId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="25" parent="6"> <key id="25" parent="6">
@ -126,25 +125,20 @@
<index id="37" parent="7" name="sqlite_autoindex_attributes_1"> <index id="37" parent="7" name="sqlite_autoindex_attributes_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>attributeId</ColNames> <ColNames>attributeId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<index id="38" parent="7" name="IDX_attributes_noteId_index"> <index id="38" parent="7" name="IDX_attributes_noteId_index">
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="39" parent="7" name="IDX_attributes_name_value"> <index id="39" parent="7" name="IDX_attributes_name_value">
<ColNames>name <ColNames>name
value</ColNames> value</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="40" parent="7" name="IDX_attributes_name_index"> <index id="40" parent="7" name="IDX_attributes_name_index">
<ColNames>name</ColNames> <ColNames>name</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="41" parent="7" name="IDX_attributes_value_index"> <index id="41" parent="7" name="IDX_attributes_value_index">
<ColNames>value</ColNames> <ColNames>value</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<key id="42" parent="7"> <key id="42" parent="7">
<ColNames>attributeId</ColNames> <ColNames>attributeId</ColNames>
@ -204,21 +198,17 @@ value</ColNames>
<index id="53" parent="8" name="sqlite_autoindex_branches_1"> <index id="53" parent="8" name="sqlite_autoindex_branches_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>branchId</ColNames> <ColNames>branchId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<index id="54" parent="8" name="IDX_branches_noteId_parentNoteId"> <index id="54" parent="8" name="IDX_branches_noteId_parentNoteId">
<ColNames>noteId <ColNames>noteId
parentNoteId</ColNames> parentNoteId</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="55" parent="8" name="IDX_branches_noteId"> <index id="55" parent="8" name="IDX_branches_noteId">
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="56" parent="8" name="IDX_branches_parentNoteId"> <index id="56" parent="8" name="IDX_branches_parentNoteId">
<ColNames>parentNoteId</ColNames> <ColNames>parentNoteId</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<key id="57" parent="8"> <key id="57" parent="8">
<ColNames>branchId</ColNames> <ColNames>branchId</ColNames>
@ -246,7 +236,6 @@ parentNoteId</ColNames>
<index id="62" parent="9" name="sqlite_autoindex_event_log_1"> <index id="62" parent="9" name="sqlite_autoindex_event_log_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>eventId</ColNames> <ColNames>eventId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="63" parent="9"> <key id="63" parent="9">
@ -278,7 +267,6 @@ parentNoteId</ColNames>
<index id="68" parent="10" name="sqlite_autoindex_note_contents_1"> <index id="68" parent="10" name="sqlite_autoindex_note_contents_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="69" parent="10"> <key id="69" parent="10">
@ -351,20 +339,16 @@ parentNoteId</ColNames>
<index id="82" parent="11" name="sqlite_autoindex_note_revisions_1"> <index id="82" parent="11" name="sqlite_autoindex_note_revisions_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>noteRevisionId</ColNames> <ColNames>noteRevisionId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<index id="83" parent="11" name="IDX_note_revisions_noteId"> <index id="83" parent="11" name="IDX_note_revisions_noteId">
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="84" parent="11" name="IDX_note_revisions_dateModifiedFrom"> <index id="84" parent="11" name="IDX_note_revisions_dateModifiedFrom">
<ColNames>utcDateModifiedFrom</ColNames> <ColNames>utcDateModifiedFrom</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<index id="85" parent="11" name="IDX_note_revisions_dateModifiedTo"> <index id="85" parent="11" name="IDX_note_revisions_dateModifiedTo">
<ColNames>utcDateModifiedTo</ColNames> <ColNames>utcDateModifiedTo</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<key id="86" parent="11"> <key id="86" parent="11">
<ColNames>noteRevisionId</ColNames> <ColNames>noteRevisionId</ColNames>
@ -435,7 +419,6 @@ parentNoteId</ColNames>
<index id="98" parent="12" name="sqlite_autoindex_notes_1"> <index id="98" parent="12" name="sqlite_autoindex_notes_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="99" parent="12"> <key id="99" parent="12">
@ -477,7 +460,6 @@ parentNoteId</ColNames>
<index id="106" parent="13" name="sqlite_autoindex_options_1"> <index id="106" parent="13" name="sqlite_autoindex_options_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>name</ColNames> <ColNames>name</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="107" parent="13"> <key id="107" parent="13">
@ -513,7 +495,6 @@ parentNoteId</ColNames>
<index id="113" parent="14" name="sqlite_autoindex_recent_notes_1"> <index id="113" parent="14" name="sqlite_autoindex_recent_notes_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>noteId</ColNames> <ColNames>noteId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="114" parent="14"> <key id="114" parent="14">
@ -534,7 +515,6 @@ parentNoteId</ColNames>
<index id="117" parent="15" name="sqlite_autoindex_source_ids_1"> <index id="117" parent="15" name="sqlite_autoindex_source_ids_1">
<NameSurrogate>1</NameSurrogate> <NameSurrogate>1</NameSurrogate>
<ColNames>sourceId</ColNames> <ColNames>sourceId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<key id="118" parent="15"> <key id="118" parent="15">
@ -597,12 +577,10 @@ parentNoteId</ColNames>
<index id="131" parent="18" name="IDX_sync_entityName_entityId"> <index id="131" parent="18" name="IDX_sync_entityName_entityId">
<ColNames>entityName <ColNames>entityName
entityId</ColNames> entityId</ColNames>
<ColumnCollations></ColumnCollations>
<Unique>1</Unique> <Unique>1</Unique>
</index> </index>
<index id="132" parent="18" name="IDX_sync_utcSyncDate"> <index id="132" parent="18" name="IDX_sync_utcSyncDate">
<ColNames>utcSyncDate</ColNames> <ColNames>utcSyncDate</ColNames>
<ColumnCollations></ColumnCollations>
</index> </index>
<key id="133" parent="18"> <key id="133" parent="18">
<ColNames>id</ColNames> <ColNames>id</ColNames>

View File

@ -15,14 +15,14 @@ const $exportProgressCount = $("#export-progress-count");
const $exportButton = $("#export-button"); const $exportButton = $("#export-button");
const $opmlVersions = $("#opml-versions"); const $opmlVersions = $("#opml-versions");
let exportId = ''; let taskId = '';
let branchId = null; let branchId = null;
export async function showDialog(node, defaultType) { export async function showDialog(node, defaultType) {
utils.closeActiveDialog(); utils.closeActiveDialog();
// each opening of the dialog resets the exportId so we don't associate it with previous exports anymore // each opening of the dialog resets the taskId so we don't associate it with previous exports anymore
exportId = ''; taskId = '';
$exportButton.removeAttr("disabled"); $exportButton.removeAttr("disabled");
$exportProgressWrapper.hide(); $exportProgressWrapper.hide();
$exportProgressCount.text('0'); $exportProgressCount.text('0');
@ -77,9 +77,9 @@ $form.submit(() => {
}); });
function exportBranch(branchId, type, format, version) { function exportBranch(branchId, type, format, version) {
exportId = utils.randomString(10); taskId = utils.randomString(10);
const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${exportId}`; const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${taskId}`;
utils.download(url); utils.download(url);
} }
@ -113,23 +113,23 @@ $('input[name=export-subtree-format]').change(function () {
}); });
ws.subscribeToMessages(async message => { ws.subscribeToMessages(async message => {
if (message.type === 'export-error') { if (message.type === 'task-error' && message.taskType === 'export') {
infoService.showError(message.message); infoService.showError(message.message);
$dialog.modal('hide'); $dialog.modal('hide');
return; return;
} }
if (!message.exportId || message.exportId !== exportId) { if (!message.taskId || message.taskId !== taskId) {
// incoming messages must correspond to this export instance // incoming messages must correspond to this export instance
return; return;
} }
if (message.type === 'export-progress-count') { if (message.type === 'task-progress-count' && message.taskType === 'export') {
$exportProgressWrapper.slideDown(); $exportProgressWrapper.slideDown();
$exportProgressCount.text(message.progressCount); $exportProgressCount.text(message.progressCount);
} }
else if (message.type === 'export-finished') { else if (message.type === 'task-succeeded' && message.taskType === 'export') {
$dialog.modal('hide'); $dialog.modal('hide');
infoService.showMessage("Export finished successfully."); infoService.showMessage("Export finished successfully.");

View File

@ -4,64 +4,24 @@ const tarExportService = require('../../services/export/tar');
const singleExportService = require('../../services/export/single'); const singleExportService = require('../../services/export/single');
const opmlExportService = require('../../services/export/opml'); const opmlExportService = require('../../services/export/opml');
const repository = require("../../services/repository"); const repository = require("../../services/repository");
const ws = require("../../services/ws.js"); const TaskContext = require("../../services/task_context");
const log = require("../../services/log"); const log = require("../../services/log");
class ExportContext {
constructor(exportId) {
// exportId is to distinguish between different export events - it is possible (though not recommended)
// to have multiple exports going at the same time
this.exportId = exportId;
// count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress
this.progressCount = 0;
this.lastSentCountTs = Date.now();
}
async increaseProgressCount() {
this.progressCount++;
if (Date.now() - this.lastSentCountTs >= 500) {
this.lastSentCountTs = Date.now();
await ws.sendMessageToAllClients({
exportId: this.exportId,
type: 'export-progress-count',
progressCount: this.progressCount
});
}
}
async exportFinished() {
await ws.sendMessageToAllClients({
exportId: this.exportId,
type: 'export-finished'
});
}
// must remaing non-static
async reportError(message) {
await ws.sendMessageToAllClients({
type: 'export-error',
message: message
});
}
}
async function exportBranch(req, res) { async function exportBranch(req, res) {
const {branchId, type, format, version, exportId} = req.params; const {branchId, type, format, version, taskId} = req.params;
const branch = await repository.getBranch(branchId); const branch = await repository.getBranch(branchId);
const exportContext = new ExportContext(exportId); const taskContext = new TaskContext(taskId, 'export');
try { try {
if (type === 'subtree' && (format === 'html' || format === 'markdown')) { if (type === 'subtree' && (format === 'html' || format === 'markdown')) {
await tarExportService.exportToTar(exportContext, branch, format, res); await tarExportService.exportToTar(taskContext, branch, format, res);
} }
else if (type === 'single') { else if (type === 'single') {
await singleExportService.exportSingleNote(exportContext, branch, format, res); await singleExportService.exportSingleNote(taskContext, branch, format, res);
} }
else if (format === 'opml') { else if (format === 'opml') {
await opmlExportService.exportToOpml(exportContext, branch, version, res); await opmlExportService.exportToOpml(taskContext, branch, version, res);
} }
else { else {
return [404, "Unrecognized export format " + format]; return [404, "Unrecognized export format " + format];
@ -69,7 +29,7 @@ async function exportBranch(req, res) {
} }
catch (e) { catch (e) {
const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs."; const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs.";
exportContext.reportError(message); taskContext.reportError(message);
log.error(message + e.stack); log.error(message + e.stack);

View File

@ -3,6 +3,8 @@
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const treeService = require('../../services/tree'); const treeService = require('../../services/tree');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
const utils = require('../../services/utils');
const TaskContext = require('../../services/task_context');
async function getNote(req) { async function getNote(req) {
const noteId = req.params.noteId; const noteId = req.params.noteId;
@ -76,8 +78,10 @@ async function deleteNote(req) {
const note = await repository.getNote(noteId); const note = await repository.getNote(noteId);
const taskContext = new TaskContext(utils.randomString(10), 'delete-note');
for (const branch of await note.getBranches()) { for (const branch of await note.getBranches()) {
await noteService.deleteBranch(branch); await noteService.deleteBranch(branch, taskContext);
} }
} }

View File

@ -141,7 +141,7 @@ function register(app) {
apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent); apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent);
apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter); apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);
route(GET, '/api/notes/:branchId/export/:type/:format/:version/:exportId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch); route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch);
route(POST, '/api/notes/:parentNoteId/import', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], importRoute.importToBranch, apiResultHandler); route(POST, '/api/notes/:parentNoteId/import', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], importRoute.importToBranch, apiResultHandler);
route(POST, '/api/notes/:parentNoteId/upload', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], route(POST, '/api/notes/:parentNoteId/upload', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware],

View File

@ -3,7 +3,7 @@
const repository = require("../repository"); const repository = require("../repository");
const utils = require('../utils'); const utils = require('../utils');
async function exportToOpml(exportContext, branch, version, res) { async function exportToOpml(taskContext, branch, version, res) {
if (!['1.0', '2.0'].includes(version)) { if (!['1.0', '2.0'].includes(version)) {
throw new Error("Unrecognized OPML version " + version); throw new Error("Unrecognized OPML version " + version);
} }
@ -38,7 +38,7 @@ async function exportToOpml(exportContext, branch, version, res) {
throw new Error("Unrecognized OPML version " + opmlVersion); throw new Error("Unrecognized OPML version " + opmlVersion);
} }
exportContext.increaseProgressCount(); taskContext.increaseProgressCount();
for (const child of await note.getChildBranches()) { for (const child of await note.getChildBranches()) {
await exportNoteInner(child.branchId); await exportNoteInner(child.branchId);
@ -66,7 +66,7 @@ async function exportToOpml(exportContext, branch, version, res) {
</opml>`); </opml>`);
res.end(); res.end();
exportContext.exportFinished(); taskContext.taskSucceeded();
} }
function prepareText(text) { function prepareText(text) {

View File

@ -5,7 +5,7 @@ const html = require('html');
const utils = require('../utils'); const utils = require('../utils');
const mdService = require('./md'); const mdService = require('./md');
async function exportSingleNote(exportContext, branch, format, res) { async function exportSingleNote(taskContext, branch, format, res) {
const note = await branch.getNote(); const note = await branch.getNote();
if (note.type === 'image' || note.type === 'file') { if (note.type === 'image' || note.type === 'file') {
@ -54,8 +54,8 @@ async function exportSingleNote(exportContext, branch, format, res) {
res.send(payload); res.send(payload);
exportContext.increaseProgressCount(); taskContext.increaseProgressCount();
exportContext.exportFinished(); taskContext.taskSucceeded();
} }
module.exports = { module.exports = {

View File

@ -12,11 +12,11 @@ const protectedSessionService = require('../protected_session');
const sanitize = require("sanitize-filename"); const sanitize = require("sanitize-filename");
/** /**
* @param {ExportContext} exportContext * @param {TaskContext} taskContext
* @param {Branch} branch * @param {Branch} branch
* @param {string} format - 'html' or 'markdown' * @param {string} format - 'html' or 'markdown'
*/ */
async function exportToTar(exportContext, branch, format, res) { async function exportToTar(taskContext, branch, format, res) {
const pack = tar.pack(); const pack = tar.pack();
const noteIdToMeta = {}; const noteIdToMeta = {};
@ -124,7 +124,7 @@ async function exportToTar(exportContext, branch, format, res) {
}) })
}; };
exportContext.increaseProgressCount(); taskContext.increaseProgressCount();
if (note.type === 'text') { if (note.type === 'text') {
meta.format = format; meta.format = format;
@ -268,7 +268,7 @@ ${content}
pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content); pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content);
} }
exportContext.increaseProgressCount(); taskContext.increaseProgressCount();
if (noteMeta.children && noteMeta.children.length > 0) { if (noteMeta.children && noteMeta.children.length > 0) {
const directoryPath = filePathPrefix + noteMeta.dirFileName; const directoryPath = filePathPrefix + noteMeta.dirFileName;
@ -315,7 +315,7 @@ ${content}
pack.pipe(res); pack.pipe(res);
exportContext.exportFinished(); taskContext.taskSucceeded();
} }
module.exports = { module.exports = {

View File

@ -390,7 +390,9 @@ async function updateNote(noteId, noteUpdates) {
} }
/** @return {boolean} - true if note has been deleted, false otherwise */ /** @return {boolean} - true if note has been deleted, false otherwise */
async function deleteBranch(branch) { async function deleteBranch(branch, taskContext) {
taskContext.increaseProgressCount();
if (!branch || branch.isDeleted) { if (!branch || branch.isDeleted) {
return false; return false;
} }
@ -413,7 +415,7 @@ async function deleteBranch(branch) {
await note.save(); await note.save();
for (const childBranch of await note.getChildBranches()) { for (const childBranch of await note.getChildBranches()) {
await deleteBranch(childBranch); await deleteBranch(childBranch, taskContext);
} }
for (const attribute of await note.getOwnedAttributes()) { for (const attribute of await note.getOwnedAttributes()) {