How do I create a second file that runs cron locally and executes server commands?

I have a nextjs app that does a nice serverless db on vercel. I want to create some cron jobs that leverage some functions in the server folder. I thought to myself, I could just host a seperate server that can carry out the functions on a schedule using cron. It seems though that if i create a new folder and create a new app that runs cron.schedule fails to execute code in that server/ directory. I was thinking that with the organization of the t3 app, I could could just create a new ts file and create a script command to execute this and set up everything but it seems to have issues with it being a module.
import cron from "node-cron";
cron.schedule("*/5 * * * * *", () => {
console.log("Event Triggered", Date.now());
});
import cron from "node-cron";
cron.schedule("*/5 * * * * *", () => {
console.log("Event Triggered", Date.now());
});
It a simple sample. Now Say I have a something located in server/api/routers to use. Note, I am going to move the code, but the underlying code is not actually a router, it is a function that leverages db object, and some schema information. Example:
import db from "~/server/db";
export const updateLocations = async () => {
db.transaction( async (ctx) => {
const accounts = await ctx.query.getMany({});
});
}
import db from "~/server/db";
export const updateLocations = async () => {
db.transaction( async (ctx) => {
const accounts = await ctx.query.getMany({});
});
}
And when I incorporate it, I would say just add it to the file:
import { updateLocations } from "./src/server/api/routers/sample.ts";
cron.schedule("*/5 * * * * *", async () => {
console.log("Event Triggered", Date.now());
await updateLocations();
});
import { updateLocations } from "./src/server/api/routers/sample.ts";
cron.schedule("*/5 * * * * *", async () => {
console.log("Event Triggered", Date.now());
await updateLocations();
});
But When I attempt to run the file, I define in my package:
{
"scripts": {
"cron": "npx ts-node path/to/server.ts",
}
}
{
"scripts": {
"cron": "npx ts-node path/to/server.ts",
}
}
It will not like it because it is a module. How would Somoene go about creating something like this that I can execute the db commands and functions I previously set up, even thought it isnt itself running a NextJs App anymore, but instead just a small server execution specific functions not scoped by auth etc. I feel like I am running in circles here. How would I set this up?
5 Replies
__Fallenreaper
__FallenreaperOP3mo ago
I was tempted to create a monorepo of sorts, so that way I could organize things differently, but givent that it is almost already well set up enough, that there could be a way to run this local server cron that isnt running say, the actual next app. A repo to actually see what I want to do is located at: https://github.com/fallenreaper/cron-server-error
GitHub
GitHub - fallenreaper/cron-server-error
Contribute to fallenreaper/cron-server-error development by creating an account on GitHub.
__Fallenreaper
__FallenreaperOP3mo ago
Solved. Spend a long time setting it up. When i used dotenv, it seemed like i was using it correctly, but i wasnt. Once i updated it, it worked. Sadly, that burned me of like 10+ hours.
Lukem121
Lukem1212mo ago
Cron Jobs
Learn about cron jobs, how they work, and how to use them on Vercel.
Lukem121
Lukem1212mo ago
Create a vercel.json in the root of yoru project
{
"crons": [
{
"path": "/api/cron/synchronize-active-orders",
"schedule": "0 */2 * * *"
}
]
}
{
"crons": [
{
"path": "/api/cron/synchronize-active-orders",
"schedule": "0 */2 * * *"
}
]
}
Check out https://vercel.com/<org>/<project_name>/settings/cron-jobs
__Fallenreaper
__FallenreaperOP2mo ago
There are limits when it comes to cron jobs. Since I have tools that need to execute every 5 seconds, in an indef timeframe, the hobby tier does not support it. So, what I was doing was creating a script that executes it as a list of crons that execute. So i have my own server which will handle all the cron eventing and push results to the neon db for consumer user. Similarly, because of its use, it could also be managed by a Github actions utility that runs for a length of time as well before restarting on Action expires. I have a decent server that can handle this lifting, abstracted from vercel requirements.

Did you find this page helpful?