diff --git a/Dockerfile b/Dockerfile index 8ffab3b..d3d1879 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,17 +11,7 @@ RUN cd sitio && \ RUN bun build scraper/cli.ts --target=bun --outfile=/tmp/cli.build.js FROM base -ARG S6_OVERLAY_VERSION=3.1.6.2 -RUN apk add --no-cache nodejs npm jq - -ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz /tmp -RUN tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz -ADD https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-x86_64.tar.xz /tmp -RUN tar -C / -Jxpf /tmp/s6-overlay-x86_64.tar.xz - -# Cron scraper -RUN printf "#!/bin/sh\nexec bun /bin/scraper auto\n" > /etc/periodic/daily/scraper \ - && chmod +x /etc/periodic/daily/scraper +RUN apk add --no-cache tini nodejs npm jq # Sitio COPY --from=build /usr/src/app/sitio/package.json package.real.json @@ -37,11 +27,5 @@ ENV NODE_ENV=production ENV DB_PATH=/db/db.db EXPOSE 3000 -# Servicios -RUN mkdir -p /etc/services.d/sitio /etc/services.d/scraper && \ - printf "#!/command/execlineb -P\nnode /usr/src/app\n" > /etc/services.d/sitio/run && \ - chmod +x /etc/services.d/sitio/run && \ - printf "#!/command/execlineb -P\nbusybox crond -f -l2\n" > /etc/services.d/scraper/run && \ - chmod +x /etc/services.d/scraper/run - -ENTRYPOINT ["/init"] \ No newline at end of file +ENTRYPOINT ["tini", "--"] +CMD ["node", "."] \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 237bd32..1dffe92 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/sitio/package.json b/sitio/package.json index 74677bc..575e1f4 100644 --- a/sitio/package.json +++ b/sitio/package.json @@ -38,6 +38,7 @@ "better-sqlite3": "^9.2.2", "chart.js": "^4.4.1", "chartjs-adapter-dayjs-4": "^1.0.4", + "cron": "^3.1.6", "dayjs": "^1.11.10", "drizzle-orm": "^0.29.1" } diff --git a/sitio/src/hooks.server.ts b/sitio/src/hooks.server.ts new file mode 100644 index 0000000..7358ae5 --- /dev/null +++ b/sitio/src/hooks.server.ts @@ -0,0 +1,17 @@ +import { spawn } from "child_process"; +import { CronJob } from "cron"; + +if (process.env.NODE_ENV === "production") { + const job = CronJob.from({ + cronTime: "0 3 * * *", + onTick: function () { + runScraper(); + }, + start: true, + timeZone: "America/Argentina/Buenos_Aires", + }); +} + +function runScraper() { + spawn("bun", ["/bin/scraper", "auto"], { stdio: "inherit" }); +}