mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-02 18:09:58 +08:00
refactor(server): use doc model on doc cron job (#10057)
This commit is contained in:
@@ -1,33 +1,27 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
|
|
||||||
import { metrics, OnEvent } from '../../base';
|
import { metrics, OnEvent } from '../../base';
|
||||||
|
import { Models } from '../../models';
|
||||||
import { PgWorkspaceDocStorageAdapter } from './adapters/workspace';
|
import { PgWorkspaceDocStorageAdapter } from './adapters/workspace';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DocStorageCronJob {
|
export class DocStorageCronJob {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly db: PrismaClient,
|
private readonly models: Models,
|
||||||
private readonly workspace: PgWorkspaceDocStorageAdapter
|
private readonly workspace: PgWorkspaceDocStorageAdapter
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)
|
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)
|
||||||
async cleanupExpiredHistory() {
|
async cleanupExpiredHistory() {
|
||||||
await this.db.snapshotHistory.deleteMany({
|
await this.models.doc.deleteExpiredHistories();
|
||||||
where: {
|
|
||||||
expiredAt: {
|
|
||||||
lte: new Date(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cron(CronExpression.EVERY_MINUTE)
|
@Cron(CronExpression.EVERY_MINUTE)
|
||||||
async reportUpdatesQueueCount() {
|
async reportUpdatesQueueCount() {
|
||||||
metrics.doc
|
metrics.doc
|
||||||
.gauge('updates_queue_count')
|
.gauge('updates_queue_count')
|
||||||
.record(await this.db.update.count());
|
.record(await this.models.doc.getGlobalUpdateCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnEvent('user.deleted')
|
@OnEvent('user.deleted')
|
||||||
|
|||||||
@@ -265,6 +265,21 @@ export class DocModel extends BaseModel {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete expired histories.
|
||||||
|
*/
|
||||||
|
async deleteExpiredHistories() {
|
||||||
|
const { count } = await this.db.snapshotHistory.deleteMany({
|
||||||
|
where: {
|
||||||
|
expiredAt: {
|
||||||
|
lte: new Date(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
this.logger.log(`Deleted ${count} expired histories`);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region Doc
|
// #region Doc
|
||||||
|
|||||||
Reference in New Issue
Block a user