Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9df5a82

Browse filesBrowse files
committed
Deduplicate post checks
1 parent d079a46 commit 9df5a82
Copy full SHA for 9df5a82

File tree

2 files changed

+11
-0
lines changed
Filter options

2 files changed

+11
-0
lines changed

‎lib/cron.ts

Copy file name to clipboardExpand all lines: lib/cron.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getLatestPost, getPost } from "./hn";
22
import {
33
getLastCheckedId,
44
setLastCheckedId,
5+
checkIfPostWasChecked,
56
getTeamsAndKeywords,
67
} from "./upstash";
78
import { equalsIgnoreOrder, postScanner } from "./helpers";
@@ -28,6 +29,8 @@ export async function cron() {
2829
let errors: any[] = [];
2930

3031
for (let i = lastCheckedId + 1; i <= latestPostId; i++) {
32+
if (await checkIfPostWasChecked(i)) continue; // avoid double checking posts
33+
3134
const post = await getPost(i); // get post from hacker news
3235
if (!post) {
3336
console.log(`Hacker News post not found. Post number: ${i}`); // by the off chance that the post fails to fetch/doesn't exist, log it

‎lib/upstash.ts

Copy file name to clipboardExpand all lines: lib/upstash.ts
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ export async function setLastCheckedId(id: number) {
112112
return await redis.set("lastCheckedId", id);
113113
}
114114

115+
export async function checkIfPostWasChecked(id: number) {
116+
/* Check if a post has been checked in redis – if setting the key for the post returns null, it means it's already been set */
117+
return (
118+
(await redis.set(`post_${id}`, true, { nx: true, ex: 24 * 60 * 60 })) ===
119+
null
120+
);
121+
}
122+
115123
export interface TeamAndKeywords {
116124
[teamId: string]: string[];
117125
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.