Skip to content

Commit

Permalink
feat: add retry for articles
Browse files Browse the repository at this point in the history
  • Loading branch information
cooderl committed Mar 26, 2024
1 parent 4dd115a commit 4e866c6
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions apps/server/src/trpc/trpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export class TrpcService {
} else if (errMsg.includes('WeReadError400')) {
this.logger.error(`账号(${id})处理请求参数出错`);
this.logger.error('WeReadError400: ', errMsg);
// 30s 后重试
await new Promise((resolve) => setTimeout(resolve, 30 * 1e3));
// 10s 后重试
await new Promise((resolve) => setTimeout(resolve, 10 * 1e3));
} else {
this.logger.error("Can't handle this error: ", errMsg);
}
Expand Down Expand Up @@ -125,28 +125,38 @@ export class TrpcService {
return account;
}

async getMpArticles(mpId: string) {
async getMpArticles(mpId: string, retryCount = 3) {
const account = await this.getAvailableAccount();

return this.request
.get<
{
id: string;
title: string;
picUrl: string;
publishTime: number;
}[]
>(`/api/v2/platform/mps/${mpId}/articles`, {
headers: {
xid: account.id,
Authorization: `Bearer ${account.token}`,
},
})
.then((res) => res.data)
.then((res) => {
this.logger.log(`getMpArticles(${mpId}): ${res.length} articles`);
return res;
});
try {
const res = await this.request
.get<
{
id: string;
title: string;
picUrl: string;
publishTime: number;
}[]
>(`/api/v2/platform/mps/${mpId}/articles`, {
headers: {
xid: account.id,
Authorization: `Bearer ${account.token}`,
},
})
.then((res) => res.data)
.then((res) => {
this.logger.log(`getMpArticles(${mpId}): ${res.length} articles`);
return res;
});
return res;
} catch (err) {
this.logger.error(`retry(${4 - retryCount}) getMpArticles error: `, err);
if (retryCount > 0) {
return this.getMpArticles(mpId, retryCount - 1);
} else {
throw err;
}
}
}

async refreshMpArticlesAndUpdateFeed(mpId: string) {
Expand Down

0 comments on commit 4e866c6

Please sign in to comment.