Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.0 KB
Raw Blame
export type FtsSearchReindexCommand =
  'apply' | 'promote' | 'purge' | 'retire' | 'skip-failure' | 'startup' | 'status';

interface RunFtsSearchReindexCommandOptions<T> {
  command: FtsSearchReindexCommand;
  installCaptureInfrastructure: () => Promise<void>;
  run: () => Promise<T>;
  runWithLockRetry: (operation: () => Promise<void>) => Promise<void>;
}

/**
 * Runs one reindex command while keeping capture installation ahead of every apply mutation.
 *
 * Status, failure-skipping, promote, and retire commands intentionally execute without installing
 * database triggers, so PG-only self-hosted instances do not pay the Elasticsearch capture
 * overhead; promote already requires a drained Outbox, which implies capture is installed.
 */
export const runFtsSearchReindexCommand = async <T>({
  command,
  installCaptureInfrastructure,
  run,
  runWithLockRetry,
}: RunFtsSearchReindexCommandOptions<T>): Promise<T> => {
  if (command === 'apply' || command === 'startup') {
    await runWithLockRetry(installCaptureInfrastructure);
  }

  return run();
};