-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
68 lines (58 loc) · 2.12 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { fastifyConnectPlugin } from "@connectrpc/connect-fastify";
import { fastifySchedule } from "@fastify/schedule";
import { PrismaClient } from "@prisma/client";
import { fastify } from "fastify";
import { AsyncTask, SimpleIntervalJob } from "toad-scheduler";
import routes from "./connect.js";
import { scrape } from "./scraper.js";
const scrapeTags = process.env.SCRAPE_TAGS?.split(",") ?? [];
if (scrapeTags.length === 0) {
console.error("Invalid SCRAPE_TAGS: %s", process.env.SCRAPE_TAGS);
process.exit(1);
}
const scrapeInterval = process.env.SCRAPE_INTERVAL ? parseInt(process.env.SCRAPE_INTERVAL, 10) : 600;
if (Number.isNaN(scrapeInterval) || scrapeInterval < 0) {
console.error("Invalid SCRAPE_INTERVAL: %s", process.env.SCRAPE_INTERVAL);
process.exit(1);
}
const scrapeDuration = process.env.SCRAPE_DURATION ? parseInt(process.env.SCRAPE_DURATION, 10) : 172800;
if (Number.isNaN(scrapeDuration) || scrapeDuration < 0) {
console.error("Invalid SCRAPE_DURATION: %s", process.env.SCRAPE_DURATION);
process.exit(1);
}
const prisma = new PrismaClient();
const scrapeTask = new AsyncTask(
"scrape",
async () => {
const result = await scrape({
tags: scrapeTags,
duration: scrapeDuration * 1000,
today: new Date(),
});
await prisma.$transaction(
result.videos.map((video) =>
prisma.nicovideoVideo.upsert({
where: { sourceId: video.sourceId },
create: { sourceId: video.sourceId, postedAt: new Date(video.postedAt) },
update: {},
}),
),
);
},
() => {},
);
const scrapeJob = new SimpleIntervalJob({ seconds: scrapeInterval, runImmediately: true }, scrapeTask);
const server = fastify({ logger: true });
await server.register(fastifySchedule);
await server.register(fastifyConnectPlugin, { routes });
server.get("/", (_, reply) => {
reply.type("text/plain");
reply.send("Hello World!");
});
await server.ready();
await server.scheduler.addSimpleIntervalJob(scrapeJob);
await server.listen({
host: process.env.NODE_ENV === "production" ? "0.0.0.0" : "localhost",
port: 58080,
});
console.log("server is listening at", server.addresses());