in lambda/recipes-responder/src/update_processor.ts [64:141]
export async function handleContentUpdate({
content,
staticBucketName,
fastlyApiKey,
contentPrefix,
outgoingEventBus,
}: {
content: Content;
staticBucketName: string;
fastlyApiKey: string;
contentPrefix: string;
outgoingEventBus: string;
}): Promise<number> {
try {
if (content.type != ContentType.ARTICLE) return 0; //no point processing live-blogs etc.
const recipesFound = await extractAllRecipesFromArticle(content);
const allRecipes: RecipeReference[] = recipesFound.map(calculateChecksum);
console.log(`INFO [${content.id}] - has ${allRecipes.length} recipes`);
const entriesToRemove = await recipesToTakeDown(
content.id,
allRecipes.map((recep) => recep.recipeUID),
);
console.log(
`INFO [${content.id}] - ${entriesToRemove.length} recipes have been removed/superceded in the incoming article`,
);
if (allRecipes.length == 0 && entriesToRemove.length == 0) return 0; //no point hanging around and noising up the logs
await Promise.all(
entriesToRemove.map((recep) =>
removeRecipeVersion({
canonicalArticleId: content.id,
recipe: recep,
staticBucketName,
fastlyApiKey,
contentPrefix,
}),
),
);
console.log(
`INFO [${content.id}] - ${entriesToRemove.length} removed/superceded recipes have been removed from the store`,
);
console.log(
`INFO [${content.id}] - publishing ${allRecipes.length} new/updated recipes to the service`,
);
await Promise.all(
allRecipes.map((recipe) =>
publishRecipe({
canonicalArticleId: content.id,
recipe,
staticBucketName,
fastlyApiKey,
contentPrefix,
}),
),
);
console.log(
`INFO [${content.id}] - sending notification of new/updated recipes`,
);
try {
await announceNewRecipe(allRecipes, entriesToRemove, outgoingEventBus);
} catch (e) {
const err = e as Error;
console.error(`Unable to announce updates: ${err.toString()}`);
}
console.log(`INFO [${content.id}] - Done`);
return allRecipes.length + entriesToRemove.length;
} catch (err) {
//log out what actually caused the breakage
console.error('Failed article was: ', JSON.stringify(content));
console.error('------------');
console.error(err);
throw err;
}
}