2022-05-16 17:41:12 +00:00
|
|
|
import {DocumentMetadata, HomeDBManager} from 'app/gen-server/lib/HomeDBManager';
|
2022-07-04 14:14:55 +00:00
|
|
|
import log from 'app/server/lib/log';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HostedMetadataManager handles pushing document metadata changes to the Home database when
|
2022-05-16 17:41:12 +00:00
|
|
|
* a doc is updated. Currently updates doc updatedAt time and usage.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
|
|
|
export class HostedMetadataManager {
|
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
// Document metadata mapped by docId.
|
|
|
|
private _metadata: {[docId: string]: DocumentMetadata} = {};
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
// Set if the class holder is closing and no further pushes should be scheduled.
|
|
|
|
private _closing: boolean = false;
|
|
|
|
|
|
|
|
// Last push time in ms since epoch.
|
|
|
|
private _lastPushTime: number = 0.0;
|
|
|
|
|
|
|
|
// Callback for next opportunity to push changes.
|
2022-05-26 06:47:26 +00:00
|
|
|
private _timeout: NodeJS.Timeout|null = null;
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2022-02-19 09:46:49 +00:00
|
|
|
// Maintains the update Promise to wait on it if the class is closing.
|
2022-05-26 06:47:26 +00:00
|
|
|
private _push: Promise<void>|null;
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
// The default delay in milliseconds between metadata pushes to the database.
|
|
|
|
private readonly _minPushDelayMs: number;
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* Create an instance of HostedMetadataManager.
|
2022-05-16 17:41:12 +00:00
|
|
|
* The minPushDelay is the default delay in seconds between metadata pushes to the database.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2022-05-16 17:41:12 +00:00
|
|
|
constructor(private _dbManager: HomeDBManager, minPushDelay: number = 60) {
|
|
|
|
this._minPushDelayMs = minPushDelay * 1000;
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the manager. Send out any pending updates and prevent more from being scheduled.
|
|
|
|
*/
|
|
|
|
public async close(): Promise<void> {
|
2022-05-16 17:41:12 +00:00
|
|
|
// Pushes will no longer be scheduled.
|
|
|
|
this._closing = true;
|
|
|
|
// Wait for outgoing pushes to finish before proceeding.
|
|
|
|
if (this._push) { await this._push; }
|
2020-07-21 13:20:51 +00:00
|
|
|
if (this._timeout) {
|
|
|
|
// Since an update was scheduled, perform one final update now.
|
|
|
|
this._update();
|
2022-05-16 17:41:12 +00:00
|
|
|
if (this._push) { await this._push; }
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-30 16:53:23 +00:00
|
|
|
* Schedule a call to _update some time from now. When the update is made, it will
|
2022-05-16 17:41:12 +00:00
|
|
|
* store the given metadata in the updated_at and usage columns of the docs table for
|
|
|
|
* the specified document.
|
|
|
|
*
|
|
|
|
* If `minimizeDelay` is true, the push will be scheduled with minimum delay (0ms) and
|
|
|
|
* will cancel/overwrite an already scheduled push (if present).
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2022-05-16 17:41:12 +00:00
|
|
|
public scheduleUpdate(docId: string, metadata: DocumentMetadata, minimizeDelay = false): void {
|
|
|
|
if (this._closing) { return; }
|
|
|
|
|
|
|
|
// Update metadata even if an update is already scheduled - if the update has not yet occurred,
|
|
|
|
// the more recent metadata will be used.
|
|
|
|
this._setOrUpdateMetadata(docId, metadata);
|
|
|
|
if (this._timeout && !minimizeDelay) { return; }
|
|
|
|
|
|
|
|
this._schedulePush(minimizeDelay ? 0 : undefined);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
public setDocsMetadata(docUpdateMap: {[docId: string]: DocumentMetadata}): Promise<any> {
|
|
|
|
return this._dbManager.setDocsMetadata(docUpdateMap);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-19 09:46:49 +00:00
|
|
|
* Push all metadata updates to the database.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
|
|
|
private _update(): void {
|
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
|
|
|
}
|
2022-05-16 17:41:12 +00:00
|
|
|
if (this._push) { return; }
|
2020-07-21 13:20:51 +00:00
|
|
|
this._push = this._performUpdate()
|
2022-05-16 17:41:12 +00:00
|
|
|
.catch(err => {
|
|
|
|
log.error("HostedMetadataManager error performing update: ", err);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this._push = null;
|
|
|
|
if (!this._closing && !this._timeout && Object.keys(this._metadata).length !== 0) {
|
|
|
|
// If we have metadata that hasn't been pushed up yet, but no push scheduled,
|
|
|
|
// go ahead and schedule an immediate push. This can happen if `scheduleUpdate`
|
|
|
|
// is called frequently with minimizeDelay set to true, particularly when
|
|
|
|
// _performUpdate is taking a bit longer than normal to complete.
|
|
|
|
this._schedulePush(0);
|
|
|
|
}
|
|
|
|
});
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called by the update function to actually perform the update. This should not
|
|
|
|
* be called unless to force an immediate update.
|
|
|
|
*/
|
|
|
|
private async _performUpdate(): Promise<void> {
|
|
|
|
// Await the database if it is not yet connected.
|
2022-05-16 17:41:12 +00:00
|
|
|
const docUpdates = this._metadata;
|
|
|
|
this._metadata = {};
|
2020-07-21 13:20:51 +00:00
|
|
|
this._lastPushTime = Date.now();
|
2022-05-16 17:41:12 +00:00
|
|
|
await this.setDocsMetadata(docUpdates);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a metadata push.
|
|
|
|
*
|
|
|
|
* If `delayMs` is specified, the push will be scheduled to occur at least that
|
|
|
|
* number of milliseconds in the future. If `delayMs` is unspecified, the push
|
|
|
|
* will be scheduled to occur at least `_minPushDelayMs` after the last push time.
|
|
|
|
*
|
|
|
|
* If called while a push is already scheduled, that push will be cancelled and
|
|
|
|
* replaced with this one.
|
|
|
|
*/
|
|
|
|
private _schedulePush(delayMs?: number): void {
|
|
|
|
if (delayMs === undefined) {
|
|
|
|
delayMs = Math.round(this._minPushDelayMs - (Date.now() - this._lastPushTime));
|
|
|
|
}
|
|
|
|
if (this._timeout) { clearTimeout(this._timeout); }
|
|
|
|
this._timeout = setTimeout(() => this._update(), delayMs < 0 ? 0 : delayMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds `docId` and its `metadata` to the list of queued updates, merging any existing values.
|
|
|
|
*/
|
|
|
|
private _setOrUpdateMetadata(docId: string, metadata: DocumentMetadata): void {
|
|
|
|
if (!this._metadata[docId]) {
|
|
|
|
this._metadata[docId] = metadata;
|
|
|
|
} else {
|
|
|
|
const {updatedAt, usage} = metadata;
|
|
|
|
if (updatedAt) { this._metadata[docId].updatedAt = updatedAt; }
|
|
|
|
if (usage !== undefined) { this._metadata[docId].usage = usage; }
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|