توابع زمانبندی (نسل اول)

اگر می‌خواهید توابع را برای اجرا در زمان‌های مشخص برنامه‌ریزی کنید، از هندلر onSchedule برای ایجاد یک موضوع Pub/Sub استفاده کنید که Cloud Scheduler برای ایجاد رویدادها در آن موضوع استفاده می‌کند.

قبل از اینکه شروع کنی

Though scheduled functions are billed, you can expect the overall cost to be manageable, as each Cloud Scheduler job costs $0.10 (USD) per month, and there is an allowance of three jobs per Google account, at no charge. Use the Blaze pricing calculator to generate a cost estimate based on your projected usage.

The Pub/Sub and Cloud Scheduler APIs must be enabled for your project. These should already be enabled for most Firebase projects; you can verify in the Google Cloud console .

نوشتن یک تابع زمان‌بندی‌شده

در Cloud Functions for Firebase ، منطق زمان‌بندی در کد توابع شما قرار دارد و هیچ الزام خاصی برای زمان استقرار وجود ندارد. برای ایجاد یک تابع زمان‌بندی‌شده، functions.pubsub.schedule('your schedule').onRun((context)) استفاده کنید. به عنوان مثال، برای اجرای یک تابع هر پنج دقیقه با سینتکس App Engine cron.yaml ، کاری شبیه به این انجام دهید:

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This will be run every 5 minutes!');
  return null;
});

هر دو سینتکس Unix Crontab و App Engine توسط Cloud Scheduler پشتیبانی می‌شوند. برای مثال، برای استفاده از Crontab جهت انتخاب یک منطقه زمانی خاص برای اجرای یک تابع زمان‌بندی شده، کاری شبیه به این انجام دهید:

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});

مقدار timeZone باید یک نام منطقه زمانی از پایگاه داده tz باشد. برای اطلاعات بیشتر در مورد ویژگی‌های پشتیبانی شده، به مرجع Cloud Scheduler مراجعه کنید.

یک تابع زمان‌بندی‌شده را مستقر کنید

وقتی یک تابع زمان‌بندی‌شده را مستقر می‌کنید، وظیفه زمان‌بندی مربوطه و موضوع انتشار/زیرموضوع به‌طور خودکار ایجاد می‌شوند. رابط خط Firebase نام موضوع را منعکس می‌کند و می‌توانید کار و موضوع را در کنسول گوگل کلود مشاهده کنید. موضوع طبق قرارداد زیر نامگذاری می‌شود:

function_name فایربیس - region

برای مثال:

تابع زمان‌بندی‌شده‌ی firebase در Crontab-us-east1.