İşlevleri belirli zamanlarda çalışacak şekilde planlamak istiyorsanız firebase-functions/v2/scheduler tarafından sağlanan onSchedule işleyicisini kullanın.
Bu işlevler, işlev mantığını tanımladığınız zamanlarda veya aralıklarda çağırmak için Cloud Scheduler kullanır.
Başlamadan önce
Programlanmış işlevler faturalandırılsa da her Cloud Scheduler işi ayda 0,10 ABD doları tuttuğu ve Google Hesabı başına üç iş ücretsiz olduğu için toplam maliyetin yönetilebilir olmasını bekleyebilirsiniz. Tahmini kullanımınıza göre maliyet tahmini oluşturmak için Blaze Fiyat Hesaplayıcıyı kullanın.
Projeniz için Cloud Scheduler API'sinin etkinleştirilmesi gerekir. Bu API, çoğu Firebase projesinde zaten etkindir. Google Cloud konsolunda doğrulayabilirsiniz.
Programlanmış işlev yazma
Cloud Functions for Firebase'da, planlama mantığı işlev kodunuzda bulunur ve özel dağıtım süresi gereksinimleri yoktur. Örneğin, etkin olmayan kullanıcı hesaplarını günde bir kez temizlemek için aşağıdaki içe aktarma ifadeleriyle başlayan bir işlev yazabilirsiniz:
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()
Ardından, onSchedule kullanarak Cloud Scheduler görevini başlatabilirsiniz:
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, hem Unix Crontab hem de App Engine söz dizimini destekler. Örneğin, Crontab'i kullanmak için aşağıdaki gibi bir işlem yapın:
Node.js
exports.scheduledFunctionCrontab = onSchedule("5 11 * * *", async (event) => {
// ...
});
Python
@scheduler_fn.on_schedule(schedule="5 11 * * *")
Programlanmış bir işlevi dağıtma
Zamanlanmış bir işlev dağıttığınızda otomatik olarak bir zamanlayıcı işi ve HTTP işlevi oluşturulur. Firebase CLI, işlev adını tekrarlar. İşi ve işlevi Google Cloud konsolunda görüntüleyebilirsiniz. Konu, aşağıdaki kurala göre adlandırılır:
firebase-schedule-function_name-region
Örneğin:
firebase-schedule-accountcleanup-us-east1.
Planlanan zamanda, varsayılan Compute hizmet hesabı ilişkili HTTP işlevini çağırır. Bu, yalnızca ilişkili Cloud Scheduler işinin işlevi çalıştırma izni olduğu anlamına gelir.