2018-03-13 01:40:43 +02:00
|
|
|
const ItemChange = require('lib/models/ItemChange');
|
|
|
|
const NoteResource = require('lib/models/NoteResource');
|
|
|
|
const Note = require('lib/models/Note');
|
2018-03-15 20:08:46 +02:00
|
|
|
const Resource = require('lib/models/Resource');
|
2018-03-13 01:40:43 +02:00
|
|
|
const BaseModel = require('lib/BaseModel');
|
2018-03-15 20:08:46 +02:00
|
|
|
const BaseService = require('lib/services/BaseService');
|
2018-03-16 16:32:47 +02:00
|
|
|
const { shim } = require('lib/shim');
|
2018-03-13 01:40:43 +02:00
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
class ResourceService extends BaseService {
|
2018-03-13 01:40:43 +02:00
|
|
|
|
|
|
|
async indexNoteResources() {
|
2018-03-15 20:08:46 +02:00
|
|
|
this.logger().info('ResourceService::indexNoteResources: Start');
|
|
|
|
|
2018-03-13 01:40:43 +02:00
|
|
|
let lastId = 0;
|
2018-03-15 20:08:46 +02:00
|
|
|
|
|
|
|
const processedChangeIds = [];
|
|
|
|
|
|
|
|
await ItemChange.waitForAllSaved();
|
2018-03-13 01:40:43 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const changes = await ItemChange.modelSelectAll(`
|
2018-03-15 20:08:46 +02:00
|
|
|
SELECT id, item_id, type
|
2018-03-13 01:40:43 +02:00
|
|
|
FROM item_changes
|
|
|
|
WHERE item_type = ?
|
|
|
|
AND id > ?
|
2018-03-15 20:08:46 +02:00
|
|
|
ORDER BY id ASC
|
2018-03-16 16:32:47 +02:00
|
|
|
LIMIT 100
|
2018-03-15 20:08:46 +02:00
|
|
|
`, [BaseModel.TYPE_NOTE, lastId]);
|
2018-03-13 01:40:43 +02:00
|
|
|
|
|
|
|
if (!changes.length) break;
|
|
|
|
|
|
|
|
const noteIds = changes.map(a => a.item_id);
|
|
|
|
const notes = await Note.modelSelectAll('SELECT id, title, body FROM notes WHERE id IN ("' + noteIds.join('","') + '")');
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
const noteById = (noteId) => {
|
|
|
|
for (let i = 0; i < notes.length; i++) {
|
|
|
|
if (notes[i].id === noteId) return notes[i];
|
|
|
|
}
|
2018-11-21 21:50:50 +02:00
|
|
|
// The note may have been deleted since the change was recorded. For example in this case:
|
|
|
|
// - Note created (Some Change object is recorded)
|
|
|
|
// - Note is deleted
|
|
|
|
// - ResourceService indexer runs.
|
|
|
|
// In that case, there will be a change for the note, but the note will be gone.
|
|
|
|
return null;
|
2018-03-15 20:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < changes.length; i++) {
|
|
|
|
const change = changes[i];
|
2018-03-13 01:40:43 +02:00
|
|
|
|
|
|
|
if (change.type === ItemChange.TYPE_CREATE || change.type === ItemChange.TYPE_UPDATE) {
|
2018-03-15 20:08:46 +02:00
|
|
|
const note = noteById(change.item_id);
|
2018-11-21 21:50:50 +02:00
|
|
|
if (note) {
|
|
|
|
const resourceIds = await Note.linkedResourceIds(note.body);
|
|
|
|
await NoteResource.setAssociatedResources(note.id, resourceIds);
|
|
|
|
} else {
|
|
|
|
this.logger().warn('ResourceService::indexNoteResources: A change was recorded for a note that has been deleted: ' + change.item_id);
|
|
|
|
}
|
2018-03-13 01:40:43 +02:00
|
|
|
} else if (change.type === ItemChange.TYPE_DELETE) {
|
2018-03-15 20:08:46 +02:00
|
|
|
await NoteResource.remove(change.item_id);
|
2018-03-13 01:40:43 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Invalid change type: ' + change.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastId = change.id;
|
2018-03-15 20:08:46 +02:00
|
|
|
|
|
|
|
processedChangeIds.push(change.id);
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 20:08:46 +02:00
|
|
|
|
|
|
|
if (lastId) {
|
|
|
|
await ItemChange.db().exec('DELETE FROM item_changes WHERE id <= ?', [lastId]);
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:39:44 +02:00
|
|
|
await NoteResource.addOrphanedResources();
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
this.logger().info('ResourceService::indexNoteResources: Completed');
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteOrphanResources(expiryDelay = null) {
|
|
|
|
const resourceIds = await NoteResource.orphanResources(expiryDelay);
|
|
|
|
this.logger().info('ResourceService::deleteOrphanResources:', resourceIds);
|
|
|
|
for (let i = 0; i < resourceIds.length; i++) {
|
|
|
|
await Resource.delete(resourceIds[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async maintenance() {
|
|
|
|
await this.indexNoteResources();
|
|
|
|
await this.deleteOrphanResources();
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
|
2018-03-16 16:32:47 +02:00
|
|
|
static runInBackground() {
|
|
|
|
if (this.isRunningInBackground_) return;
|
|
|
|
|
|
|
|
this.isRunningInBackground_ = true;
|
|
|
|
const service = new ResourceService();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
service.maintenance();
|
|
|
|
}, 1000 * 30);
|
|
|
|
|
|
|
|
shim.setInterval(() => {
|
|
|
|
service.maintenance();
|
|
|
|
}, 1000 * 60 * 60 * 4);
|
|
|
|
}
|
|
|
|
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ResourceService;
|