mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-12 15:46:29 +08:00
86 lines
1.8 KiB
TypeScript
86 lines
1.8 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
|
|
|
import { Config, JobQueue, OnEvent } from '../../base';
|
|
|
|
@Injectable()
|
|
export class IndexerEvent {
|
|
constructor(
|
|
private readonly queue: JobQueue,
|
|
private readonly config: Config
|
|
) {}
|
|
|
|
@OnEvent('doc.updated')
|
|
async indexDoc({ workspaceId, docId }: Events['doc.updated']) {
|
|
if (!this.config.indexer.enabled) {
|
|
return;
|
|
}
|
|
|
|
await this.queue.add(
|
|
'indexer.indexDoc',
|
|
{
|
|
workspaceId,
|
|
docId,
|
|
},
|
|
{
|
|
jobId: `indexDoc/${workspaceId}/${docId}`,
|
|
priority: 100,
|
|
}
|
|
);
|
|
}
|
|
|
|
@OnEvent('doc.snapshot.updated')
|
|
async indexWorkspace({ workspaceId, docId }: Events['doc.snapshot.updated']) {
|
|
if (!this.config.indexer.enabled) {
|
|
return;
|
|
}
|
|
|
|
if (workspaceId !== docId) {
|
|
return;
|
|
}
|
|
|
|
await this.queue.add(
|
|
'indexer.indexWorkspace',
|
|
{ workspaceId },
|
|
{ jobId: `indexWorkspace/${workspaceId}`, priority: 100 }
|
|
);
|
|
}
|
|
|
|
@OnEvent('user.deleted')
|
|
async deleteUserWorkspaces(payload: Events['user.deleted']) {
|
|
if (!this.config.indexer.enabled) {
|
|
return;
|
|
}
|
|
|
|
for (const workspace of payload.ownedWorkspaces) {
|
|
await this.queue.add(
|
|
'indexer.deleteWorkspace',
|
|
{
|
|
workspaceId: workspace,
|
|
},
|
|
{
|
|
jobId: `deleteWorkspace/${workspace}`,
|
|
priority: 0,
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
@Cron(CronExpression.EVERY_30_SECONDS)
|
|
async autoIndexWorkspaces() {
|
|
if (!this.config.indexer.enabled) {
|
|
return;
|
|
}
|
|
|
|
await this.queue.add(
|
|
'indexer.autoIndexWorkspaces',
|
|
{},
|
|
{
|
|
// make sure only one job is running at a time
|
|
delay: 30 * 1000,
|
|
jobId: 'autoIndexWorkspaces',
|
|
}
|
|
);
|
|
}
|
|
}
|