Skip to content

Commit

Permalink
refactor: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 28, 2024
1 parent 30e57d7 commit b523ec7
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/command/like-rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const likeRank = createCommand('like-rank')
count: +opts.count,
})
})
const maxRetries = 3; // Set the maximum number of retries
const retryDelay = 1000; // Set the delay between retries in milliseconds
const maxRetries = 3 // Set the maximum number of retries
const retryDelay = 1000 // Set the delay between retries in milliseconds

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


const userMap: Record<string, { user: Entity.User; count: number }> = {}
const repositoryErrorCount: Record<string, number> = {}; // Track the error count for each repository
const repositoryErrorCount: Record<string, number> = {} // Track the error count for each repository
for (const [i, post] of posts.entries()) {
spinner.update(`Fetching post (${i + 1} / ${posts.length})`)
const repositoryId = post.id;
const repositoryId = post.id

let retryCount = 0;
let success = false;
let retryCount = 0
let success = false

while (!success && retryCount < maxRetries) {
try {
const users = await post.listLikedUsers();
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++;
const id = user.id
if (!userMap[id]) userMap[id] = { user, count: 1 }
else userMap[id].count++
}

success = true; // Mark success to exit the loop
} catch (error) {
retryCount++;
repositoryErrorCount[repositoryId] = (repositoryErrorCount[repositoryId] || 0) + 1;
success = true // Mark success to exit the loop
} catch {
retryCount++
repositoryErrorCount[repositoryId] =
(repositoryErrorCount[repositoryId] || 0) + 1

if (retryCount < maxRetries) {
await new Promise(resolve => setTimeout(resolve, retryDelay)); // Wait for the specified delay
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.`);
console.error(
`Failed to fetch liked users for post ${
i + 1
} after ${maxRetries} attempts.`,
)

if (repositoryErrorCount[repositoryId] >= 3) {
console.error(`Exiting due to repeated failures for repository ${repositoryId}`);
process.exit(1); // Exit the process with an error code
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 b523ec7

Please sign in to comment.