Help: Cron jobs scheduler

Hello, greetings dear helper. I am a non programmer but have built really good 3 production web applications that are currently gaining traction.

One is being used by a company as an intranet/operational tool for 360 degree lifecycle of the company. I’m impressed thanks to Firebase studio.

I did prompted for cron jobs and daily scheduler and the agent said everything was good but I realise that those schedules never gets to run.

I read online where they said I need a cloud function or cron scheduler or so. So I asked the agent several times, the AI keeps saying I don’t need it that the work we have done is okay.

However, my cron have not been running resulting to missing notifications, missing tasks such as billing renewal for users, account renewal, transactional emails sending etc.

I need your experience advice and recommendations on what to do.

Thanks.

Hello! It’s fantastic to hear you’ve built three production apps with Firebase Studio—that’s a very impressive achievement.

We can definitely clear up the confusion about your scheduled tasks. Your intuition and your online research are correct. You absolutely do need a server-side solution to run tasks on a schedule.

The AI agent was likely unclear. It can write the code for a task, but that code needs a place to run persistently.

The Problem: Your App vs. Firebase’s Servers

Here’s the key difference:

  • Your Web App (Frontend): The code in your web app only runs when a user has it open in their browser. The moment they close the tab, the code stops. This is why setInterval or other in-app schedulers won’t work for billing or daily notifications.

  • Firebase (Backend): You need a piece of code that lives on Firebase’s servers and runs 24/7, completely independent of whether any user has your app open.

The Solution: Scheduled Cloud Functions

The official, supported way to do this in Firebase is with Scheduled Cloud Functions.

A Scheduled Function is a piece of backend code that you deploy to Firebase. You tell Firebase when to run it (e.g., “every day at 2:00 AM” or “every 5 minutes”), and Firebase’s internal cron service (Cloud Scheduler) will trigger it automatically.

This is the perfect and most reliable way to handle all the tasks you mentioned:

  • Sending renewal notifications

  • Processing billing

  • Sending transactional emails

  • Cleaning up old data

How to Fix This in Firebase Studio

You can use the Firebase Studio agent to help you create this. The key is to use the right prompts.

Step 1: Ask the Agent to Create a Scheduled Function

Instead of asking for a “cron job,” try being very specific with these prompts:

“Generate a scheduled Cloud Function that runs once every 24 hours.”

Or, you can be even more specific about the task:

“Create a scheduled Cloud Function using Node.js that runs every day at 9:00 AM. This function should query my ‘subscriptions’ collection in Firestore and find all documents where ‘renewalDate’ is tomorrow, then send them a notification.”

Step 2: Understand What the Agent Will Create

The agent should create a new file, or add to an existing one, inside a functions folder in your project. The code will look something like this:

JavaScript

// This will likely be in a file like /functions/index.js

const {onSchedule} = require("firebase-functions/v2/scheduler");
const {logger} = require("firebase-functions");

// This is the function definition.
// The "every day 09:00" part is the schedule (called "cron syntax").
exports.sendRenewalEmails = onSchedule("every day 09:00", async (event) => {
  
  logger.info("Running the daily renewal email job...");

  // TODO: Add your logic here
  // 1. Query Firestore for users to bill.
  // 2. Loop through the users.
  // 3. Send an email or notification.

  logger.info("Daily renewal job complete.");
  return null;
});

Step 3: Deploy the Function

This is the most important step. Just writing the code doesn’t make it run. You must deploy it to your Firebase project.

Open the terminal inside Firebase Studio and run this command:

Bash

firebase deploy --only functions

This command will upload your functions folder to Firebase. Once it’s deployed, Firebase will automatically set up the schedule, and your function will start running as defined.

Summary: Your Path Forward

  1. Your intuition was right. You need a backend scheduler.

  2. The correct Firebase service is Scheduled Cloud Functions.

  3. Use the Firebase Studio agent to generate the code for a scheduled function (use the prompts above).

  4. Deploy the function using the firebase deploy --only functions command in your terminal.

You can check the status of your scheduled functions and see their logs in the Functions section of your Firebase Console (the website).

I hope this clears things up! Let us know if you have more questions.

@rody thank you so much for such detailed reply. This is so clear. I’ll deploy this on my second app. However, on the first app, I went to https://console.cloud.google.com/cloudscheduler and created a schedule using my endpoint /api/cron which is running twice a day as specified. Is this the same thing perhaps I just did it in a different way? or the cloud function would have been better?

That’s a great question! You’ve absolutely found the “engine” that powers all of this.

Yes, what you did is almost exactly the same thing! You just built the two pieces manually, whereas the onSchedule function I described bundles them together for you.

Here’s the breakdown:

Method 1: Your Way (Manual HTTP Trigger)

  • What it is: You went to Cloud Scheduler (the Google Cloud service for cron jobs) and told it to send an HTTP request to your app’s public /api/cron endpoint at a specific time.

  • How it works: Cloud SchedulerPublic HTTP Endpoint

  • Pros: It works! It’s clear and gives you direct control in the Google Cloud console.

  • Cons (The BIG One): Security. Your /api/cron endpoint is now public. Anyone on the internet who finds that URL can run it, potentially triggering your billing or notification tasks over and over.

    • To fix this, you must manually secure that endpoint. The standard way is to have your endpoint code check for a specific, secret header (like X-CloudScheduler-Token: "my-secret-string") that you set up in the Cloud Scheduler job.

Method 2: The onSchedule Way (Firebase SDK)

  • What it is: You write an onSchedule function in your functions/index.js file and deploy it.

  • How it works: When you deploy, Firebase automatically does two things for you:

    1. It creates a private Cloud Function.

    2. It creates a Cloud Scheduler job (in that same console you found!) and configures it to trigger that private function.

  • Pros: It’s secure by default. The Cloud Function it creates is not public. It’s set up with IAM permissions so that only that specific Cloud Scheduler job can ever run it. No one on the internet can access it, even if they guess the name. It’s simpler to manage because the schedule ("every day 09:00") and the code live together in one file.


So, Is One Better?

For most Firebase projects, the onSchedule method is better simply because it’s more secure out-of-the-box and easier to manage from your codebase.

Your current method is perfectly fine and a very common pattern, as long as you have secured that /api/cron endpoint. If you haven’t, I would strongly recommend switching to the onSchedule method to prevent unauthorized access.

Great job on figuring out the Google Cloud console side of things!

2 Likes

This is super great discovery you’ve given to me today. Thanks a lot dear. I appreciate this.