|
|
|
|
@@ -1,61 +1,17 @@
|
|
|
|
|
import { Injectable, Logger, OnModuleInit, Optional } from '@nestjs/common';
|
|
|
|
|
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule';
|
|
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
|
|
|
|
|
import { CallMetric, Config, metrics, OnEvent } from '../../base';
|
|
|
|
|
import { metrics, OnEvent } from '../../base';
|
|
|
|
|
import { PgWorkspaceDocStorageAdapter } from './adapters/workspace';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class DocStorageCronJob implements OnModuleInit {
|
|
|
|
|
private busy = false;
|
|
|
|
|
private readonly logger = new Logger(DocStorageCronJob.name);
|
|
|
|
|
|
|
|
|
|
export class DocStorageCronJob {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly config: Config,
|
|
|
|
|
private readonly db: PrismaClient,
|
|
|
|
|
private readonly workspace: PgWorkspaceDocStorageAdapter,
|
|
|
|
|
@Optional() private readonly registry?: SchedulerRegistry
|
|
|
|
|
private readonly workspace: PgWorkspaceDocStorageAdapter
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
|
|
|
if (this.registry && this.config.doc.manager.enableUpdateAutoMerging) {
|
|
|
|
|
this.registry.addInterval(
|
|
|
|
|
this.autoMergePendingDocUpdates.name,
|
|
|
|
|
// scheduler registry will clean up the interval when the app is stopped
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
if (this.busy) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.busy = true;
|
|
|
|
|
this.autoMergePendingDocUpdates()
|
|
|
|
|
.catch(() => {
|
|
|
|
|
/* never fail */
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
this.busy = false;
|
|
|
|
|
});
|
|
|
|
|
}, this.config.doc.manager.updatePollInterval)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.logger.log('Updates pending queue auto merging cron started');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@CallMetric('doc', 'auto_merge_pending_doc_updates')
|
|
|
|
|
async autoMergePendingDocUpdates() {
|
|
|
|
|
try {
|
|
|
|
|
const randomDoc = await this.workspace.randomDoc();
|
|
|
|
|
if (!randomDoc) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.workspace.getDoc(randomDoc.workspaceId, randomDoc.docId);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
metrics.doc.counter('auto_merge_pending_doc_updates_error').add(1);
|
|
|
|
|
this.logger.error('Failed to auto merge pending doc updates', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)
|
|
|
|
|
async cleanupExpiredHistory() {
|
|
|
|
|
await this.db.snapshotHistory.deleteMany({
|
|
|
|
|
|