फ़ंक्शन शेड्यूल करें

अगर आपको तय समय पर फ़ंक्शन शेड्यूल करने हैं, तो firebase-functions/v2/scheduler से मिले onSchedule हैंडलर का इस्तेमाल करें. ये फ़ंक्शन, Cloud Scheduler का इस्तेमाल करके, आपके तय किए गए समय या इंटरवल पर फ़ंक्शन लॉजिक को लागू करते हैं.

शुरू करने से पहले

शेड्यूल किए गए फ़ंक्शन के लिए बिल भेजा जाता है. हालांकि, कुल लागत को मैनेज किया जा सकता है. ऐसा इसलिए, क्योंकि हर Cloud Scheduler जॉब की लागत हर महीने 0.10 डॉलर (यूएसडी) होती है. साथ ही, हर Google खाते के लिए तीन जॉब की अनुमति होती है. इसके लिए कोई शुल्क नहीं लिया जाता. अपने अनुमानित इस्तेमाल के आधार पर लागत का अनुमान पाने के लिए, ब्लेज़ प्लान के प्राइसिंग कैलकुलेटर का इस्तेमाल करें.

आपके प्रोजेक्ट के लिए, Cloud Scheduler API चालू होना चाहिए. ज़्यादातर Firebase प्रोजेक्ट के लिए, यह पहले से ही चालू होता है. इसकी पुष्टि Google Cloud Console में की जा सकती है.

शेड्यूल किया गया फ़ंक्शन लिखना

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 admin = require("firebase-admin");
admin.initializeApp();

// The es6-promise-pool to limit the concurrency of promises.
const PromisePool = require("es6-promise-pool").default;
// 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 सिंटैक्स के साथ काम करता हैCloud Scheduler. उदाहरण के लिए, Crontab का इस्तेमाल करने के लिए, इस तरह का कोड लिखें:

Node.js

exports.scheduledFunctionCrontab = onSchedule("5 11 * * *", async (event) => {
  // ...
});

Python

@scheduler_fn.on_schedule(schedule="5 11 * * *")

शेड्यूल किए गए फ़ंक्शन को डिप्लॉय करना

शेड्यूल किए गए फ़ंक्शन को डिप्लॉय करने पर, शेड्यूल करने का जॉब और एचटीटीपी फ़ंक्शन अपने-आप बन जाते हैं. Firebase CLI, फ़ंक्शन का नाम दिखाता है, साथ ही, Google Cloud Console में जॉब और फ़ंक्शन देखा जा सकता है. विषय का नाम, इस तरीके से रखा जाता है:

firebase-schedule-function_name-region

उदाहरण के लिए:

firebase-schedule-accountcleanup-us-east1.

शेड्यूल किए गए समय पर, Compute का डिफ़ॉल्ट सेवा खाता, उससे जुड़े एचटीटीपी फ़ंक्शन को लागू करता है. इसका मतलब है कि सिर्फ़ उससे जुड़े Cloud Scheduler जॉब के पास, फ़ंक्शन को चलाने की अनुमति होती है.