export async function streamRemoveDatapointHandler()

in firestore-semantic-search/functions/src/functions/stream_remove_datapoint.ts [26:68]


export async function streamRemoveDatapointHandler(
  snap: FirebaseFirestore.DocumentSnapshot
) {
  const {status, index} = await checkIndexStatus();
  if (
    (index && status !== IndexStatus.DEPLOYED) ||
    status === IndexStatus.BUILDING
  ) {
    functions.logger.info('Index not deployed yet, skipping...');
    const queue = getFunctions().taskQueue(
      'datapointWriteTask',
      config.instanceId
    );

    // Index isn't ready yet, retry in an hour.
    await queue.enqueue(
      {
        operation: 'remove',
        docId: snap.id,
      },
      {
        scheduleDelaySeconds: 60 * 60,
      }
    );
    return;
  }

  try {
    // Get the index name from the metadata document.
    const metdata = await admin.firestore().doc(config.metadataDoc).get();
    const index = metdata.data()?.index;
    if (!index) {
      functions.logger.error('Index not found');
      return;
    }

    functions.logger.info(`Removing datapoint ${snap.id}`);

    await removeDatapoint(index, [snap.id]);
  } catch (error) {
    functions.logger.error((error as AxiosError).response);
  }
}