From 374c1374ca8cf40c0f8b79500e3b4ee36cc4d547 Mon Sep 17 00:00:00 2001 From: Walter Jenkins Date: Sun, 4 Feb 2024 17:24:57 -0600 Subject: [PATCH] move cron to server entry --- app/entry.server.tsx | 28 +++++++++++++++++----------- drizzle/backup.ts | 2 +- start.sh | 3 ++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/entry.server.tsx b/app/entry.server.tsx index e099bfa..1e102e1 100644 --- a/app/entry.server.tsx +++ b/app/entry.server.tsx @@ -3,7 +3,7 @@ * You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨ * For more information, see https://remix.run/docs/en/main/file-conventions/entry.server */ - +import cron from 'node-cron' import { PassThrough } from "node:stream"; import type { EntryContext } from "@remix-run/node"; @@ -11,6 +11,8 @@ import { createReadableStreamFromReadable } from "@remix-run/node"; import { RemixServer } from "@remix-run/react"; import isbot from "isbot"; import { renderToPipeableStream } from "react-dom/server"; +import { backupS3Object } from 'drizzle/backup'; +import { envConfig } from './config.server'; const ABORT_DELAY = 5_000; @@ -22,17 +24,17 @@ export default function handleRequest( ) { return isbot(request.headers.get("user-agent")) ? handleBotRequest( - request, - responseStatusCode, - responseHeaders, - remixContext, - ) + request, + responseStatusCode, + responseHeaders, + remixContext, + ) : handleBrowserRequest( - request, - responseStatusCode, - responseHeaders, - remixContext, - ); + request, + responseStatusCode, + responseHeaders, + remixContext, + ); } function handleBotRequest( @@ -118,3 +120,7 @@ function handleBrowserRequest( setTimeout(abort, ABORT_DELAY); }); } + +cron.schedule('* * * * *', async () => { + await backupS3Object(envConfig.S3_BUCKET, 'data.db', 'drizzle/data.db') +}) diff --git a/drizzle/backup.ts b/drizzle/backup.ts index 985c78e..9f99800 100644 --- a/drizzle/backup.ts +++ b/drizzle/backup.ts @@ -8,7 +8,7 @@ const s3Client = new S3Client({ }); // Function to perform S3 object copy (backup) -async function backupS3Object(bucket: string, sourceKey: string, destinationKey: string) { +export async function backupS3Object(bucket: string, sourceKey: string, destinationKey: string) { try { const copyObjectCommand = new CopyObjectCommand({ Bucket: envConfig.S3_BUCKET, // Replace with your S3 bucket name diff --git a/start.sh b/start.sh index 87ddc2d..3b9fd73 100755 --- a/start.sh +++ b/start.sh @@ -16,5 +16,6 @@ # npx prisma migrate deploy ## Enables WAL Mode -npx concurrently "npm run backup" "npm run start" +npm run start +# npx concurrently "npm run backup" "npm run start" # exec litestream replicate -exec "npm run start" /drizzle/data.db s3://simple-transit-site/data.db