Bird
Raised Fist0
Firebasecloud~10 mins

Scheduled functions in Firebase - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Scheduled functions
Define schedule with cron
↓
Deploy function to cloud
↓
Cloud scheduler triggers
↓
Function runs code
↓
Function completes task
↓
Wait for next scheduled time
Scheduled functions run automatically at set times defined by a schedule, triggered by the cloud scheduler.
Execution Sample
Firebase
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This runs every 5 minutes');
  return null;
});
This function logs a message every 5 minutes automatically.
Process Table
StepActionSchedule TimeFunction TriggeredFunction Output
1Deploy function with schedule 'every 5 minutes'N/ANoN/A
2Cloud scheduler waits for next trigger00:00NoN/A
3Scheduler triggers function00:05YesLogs 'This runs every 5 minutes'
4Function completes execution00:05YesReturns null
5Scheduler waits for next trigger00:10NoN/A
6Scheduler triggers function again00:10YesLogs 'This runs every 5 minutes'
7Function completes execution00:10YesReturns null
8Process repeats every 5 minutesN/AN/AN/A
💡 Scheduled functions run repeatedly at defined intervals until undeployed.
Status Tracker
VariableStartAfter 1 TriggerAfter 2 TriggersFinal
scheduleevery 5 minutesevery 5 minutesevery 5 minutesevery 5 minutes
functionTriggeredfalsetruetruetrue
functionOutputnonelogged message + null returnlogged message + null returnlogged message + null return
Key Moments - 3 Insights
Why doesn't the function run immediately after deployment?
Because the function only runs when the cloud scheduler triggers it at the scheduled time, as shown in execution_table step 2.
What happens if the function code takes longer than the schedule interval?
The scheduler triggers at the scheduled interval regardless. Concurrent executions may occur if runtime exceeds the interval (deploy with --max-instances 1 to prevent overlap). The execution_table assumes quick execution.
Can the schedule be changed after deployment?
Yes, but you must update the schedule in the code and redeploy, as the schedule variable in variable_tracker remains constant until redeployment.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the function first run?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Function Triggered' column in execution_table rows.
According to variable_tracker, what is the value of 'functionTriggered' after the second trigger?
Afalse
Btrue
Cnull
Dundefined
💡 Hint
Look at the 'After 2 Triggers' column for 'functionTriggered' in variable_tracker.
If the schedule changed to 'every 10 minutes', how would the execution_table change?
AScheduler triggers at 00:10 and 00:20
BScheduler triggers every minute
CScheduler triggers at 00:05 and 00:10
DFunction runs immediately after deployment
💡 Hint
Refer to the 'Schedule Time' column in execution_table and think about the interval.
Concept Snapshot
Scheduled functions run code automatically at set times.
Define schedule using cron or preset strings.
Deploy function to cloud with schedule.
Cloud scheduler triggers function at intervals.
Function runs, completes, then waits for next trigger.
Full Transcript
Scheduled functions in Firebase run automatically at times you set. You write a function and attach a schedule like 'every 5 minutes'. After deploying, the cloud scheduler waits and triggers your function at those times. The function runs its code, like logging a message, then finishes. This repeats forever until you stop it. The schedule controls when the function runs, not immediate deployment. If the function takes longer than the interval, concurrent executions may occur (deploy with --max-instances 1 to prevent). You can change the schedule by updating code and redeploying.

Practice

(1/5)
1. What is the main purpose of Firebase Scheduled Functions?
easy
A. To run code automatically at specific times without manual intervention
B. To store user data securely in the cloud
C. To create real-time chat applications
D. To manage user authentication and login

Solution

  1. Step 1: Understand the role of scheduled functions

    Scheduled functions are designed to run tasks automatically at set times without needing a user to start them.
  2. Step 2: Compare with other Firebase features

    Other options like storing data, chat apps, or authentication are handled by different Firebase services, not scheduled functions.
  3. Final Answer:

    To run code automatically at specific times without manual intervention -> Option A
  4. Quick Check:

    Scheduled functions = automatic timed code run [OK]
Hint: Scheduled functions run code on a timer, no clicks needed [OK]
Common Mistakes:
  • Confusing scheduled functions with database storage
  • Thinking scheduled functions handle user login
  • Assuming scheduled functions manage real-time data
2. Which of the following is the correct way to schedule a Firebase function to run every day at midnight using cron syntax?
easy
A. functions.pubsub.schedule('every day 00:00').onRun(...)
B. functions.pubsub.schedule('daily at midnight').onRun(...)
C. functions.pubsub.schedule('0 0 * * *').onRun(...)
D. functions.pubsub.schedule('midnight every day').onRun(...)

