Skip to content

Commit

Permalink
feat: add retry logic to like ranking command(#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Jan 28, 2024
1 parent 19d61d7 commit 30e57d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.DS_Store
dist
*.log
.idea
40 changes: 35 additions & 5 deletions src/command/like-rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const likeRank = createCommand('like-rank')
count: +opts.count,
})
})
const maxRetries = 3; // Set the maximum number of retries

Check warning on line 24 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
const retryDelay = 1000; // Set the delay between retries in milliseconds

Check warning on line 25 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

export const likeRanking = async ({ top, count }: LikeRankOptions) => {
const [user] = filterUsers()
Expand All @@ -32,15 +34,43 @@ export const likeRanking = async ({ top, count }: LikeRankOptions) => {
limit: count > 0 ? limit.limitMaxCount(count) : limit.limitNone(),
})

Check warning on line 35 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`


const userMap: Record<string, { user: Entity.User; count: number }> = {}
const repositoryErrorCount: Record<string, number> = {}; // Track the error count for each repository

Check warning on line 39 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
for (const [i, post] of posts.entries()) {
spinner.update(`Fetching post (${i + 1} / ${posts.length})`)
const repositoryId = post.id;

Check warning on line 42 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

let retryCount = 0;

Check warning on line 44 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
let success = false;

Check warning on line 45 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`

while (!success && retryCount < maxRetries) {
try {
const users = await post.listLikedUsers();

Check warning on line 49 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
for (const user of users) {
const id = user.id;

Check warning on line 51 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
if (!userMap[id]) userMap[id] = { user, count: 1 };

Check warning on line 52 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `;`
else userMap[id].count++;
}

success = true; // Mark success to exit the loop
} catch (error) {

Check failure on line 57 in src/command/like-rank.ts

View workflow job for this annotation

GitHub Actions / lint

Remove unused catch binding `error`
retryCount++;
repositoryErrorCount[repositoryId] = (repositoryErrorCount[repositoryId] || 0) + 1;

if (retryCount < maxRetries) {
await new Promise(resolve => setTimeout(resolve, retryDelay)); // Wait for the specified delay
}
}
}

if (!success) {
console.error(`Failed to fetch liked users for post ${i + 1} after ${maxRetries} attempts.`);

const users = await post.listLikedUsers()
for (const user of users) {
const id = user.id
if (!userMap[id]) userMap[id] = { user, count: 1 }
else userMap[id].count++
if (repositoryErrorCount[repositoryId] >= 3) {
console.error(`Exiting due to repeated failures for repository ${repositoryId}`);
process.exit(1); // Exit the process with an error code
}
}
}

Expand Down

0 comments on commit 30e57d7

Please sign in to comment.