-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import fetch from "@/lib/data"; | ||
import { db } from "@/lib/db/connection"; | ||
import { reify } from "@/lib/db/domains"; | ||
import { sql } from "kysely"; | ||
import pino from "pino"; | ||
|
||
const logger = pino({ | ||
name: "cron-refresh", | ||
}); | ||
|
||
const RAW_QUERY = sql<{ | ||
domain: string; | ||
}>` | ||
select domain from tranco TABLESAMPLE system (0.01) | ||
`; | ||
|
||
const getRandomDomains = async () => { | ||
const domains = await RAW_QUERY.execute(db); | ||
return domains.rows.map((row) => row.domain); | ||
}; | ||
|
||
const MAXIMUM_DOMAINS = 10; | ||
|
||
export async function GET( | ||
request: Request, | ||
context: { | ||
params: { | ||
domain: string; | ||
}; | ||
}, | ||
) { | ||
const domains = await getRandomDomains(); | ||
const selectedDomains = domains.slice(0, MAXIMUM_DOMAINS); | ||
await Promise.all(selectedDomains.map(async (domain) => { | ||
logger.info({ | ||
message: "refresh.started", | ||
domain: domain, | ||
}); | ||
const rawResponse = await fetch(domain); | ||
await reify(domain, rawResponse); | ||
})); | ||
|
||
return Response.json({ | ||
domains: selectedDomains, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"crons": [ | ||
{ | ||
"path": "/api/crons/refresh", | ||
"schedule": "* * * * *" | ||
} | ||
] | ||
} |