Solution

  1. Step 1: Identify correct cron syntax for midnight daily

    The cron expression '0 0 * * *' means minute 0, hour 0, every day, which is midnight daily.
  2. Step 2: Match with Firebase schedule method

    Firebase uses cron syntax strings like '0 0 * * *' in schedule() to set times precisely.
  3. Final Answer:

    functions.pubsub.schedule('0 0 * * *').onRun(...) -> Option C
  4. Quick Check:

    Cron '0 0 * * *' = midnight daily [OK]
Hint: Cron '0 0 * * *' means every day at midnight [OK]
Common Mistakes:
  • Using natural language instead of cron syntax
  • Incorrect cron format like 'every day 00:00'
  • Misplacing hour and minute values in cron
3. Given the following Firebase scheduled function code, what will it log when triggered?
exports.dailyTask = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('Task running at', new Date().toISOString());
  return null;
});
medium
A. Logs 'Task running at' once at deployment time only
B. Runs continuously without delay, flooding logs
C. Throws a syntax error due to invalid schedule string
D. Logs 'Task running at' with the current timestamp every 5 minutes

Solution

  1. Step 1: Understand the schedule string 'every 5 minutes'

    This string tells Firebase to run the function every 5 minutes automatically.
  2. Step 2: Analyze the function behavior

    Each time it runs, it logs the message with the current timestamp in ISO format.
  3. Final Answer:

    Logs 'Task running at' with the current timestamp every 5 minutes -> Option D
  4. Quick Check:

    Schedule 'every 5 minutes' triggers repeated logs [OK]
Hint: Schedule string 'every 5 minutes' means repeated logs every 5 minutes [OK]
Common Mistakes:
  • Thinking it runs only once at deployment
  • Assuming schedule string is invalid syntax
  • Believing it runs nonstop without delay
4. What is wrong with this Firebase scheduled function code?
exports.cleanup = functions.pubsub.schedule('every 60 minutes').onRun(() => {
  // cleanup code here
});
medium
A. The schedule string 'every 60 minutes' is invalid; should use 'every 1 hour' or cron syntax
B. The function must return a promise or null, missing return statement
C. The function name 'cleanup' is reserved and cannot be used
D. The schedule method requires a timezone parameter

Solution

  1. Step 1: Check the schedule string validity

    Firebase does not recognize 'every 60 minutes'; it supports 'every 1 hour' or cron expressions.
  2. Step 2: Review other code parts

    Returning a promise or null is recommended but not mandatory here; function name and timezone are not issues.
  3. Final Answer:

    The schedule string 'every 60 minutes' is invalid; should use 'every 1 hour' or cron syntax -> Option A
  4. Quick Check:

    Invalid schedule string = error [OK]
Hint: Use 'every 1 hour' not 'every 60 minutes' in schedule [OK]
Common Mistakes:
  • Using unsupported time phrases in schedule
  • Forgetting to return null or a promise
  • Assuming timezone is always required
5. You want to create a Firebase scheduled function that runs every Monday at 9:30 AM in New York time to send weekly reports. Which is the correct way to configure this function?
hard
A. functions.pubsub.schedule('every Monday 9:30').timeZone('America/New_York').onRun(...)
B. functions.pubsub.schedule('30 9 * * 1').timeZone('America/New_York').onRun(...)
C. functions.pubsub.schedule('0 9 * * 1').timeZone('EST').onRun(...)
D. functions.pubsub.schedule('30 9 * * 1').onRun(...)

Solution

  1. Step 1: Use correct cron syntax for Monday 9:30 AM

    The cron '30 9 * * 1' means minute 30, hour 9, every Monday (day 1).
  2. Step 2: Set proper timezone for New York

    Use the full IANA timezone string 'America/New_York' to handle daylight savings correctly.
  3. Step 3: Verify other options

    functions.pubsub.schedule('every Monday 9:30').timeZone('America/New_York').onRun(...) uses invalid schedule string; functions.pubsub.schedule('0 9 * * 1').timeZone('EST').onRun(...) uses 'EST' which is not a valid timezone identifier; functions.pubsub.schedule('30 9 * * 1').onRun(...) misses timezone setting.
  4. Final Answer:

    functions.pubsub.schedule('30 9 * * 1').timeZone('America/New_York').onRun(...) -> Option B
  5. Quick Check:

    Cron + correct timezone = functions.pubsub.schedule('30 9 * * 1').timeZone('America/New_York').onRun(...) [OK]
Hint: Use cron '30 9 * * 1' plus 'America/New_York' timezone [OK]
Common Mistakes:
  • Using natural language instead of cron syntax
  • Using short timezone codes like 'EST' instead of full IANA names
  • Forgetting to set timezone causing wrong run times
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard