mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
feat: ✨ Add CLI command to rebuild index
This commit is contained in:
parent
19c15f7e96
commit
93b8609411
3 changed files with 89 additions and 6 deletions
68
cli/commands/index/rebuild.ts
Normal file
68
cli/commands/index/rebuild.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import { Args, Flags } from "@oclif/core";
|
||||
import ora from "ora";
|
||||
import { SonicIndexType, searchManager } from "~/classes/search/search-manager";
|
||||
import { BaseCommand } from "~/cli/base";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export default class IndexRebuild extends BaseCommand<typeof IndexRebuild> {
|
||||
static override args = {
|
||||
type: Args.string({
|
||||
description: "Index category to rebuild",
|
||||
options: ["accounts", "statuses"],
|
||||
required: true,
|
||||
}),
|
||||
};
|
||||
|
||||
static override description = "Rebuild search indexes";
|
||||
|
||||
static override examples = ["<%= config.bin %> <%= command.id %>"];
|
||||
|
||||
static override flags = {
|
||||
"batch-size": Flags.integer({
|
||||
char: "b",
|
||||
description: "Number of items to process in each batch",
|
||||
default: 100,
|
||||
}),
|
||||
};
|
||||
|
||||
public async run(): Promise<void> {
|
||||
const { flags, args } = await this.parse(IndexRebuild);
|
||||
|
||||
if (!config.sonic.enabled) {
|
||||
this.error("Sonic search is disabled");
|
||||
this.exit(1);
|
||||
}
|
||||
|
||||
await searchManager.connect();
|
||||
|
||||
const spinner = ora("Rebuilding search indexes").start();
|
||||
|
||||
switch (args.type) {
|
||||
case "accounts":
|
||||
await searchManager.rebuildSearchIndexes(
|
||||
[SonicIndexType.Accounts],
|
||||
flags["batch-size"],
|
||||
(progress) => {
|
||||
spinner.text = `Rebuilding search indexes (${(progress * 100).toFixed(2)}%)`;
|
||||
},
|
||||
);
|
||||
break;
|
||||
case "statuses":
|
||||
await searchManager.rebuildSearchIndexes(
|
||||
[SonicIndexType.Statuses],
|
||||
flags["batch-size"],
|
||||
(progress) => {
|
||||
spinner.text = `Rebuilding search indexes (${(progress * 100).toFixed(2)}%)`;
|
||||
},
|
||||
);
|
||||
break;
|
||||
default: {
|
||||
this.error("Invalid index type");
|
||||
}
|
||||
}
|
||||
|
||||
spinner.succeed("Search indexes rebuilt");
|
||||
|
||||
this.exit(0);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
import { configureLoggers } from "@/loggers";
|
||||
import { execute } from "@oclif/core";
|
||||
import EmojiAdd from "./commands/emoji/add";
|
||||
import EmojiDelete from "./commands/emoji/delete";
|
||||
import EmojiImport from "./commands/emoji/import";
|
||||
import EmojiList from "./commands/emoji/list";
|
||||
import IndexRebuild from "./commands/index/rebuild";
|
||||
import Start from "./commands/start";
|
||||
import UserCreate from "./commands/user/create";
|
||||
import UserDelete from "./commands/user/delete";
|
||||
|
|
@ -10,6 +12,8 @@ import UserList from "./commands/user/list";
|
|||
import UserRefetch from "./commands/user/refetch";
|
||||
import UserReset from "./commands/user/reset";
|
||||
|
||||
await configureLoggers();
|
||||
|
||||
// Use "explicit" oclif strategy to avoid issues with oclif's module resolver and bundling
|
||||
export const commands = {
|
||||
"user:list": UserList,
|
||||
|
|
@ -21,6 +25,7 @@ export const commands = {
|
|||
"emoji:delete": EmojiDelete,
|
||||
"emoji:list": EmojiList,
|
||||
"emoji:import": EmojiImport,
|
||||
"index:rebuild": IndexRebuild,
|
||||
start: Start,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue