in storage-reverse-image-search/functions/src/functions/stream_remove_datapoint.ts [27:82]
export async function streamRemoveDatapointHandler(
object: functions.storage.ObjectMetadata
) {
const doc = await checkIndexStatus();
const {status, index} = doc || {};
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',
imagePath: object.name,
},
{
scheduleDelaySeconds: 60 * 60,
}
);
return;
}
if (!object.name) return;
if (!utils.isImage(object.name)) {
functions.logger.info(`Skipping ${object.name}, not an image...`);
return;
}
functions.logger.info(`Processing ${object.name}...`);
const imagePath = object.name;
functions.logger.info(`Deleting ${imagePath}`);
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;
}
// Remove a datapoint from the index by its ID.
await removeDatapoint(index, [imagePath]);
} catch (error) {
functions.logger.error((error as AxiosError).response);
}
}