अगर आपको किसी तय समय पर फ़ंक्शन चलाने के लिए शेड्यूल करना है, तो firebase-functions/v2/scheduler से मिले onSchedule हैंडलर का इस्तेमाल करें.
ये फ़ंक्शन, आपके तय किए गए समय या इंटरवल पर फ़ंक्शन लॉजिक को लागू करने के लिए, Cloud Scheduler का इस्तेमाल करते हैं.
शुरू करने से पहले
शेड्यूल किए गए फ़ंक्शन के लिए बिल भेजा जाता है. हालांकि, कुल लागत को मैनेज किया जा सकता है, क्योंकि हर Cloud Scheduler जॉब के लिए हर महीने 0.10 डॉलर (USD) खर्च होते हैं. साथ ही, हर Google खाते के लिए, बिना किसी शुल्क के तीन जॉब की अनुमति होती है. Blaze प्लान के प्राइसिंग कैलकुलेटर का इस्तेमाल करके, अपने अनुमानित इस्तेमाल के आधार पर लागत का अनुमान लगाएं.
आपके प्रोजेक्ट के लिए, Cloud Scheduler एपीआई चालू होना चाहिए. यह Firebase के ज़्यादातर प्रोजेक्ट के लिए पहले से ही चालू होना चाहिए. इसकी पुष्टि Google Cloud कंसोल में की जा सकती है.
शेड्यूल किया गया फ़ंक्शन लिखना
Cloud Functions for Firebase में, शेड्यूल करने का लॉजिक आपके फ़ंक्शन कोड में मौजूद होता है. इसके लिए, डिप्लॉय करने के समय कोई खास ज़रूरी शर्तें नहीं होती हैं. उदाहरण के लिए, हर दिन एक बार उन उपयोगकर्ता खातों को हटाने के लिए जो इस्तेमाल में नहीं हैं, यहां दिए गए इंपोर्ट स्टेटमेंट से शुरू होने वाला फ़ंक्शन लिखा जा सकता है:
Node.js
// The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onSchedule} = require("firebase-functions/scheduler");
const {logger} = require("firebase-functions");
// The Firebase Admin SDK to delete inactive users.
const {initializeApp} = require("firebase-admin/app");
const {getAuth} = require("firebase-admin/auth");
initializeApp();
// The es6-promise-pool to limit the concurrency of promises.
const PromisePool = /** @type {PromisePoolConstructor} */ (
require("es6-promise-pool")
);
// Maximum concurrent account deletions.
const MAX_CONCURRENT = 3;
Python
# The Cloud Functions for Firebase SDK to set up triggers and logging.
from firebase_functions import scheduler_fn
# The Firebase Admin SDK to delete users.
import firebase_admin
from firebase_admin import auth
firebase_admin.initialize_app()
इसके बाद, onSchedule का इस्तेमाल करके Cloud Scheduler टास्क शुरू किया जा सकता है:
Node.js
// Run once a day at midnight, to clean up the users
// Manually run the task here https://console.cloud.google.com/cloudscheduler
exports.accountcleanup = onSchedule("every day 00:00", async (event) => {
// Fetch all user details.
const inactiveUsers = await getInactiveUsers();
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
const promisePool = new PromisePool(
() => deleteInactiveUser(inactiveUsers),
MAX_CONCURRENT,
);
await promisePool.start();
logger.log("User cleanup finished");
});
Python
# Run once a day at midnight, to clean up inactive users.
# Manually run the task here https://console.cloud.google.com/cloudscheduler
@scheduler_fn.on_schedule(schedule="every day 00:00")
def accountcleanup(event: scheduler_fn.ScheduledEvent) -> None:
"""Delete users who've been inactive for 30 days or more."""
user_page: auth.ListUsersPage | None = auth.list_users()
while user_page is not None:
inactive_uids = [
user.uid for user in user_page.users if is_inactive(user, timedelta(days=30))
]
auth.delete_users(inactive_uids)
user_page = user_page.get_next_page()
Cloud Scheduler में Unix Crontab और App Engine, दोनों सिंटैक्स का इस्तेमाल किया जा सकता है. उदाहरण के लिए, Crontab का इस्तेमाल करने के लिए, कुछ ऐसा करें:
Node.js
exports.scheduledFunctionCrontab = onSchedule("5 11 * * *", async (event) => {
// ...
});
Python
@scheduler_fn.on_schedule(schedule="5 11 * * *")
शेड्यूल किया गया फ़ंक्शन डिप्लॉय करना
शेड्यूल किए गए फ़ंक्शन को डिप्लॉय करने पर, शेड्यूलर जॉब और एचटीटीपी फ़ंक्शन अपने-आप बन जाता है. Firebase सीएलआई, फ़ंक्शन का नाम दिखाता है. साथ ही, Google Cloud कंसोल में जॉब और फ़ंक्शन को देखा जा सकता है. विषय का नाम इस तरीके से रखा जाता है:
firebase-schedule-function_name-region
उदाहरण के लिए:
firebase-schedule-accountcleanup-us-east1.
तय किए गए समय पर, Compute का डिफ़ॉल्ट सेवा खाता, उससे जुड़े एचटीटीपी फ़ंक्शन को ट्रिगर करता है. इसका मतलब है कि सिर्फ़ इससे जुड़े Cloud Scheduler जॉब के पास फ़ंक्शन को चलाने की अनुमति है.