From 386bf732fcd54161d4ac4df70451d41cf1990e80 Mon Sep 17 00:00:00 2001 From: Sallin142 Date: Sun, 14 Jul 2024 16:57:38 -0700 Subject: [PATCH 1/2] feat/created a cronjob to get the data from keep_db_active table every day at midnight --- src/app/api/keep-db-active/route.ts | 19 +++++ src/cronJob.mjs | 22 ++++++ src/next.config.mjs | 13 +++- src/package-lock.json | 105 ++++++++++++++++++++++++++++ src/package.json | 2 + 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/app/api/keep-db-active/route.ts create mode 100644 src/cronJob.mjs diff --git a/src/app/api/keep-db-active/route.ts b/src/app/api/keep-db-active/route.ts new file mode 100644 index 0000000..77f60bb --- /dev/null +++ b/src/app/api/keep-db-active/route.ts @@ -0,0 +1,19 @@ +// src/app/api/keep-db-active/route.ts +import { NextRequest, NextResponse } from 'next/server'; +import supabase from "@/lib/supabase"; +export async function GET(req: NextRequest) { + const { data, error } = await supabase + .from('keep_db_active') + .select('*'); + + if (error) { + console.error(error); + return NextResponse.json({ error: 'Error fetching data' }, { status: 500 }); + } else { + return NextResponse.json(data, { status: 200 }); + } +} + +export const config = { + runtime: 'experimental-edge', +}; diff --git a/src/cronJob.mjs b/src/cronJob.mjs new file mode 100644 index 0000000..394c1fd --- /dev/null +++ b/src/cronJob.mjs @@ -0,0 +1,22 @@ +// cronJob.mjs +import cron from 'node-cron'; +import fetch from 'node-fetch'; + +const fetchData = async () => { + try { + const res = await fetch('http://localhost:3000/api/keep-db-active'); + const data = await res.json(); + console.log('Fetched data:', data); + } catch (error) { + console.error('Error fetching data:', error); + } +}; + +// Schedule the task to run every day at midnight +cron.schedule('0 0 * * *', fetchData); + +console.log('Cron job started: Fetching data every day at midnight'); + +export default function startCronJob() { + cron.schedule('0 0 * * *', fetchData); +} \ No newline at end of file diff --git a/src/next.config.mjs b/src/next.config.mjs index c4a407b..9420e81 100644 --- a/src/next.config.mjs +++ b/src/next.config.mjs @@ -1,2 +1,13 @@ +import startCronJob from '../src/cronJob.mjs'; + /** @type {import('next').NextConfig} */ -export default {}; +const nextConfig = { + webpack: (config, { isServer }) => { + if (isServer) { + startCronJob(); + } + return config; + }, +}; + +export default nextConfig; diff --git a/src/package-lock.json b/src/package-lock.json index ebf9b7f..5e83a35 100644 --- a/src/package-lock.json +++ b/src/package-lock.json @@ -10,6 +10,8 @@ "dependencies": { "@supabase/supabase-js": "^2.44.0", "next": "14.2.3", + "node-cron": "^3.0.3", + "node-fetch": "^3.3.2", "react": "^18.3.1", "react-dom": "^18.3.1", "ts-jest": "^29.1.5" @@ -3368,6 +3370,14 @@ "integrity": "sha512-sdQSFB7+llfUcQHUQO3+B8ERRj0Oa4w9POWMI/puGtuf7gFywGmkaLCElnudfTiKZV+NvHqL0ifzdrI8Ro7ESA==", "dev": true }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "engines": { + "node": ">= 12" + } + }, "node_modules/data-view-buffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz", @@ -4561,6 +4571,28 @@ "bser": "2.1.1" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -4645,6 +4677,17 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/fresh": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", @@ -7602,6 +7645,52 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/node-cron": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz", + "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", @@ -9643,6 +9732,14 @@ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "dev": true }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-to-istanbul": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", @@ -9664,6 +9761,14 @@ "makeerror": "1.0.12" } }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "engines": { + "node": ">= 8" + } + }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/src/package.json b/src/package.json index c509570..a3e67f7 100644 --- a/src/package.json +++ b/src/package.json @@ -20,6 +20,8 @@ "dependencies": { "@supabase/supabase-js": "^2.44.0", "next": "14.2.3", + "node-cron": "^3.0.3", + "node-fetch": "^3.3.2", "react": "^18.3.1", "react-dom": "^18.3.1", "ts-jest": "^29.1.5" From e58c2cb8c6a6c1890b09ebd49b05f261257b53e3 Mon Sep 17 00:00:00 2001 From: Sallin142 Date: Sun, 14 Jul 2024 17:16:18 -0700 Subject: [PATCH 2/2] fix/ fixed linter --- src/app/api/keep-db-active/route.ts | 21 ++++++++------------- src/cronJob.mjs | 18 +++++++++--------- src/next.config.mjs | 4 ++-- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/app/api/keep-db-active/route.ts b/src/app/api/keep-db-active/route.ts index 77f60bb..2e8f4f8 100644 --- a/src/app/api/keep-db-active/route.ts +++ b/src/app/api/keep-db-active/route.ts @@ -1,19 +1,14 @@ // src/app/api/keep-db-active/route.ts -import { NextRequest, NextResponse } from 'next/server'; +/* eslint-disable import/prefer-default-export */ +import { NextResponse } from "next/server"; import supabase from "@/lib/supabase"; -export async function GET(req: NextRequest) { - const { data, error } = await supabase - .from('keep_db_active') - .select('*'); + +export async function GET() { + const { data, error } = await supabase.from("keep_db_active").select("*"); if (error) { - console.error(error); - return NextResponse.json({ error: 'Error fetching data' }, { status: 500 }); - } else { - return NextResponse.json(data, { status: 200 }); + return NextResponse.json({ error: "Error fetching data" }, { status: 500 }); } -} -export const config = { - runtime: 'experimental-edge', -}; + return NextResponse.json(data, { status: 200 }); +} diff --git a/src/cronJob.mjs b/src/cronJob.mjs index 394c1fd..7f4a0a8 100644 --- a/src/cronJob.mjs +++ b/src/cronJob.mjs @@ -1,22 +1,22 @@ // cronJob.mjs -import cron from 'node-cron'; -import fetch from 'node-fetch'; +import cron from "node-cron"; +import fetch from "node-fetch"; const fetchData = async () => { try { - const res = await fetch('http://localhost:3000/api/keep-db-active'); + const res = await fetch("http://localhost:3000/api/keep-db-active"); const data = await res.json(); - console.log('Fetched data:', data); + console.log("Fetched data:", data); } catch (error) { - console.error('Error fetching data:', error); + console.error("Error fetching data:", error); } }; // Schedule the task to run every day at midnight -cron.schedule('0 0 * * *', fetchData); +cron.schedule("* * * * * *", fetchData); -console.log('Cron job started: Fetching data every day at midnight'); +console.log("Cron job started: Fetching data every second"); export default function startCronJob() { - cron.schedule('0 0 * * *', fetchData); -} \ No newline at end of file + cron.schedule("* * * * * *", fetchData); +} diff --git a/src/next.config.mjs b/src/next.config.mjs index 9420e81..1285502 100644 --- a/src/next.config.mjs +++ b/src/next.config.mjs @@ -1,4 +1,4 @@ -import startCronJob from '../src/cronJob.mjs'; +import startCronJob from "../src/cronJob.mjs"; /** @type {import('next').NextConfig} */ const nextConfig = { @@ -7,7 +7,7 @@ const nextConfig = { startCronJob(); } return config; - }, + } }; export default nextConfig;