Skip to content

Commit

Permalink
move cron to server entry
Browse files Browse the repository at this point in the history
  • Loading branch information
AvidDabbler committed Feb 4, 2024
1 parent 89bad26 commit 374c137
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
28 changes: 17 additions & 11 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* 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'

Check failure on line 6 in app/entry.server.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

There should be at least one empty line between import groups

Check failure on line 6 in app/entry.server.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

`node-cron` import should occur after import of `isbot`
import { PassThrough } from "node:stream";

import type { EntryContext } from "@remix-run/node";
import { createReadableStreamFromReadable } from "@remix-run/node";
import { RemixServer } from "@remix-run/react";
import isbot from "isbot";
import { renderToPipeableStream } from "react-dom/server";

Check failure on line 13 in app/entry.server.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

There should be at least one empty line between import groups
import { backupS3Object } from 'drizzle/backup';

Check failure on line 14 in app/entry.server.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

There should be at least one empty line between import groups
import { envConfig } from './config.server';

const ABORT_DELAY = 5_000;

Expand All @@ -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(
Expand Down Expand Up @@ -118,3 +120,7 @@ function handleBrowserRequest(
setTimeout(abort, ABORT_DELAY);
});
}

cron.schedule('* * * * *', async () => {
await backupS3Object(envConfig.S3_BUCKET, 'data.db', 'drizzle/data.db')
})
2 changes: 1 addition & 1 deletion drizzle/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 374c137

Please sign in to comment.