หากต้องการกำหนดเวลาให้ฟังก์ชันทำงานในเวลาที่ระบุ ให้ใช้ตัวแฮนเดิลonScheduleที่firebase-functions/v2/schedulerจัดเตรียมไว้ให้
ฟังก์ชันเหล่านี้ใช้ Cloud Scheduler
เพื่อเรียกใช้ตรรกะของฟังก์ชันในเวลาหรือช่วงเวลาที่คุณกำหนด
ก่อนเริ่มต้น
แม้ว่าระบบจะเรียกเก็บเงินสำหรับฟังก์ชันที่กำหนดเวลาไว้ แต่คุณก็คาดการณ์ได้ว่าค่าใช้จ่ายโดยรวมจะจัดการได้ เนื่องจาก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/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 * * *")
ติดตั้งใช้งานฟังก์ชันที่กำหนดเวลาไว้
เมื่อคุณติดตั้งใช้งานฟังก์ชันที่กำหนดเวลาไว้ ระบบจะสร้างงานตัวกำหนดเวลาและฟังก์ชัน HTTP โดยอัตโนมัติ Firebase CLI จะแสดงชื่อฟังก์ชัน และคุณดูงานและฟังก์ชันได้ใน Google Cloud คอนโซล หัวข้อจะมีชื่อตามแบบแผนต่อไปนี้
firebase-schedule-function_name-region
เช่น
firebase-schedule-accountcleanup-us-east1.
เมื่อถึงเวลาที่กำหนด บัญชีบริการเริ่มต้นของ Compute จะเรียกใช้ฟังก์ชัน HTTP ที่เชื่อมโยง ซึ่งหมายความว่าเฉพาะงาน Cloud Scheduler ที่เชื่อมโยงเท่านั้นที่มีสิทธิ์เรียกใช้ฟังก์ชัน