correr cron en sitio

This commit is contained in:
Cat /dev/Nulo 2024-01-06 21:17:18 -03:00
parent 14873c95db
commit 43954d7af8
4 changed files with 21 additions and 19 deletions

View file

@ -11,17 +11,7 @@ RUN cd sitio && \
RUN bun build scraper/cli.ts --target=bun --outfile=/tmp/cli.build.js RUN bun build scraper/cli.ts --target=bun --outfile=/tmp/cli.build.js
FROM base FROM base
ARG S6_OVERLAY_VERSION=3.1.6.2 RUN apk add --no-cache tini nodejs npm jq
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
# Sitio # Sitio
COPY --from=build /usr/src/app/sitio/package.json package.real.json 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 ENV DB_PATH=/db/db.db
EXPOSE 3000 EXPOSE 3000
# Servicios ENTRYPOINT ["tini", "--"]
RUN mkdir -p /etc/services.d/sitio /etc/services.d/scraper && \ CMD ["node", "."]
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"]

BIN
bun.lockb

Binary file not shown.

View file

@ -38,6 +38,7 @@
"better-sqlite3": "^9.2.2", "better-sqlite3": "^9.2.2",
"chart.js": "^4.4.1", "chart.js": "^4.4.1",
"chartjs-adapter-dayjs-4": "^1.0.4", "chartjs-adapter-dayjs-4": "^1.0.4",
"cron": "^3.1.6",
"dayjs": "^1.11.10", "dayjs": "^1.11.10",
"drizzle-orm": "^0.29.1" "drizzle-orm": "^0.29.1"
} }

17
sitio/src/hooks.server.ts Normal file
View file

@ -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" });
}