หากต้องการกำหนดเวลาให้ฟังก์ชันทำงานในเวลาที่ระบุ ให้ใช้แฮนเดิลอร์ onSchedule
ที่ firebase-functions/v2/scheduler
จัดเตรียมไว้ให้
ฟังก์ชันเหล่านี้ใช้ Cloud Scheduler
เพื่อเรียกใช้ตรรกะของฟังก์ชันในเวลาหรือช่วงเวลาที่คุณกำหนด
ก่อนเริ่มต้น
หากต้องการใช้โซลูชันนี้ในโปรเจ็กต์ Firebase โปรเจ็กต์ของคุณต้องใช้ แพ็กเกจราคา Blaze หากยังไม่ได้ใช้แพ็กเกจ Blaze ให้อัปเกรดแพ็กเกจราคา
แม้ว่าคุณจะต้องเรียกเก็บเงิน แต่ค่าใช้จ่ายโดยรวมจะจัดการได้ เนื่องจากCloud Schedulerงานแต่ละรายการมีค่าใช้จ่าย $0.10 (USD) ต่อเดือน และมีโควต้างาน 3 รายการต่อบัญชี Google โดยไม่มีค่าใช้จ่าย ใช้เครื่องคำนวณราคาของ Blaze เพื่อสร้างค่าใช้จ่ายโดยประมาณ ตามการใช้งานที่คาดการณ์ไว้
ต้องเปิดใช้ Cloud Scheduler API สำหรับโปรเจ็กต์ ของคุณ โดยส่วนใหญ่แล้วระบบจะเปิดใช้ฟีเจอร์นี้สำหรับโปรเจ็กต์ 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/v2/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ไวยากรณ์ เช่น หากต้องการใช้ Crontab ให้ทำดังนี้
Node.js
exports.scheduledFunctionCrontab = onSchedule("5 11 * * *", async (event) => {
// ...
});
Python
@scheduler_fn.on_schedule(schedule="5 11 * * *")
ทำให้ฟังก์ชันที่กำหนดเวลาไว้ใช้งานได้
เมื่อคุณทําการติดตั้งใช้งานฟังก์ชันที่ตั้งเวลาไว้ ระบบจะสร้างงานของเครื่องจัดตารางเวลาและฟังก์ชัน HTTP โดยอัตโนมัติ Firebase CLI จะแสดงชื่อฟังก์ชัน และคุณสามารถดูงานและฟังก์ชันในคอนโซล Google Cloud ได้ หัวข้อจะมีชื่อตามรูปแบบต่อไปนี้
firebase-schedule-function_name-region
เช่น
firebase-schedule-accountcleanup-us-east1.
เมื่อถึงเวลาที่กำหนด บัญชีบริการเริ่มต้นของ Compute จะเรียกใช้ฟังก์ชัน HTTP ที่เชื่อมโยง ซึ่งหมายความว่าเฉพาะCloud Scheduler งานที่เชื่อมโยงเท่านั้นที่มีสิทธิ์เรียกใช้ฟังก์ชัน