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