From 218a8766d71ff036376558c384cff95a121e403c Mon Sep 17 00:00:00 2001 From: cooder Date: Sat, 30 Mar 2024 19:03:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E5=85=A8=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/trpc/trpc.router.ts | 6 ++++++ apps/server/src/trpc/trpc.service.ts | 18 ++++++++++++++---- apps/web/src/pages/feeds/index.tsx | 11 +++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/apps/server/src/trpc/trpc.router.ts b/apps/server/src/trpc/trpc.router.ts index 417b5fd..4e12a0f 100644 --- a/apps/server/src/trpc/trpc.router.ts +++ b/apps/server/src/trpc/trpc.router.ts @@ -253,6 +253,12 @@ export class TrpcRouter { await this.trpcService.refreshAllMpArticlesAndUpdateFeed(); } }), + + isRefreshAllMpArticlesRunning: this.trpcService.protectedProcedure.query( + async () => { + return this.trpcService.isRefreshAllMpArticlesRunning; + }, + ), }); articleRouter = this.trpcService.router({ diff --git a/apps/server/src/trpc/trpc.service.ts b/apps/server/src/trpc/trpc.service.ts index e6fbaab..c85e8b6 100644 --- a/apps/server/src/trpc/trpc.service.ts +++ b/apps/server/src/trpc/trpc.service.ts @@ -203,12 +203,22 @@ export class TrpcService { }); } + isRefreshAllMpArticlesRunning = false; + async refreshAllMpArticlesAndUpdateFeed() { + if (this.isRefreshAllMpArticlesRunning) { + this.logger.log('refreshAllMpArticlesAndUpdateFeed is running'); + return; + } const mps = await this.prismaService.feed.findMany(); - - for (const { id } of mps) { - await this.refreshMpArticlesAndUpdateFeed(id); - await new Promise((resolve) => setTimeout(resolve, 10 * 1e3)); + this.isRefreshAllMpArticlesRunning = true; + try { + for (const { id } of mps) { + await this.refreshMpArticlesAndUpdateFeed(id); + await new Promise((resolve) => setTimeout(resolve, 10 * 1e3)); + } + } finally { + this.isRefreshAllMpArticlesRunning = false; } } diff --git a/apps/web/src/pages/feeds/index.tsx b/apps/web/src/pages/feeds/index.tsx index 39f6c35..15f432a 100644 --- a/apps/web/src/pages/feeds/index.tsx +++ b/apps/web/src/pages/feeds/index.tsx @@ -49,6 +49,9 @@ const Feeds = () => { const { mutateAsync: refreshMpArticles, isLoading: isGetArticlesLoading } = trpc.feed.refreshArticles.useMutation(); + const { data: isRefreshAllMpArticlesRunning } = + trpc.feed.isRefreshAllMpArticlesRunning.useQuery(); + const { mutateAsync: deleteFeed, isLoading: isDeleteFeedLoading } = trpc.feed.delete.useMutation({}); @@ -276,7 +279,9 @@ const Feeds = () => { { ev.preventDefault(); ev.stopPropagation(); @@ -285,7 +290,9 @@ const Feeds = () => { await queryUtils.article.list.reset(); }} > - {isGetArticlesLoading ? '更新中...' : '更新全部'} + {isRefreshAllMpArticlesRunning || isGetArticlesLoading + ? '更新中...' + : '更新全部'}