From dbf8e345a3eed46d0871b2a5276142272cbadd2f Mon Sep 17 00:00:00 2001 From: gunshiz Date: Sun, 2 Aug 2026 18:21:31 +0700 Subject: [PATCH] fix(upload): isolate cron jobs in worker --- Dockerfile | 5 +++++ Dockerfile.scheduler | 8 -------- docker-compose.yml | 17 ++++++++++------- nginx.conf | 2 +- scripts/process-scheduled-video-uploads.sh | 18 ------------------ scripts/video-upload-crontab | 2 +- worker/video-upload.ts | 11 +++++++++++ 7 files changed, 28 insertions(+), 35 deletions(-) delete mode 100644 Dockerfile.scheduler delete mode 100644 scripts/process-scheduled-video-uploads.sh create mode 100644 worker/video-upload.ts diff --git a/Dockerfile b/Dockerfile index 61a86cb..0aa861d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,3 +32,8 @@ COPY --from=builder /app/package.json ./package.json EXPOSE 3000 CMD ["bun", "start"] + +# Stage 4: Scheduled video upload worker +FROM builder AS video-upload-worker +COPY scripts/video-upload-crontab /etc/crontabs/root +ENTRYPOINT ["crond", "-f", "-l", "2", "-L", "/dev/stdout"] diff --git a/Dockerfile.scheduler b/Dockerfile.scheduler deleted file mode 100644 index c62f487..0000000 --- a/Dockerfile.scheduler +++ /dev/null @@ -1,8 +0,0 @@ -FROM nginx:alpine - -COPY scripts/video-upload-crontab /etc/crontabs/root -COPY scripts/process-scheduled-video-uploads.sh /usr/local/bin/process-scheduled-video-uploads - -RUN chmod 0755 /usr/local/bin/process-scheduled-video-uploads - -ENTRYPOINT ["crond", "-f", "-l", "2", "-L", "/dev/stdout"] diff --git a/docker-compose.yml b/docker-compose.yml index 67c6cf2..a456669 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,8 @@ services: app: build: . restart: unless-stopped + ports: + - "8606:80" expose: - "3000" env_file: @@ -11,23 +13,24 @@ services: nginx: image: nginx:alpine - ports: - - "8606:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - - app + app: + condition: service_started + restart: true + network_mode: "service:app" restart: unless-stopped - video-upload-scheduler: + video-upload-worker: build: context: . - dockerfile: Dockerfile.scheduler + target: video-upload-worker restart: unless-stopped - depends_on: - - app env_file: - .env + volumes: + - /nfs/erika:/nfs/erika secret-scan: image: ghcr.io/gitleaks/gitleaks:v8.30.1 diff --git a/nginx.conf b/nginx.conf index d2fa502..4e163b8 100644 --- a/nginx.conf +++ b/nginx.conf @@ -33,7 +33,7 @@ http { listen 80; location / { - proxy_pass http://app:3000; + proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/scripts/process-scheduled-video-uploads.sh b/scripts/process-scheduled-video-uploads.sh deleted file mode 100644 index 32eac4d..0000000 --- a/scripts/process-scheduled-video-uploads.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -set -eu - -upload_cron_lock=/tmp/erika-video-upload-cron.lock - -if ! mkdir "$upload_cron_lock" 2>/dev/null; then - echo "Previous scheduled video upload run is still active; skipping this minute." - exit 0 -fi - -trap 'rmdir "$upload_cron_lock"' EXIT INT TERM - -wget -q -O /dev/null \ - --header="Authorization: Bearer ${CRON_SECRET}" \ - http://app:3000/api/cron/video-uploads || { - echo "Scheduled video upload processing failed." >&2 - exit 1 - } diff --git a/scripts/video-upload-crontab b/scripts/video-upload-crontab index a07a4f0..911946a 100644 --- a/scripts/video-upload-crontab +++ b/scripts/video-upload-crontab @@ -1 +1 @@ -* * * * * /usr/local/bin/process-scheduled-video-uploads +* * * * * bun run worker/video-upload.ts diff --git a/worker/video-upload.ts b/worker/video-upload.ts new file mode 100644 index 0000000..399b268 --- /dev/null +++ b/worker/video-upload.ts @@ -0,0 +1,11 @@ +import { processDueVideoUploads } from "@/lib/video/video-platforms"; + +console.log(`[${new Date().toISOString()}] Checking scheduled video uploads.`); + +try { + await processDueVideoUploads(); + console.log(`[${new Date().toISOString()}] Scheduled video upload check finished.`); +} catch (error) { + console.error("Scheduled video upload worker failed:", error); + process.exitCode = 1; +}