Skip to content

Commit

Permalink
Merge pull request #43 from SFU-Blueprint/feature/continuous-update
Browse files Browse the repository at this point in the history
feat/created a cronjob to get the data from keep_db_active table ever…
  • Loading branch information
Sallin142 authored Jul 15, 2024
2 parents 6e41af6 + e58c2cb commit fb4635f
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/app/api/keep-db-active/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// src/app/api/keep-db-active/route.ts
/* eslint-disable import/prefer-default-export */
import { NextResponse } from "next/server";
import supabase from "@/lib/supabase";

export async function GET() {
const { data, error } = await supabase.from("keep_db_active").select("*");

if (error) {
return NextResponse.json({ error: "Error fetching data" }, { status: 500 });
}

return NextResponse.json(data, { status: 200 });
}
22 changes: 22 additions & 0 deletions src/cronJob.mjs
Original file line number Diff line number Diff line change
@@ -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("* * * * * *", fetchData);

console.log("Cron job started: Fetching data every second");

export default function startCronJob() {
cron.schedule("* * * * * *", fetchData);
}
13 changes: 12 additions & 1 deletion src/next.config.mjs
Original file line number Diff line number Diff line change
@@ -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;
105 changes: 105 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fb4635f

Please sign in to comment.