File tree 2 files changed +11
-0
lines changed
Filter options
2 files changed +11
-0
lines changed
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { getLatestPost, getPost } from "./hn";
2
2
import {
3
3
getLastCheckedId ,
4
4
setLastCheckedId ,
5
+ checkIfPostWasChecked ,
5
6
getTeamsAndKeywords ,
6
7
} from "./upstash" ;
7
8
import { equalsIgnoreOrder , postScanner } from "./helpers" ;
@@ -28,6 +29,8 @@ export async function cron() {
28
29
let errors : any [ ] = [ ] ;
29
30
30
31
for ( let i = lastCheckedId + 1 ; i <= latestPostId ; i ++ ) {
32
+ if ( await checkIfPostWasChecked ( i ) ) continue ; // avoid double checking posts
33
+
31
34
const post = await getPost ( i ) ; // get post from hacker news
32
35
if ( ! post ) {
33
36
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
Original file line number Diff line number Diff line change @@ -112,6 +112,14 @@ export async function setLastCheckedId(id: number) {
112
112
return await redis . set ( "lastCheckedId" , id ) ;
113
113
}
114
114
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
+
115
123
export interface TeamAndKeywords {
116
124
[ teamId : string ] : string [ ] ;
117
125
}
You can’t perform that action at this time.
0 commit comments