From 21640764f7cf94d73a47b644ef5bd7f3dd03eb08 Mon Sep 17 00:00:00 2001 From: Kurbanov Bulat Date: Sun, 9 Jan 2022 16:52:05 +0300 Subject: [PATCH] Clear cache on fail --- src/bots/factory/bots/approved/index.ts | 41 +++++++++++++------ .../bots/approved/services/book_cache.ts | 16 ++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/bots/factory/bots/approved/index.ts b/src/bots/factory/bots/approved/index.ts index ba7a418..b2ab8be 100644 --- a/src/bots/factory/bots/approved/index.ts +++ b/src/bots/factory/bots/approved/index.ts @@ -11,7 +11,7 @@ import * as Messages from "./messages"; import * as CallbackData from "./callback_data"; import * as BookLibrary from "./services/book_library"; -import { CachedMessage, getBookCache } from './services/book_cache'; +import { CachedMessage, clearBookCache, getBookCache } from './services/book_cache'; import { getBookCacheBuffer } from './services/book_cache_buffer'; import { download } from './services/downloader'; import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings'; @@ -150,14 +150,9 @@ export async function createApprovedBot(token: string, state: BotState): Promise } const [_, format, id] = ctx.message.text.split('_'); - - let cache: CachedMessage; - - if (state.cache === Cache.ORIGINAL) { - cache = await getBookCache(parseInt(id), format); - } else if (state.cache === Cache.BUFFER) { - cache = await getBookCacheBuffer(parseInt(id), format); - } else { + const chatId = ctx.message.chat.id; + + if (state.cache === Cache.NO_CACHE) { const book = await BookLibrary.getBookById(parseInt(id)); const data = await download(book.source.id, book.remote_id, format); ctx.telegram.sendDocument(ctx.message.chat.id, data, { @@ -166,9 +161,30 @@ export async function createApprovedBot(token: string, state: BotState): Promise return; } - ctx.telegram.copyMessage(ctx.message.chat.id, cache.chat_id, cache.message_id, { - allow_sending_without_reply: true, - }) + let cache: CachedMessage | null = null; + + const getCachedMessage = async () => { + if (state.cache === Cache.ORIGINAL) { + return getBookCache(parseInt(id), format); + } + + return getBookCacheBuffer(parseInt(id), format); + }; + + const sendCached = async () => { + cache = await getCachedMessage(); + await ctx.telegram.copyMessage(chatId, cache.chat_id, cache.message_id, { + allow_sending_without_reply: true, + }); + }; + + + try { + await sendCached(); + } catch (e) { + await clearBookCache(parseInt(id), format); + await sendCached(); + } }); bot.hears(/^\/b_info_[\d]+$/gm, async (ctx: Context) => { @@ -279,6 +295,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise }); bot.catch((err) => { + console.log(err); Sentry.captureException(err); }); diff --git a/src/bots/factory/bots/approved/services/book_cache.ts b/src/bots/factory/bots/approved/services/book_cache.ts index 99d6e45..15e3b28 100644 --- a/src/bots/factory/bots/approved/services/book_cache.ts +++ b/src/bots/factory/bots/approved/services/book_cache.ts @@ -31,7 +31,23 @@ async function _makeRequest(url: string, searchParams?: string | Record(url: string, searchParams?: string | Record | URLSearchParams | undefined): Promise { + const response = await got.delete(`${env.CACHE_SERVER_URL}${url}`, { + searchParams, + headers: { + 'Authorization': env.CACHE_SERVER_API_KEY, + }, + responseType: 'json', + }); + + return response.body; +} + export async function getBookCache(bookId: number, fileType: string): Promise { return (await _makeRequest(`/api/v1/${bookId}/${fileType}`)).data; } + +export async function clearBookCache(bookId: number, fileType: string): Promise { + return (await _makeDeleteRequest(`/api/v1/${bookId}/${fileType}`)).data; +}