تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
إذا أردت جدولة الدوال لتنفيذها في أوقات محدّدة، استخدِم معالج onSchedule الذي توفّره firebase-functions/v2/scheduler.
تستخدم هذه الدوالّ Cloud Scheduler
لتفعيل منطق الدالة في الأوقات أو الفواصل الزمنية التي تحدّدها.
قبل البدء
على الرغم من أنّ الفوترة مطلوبة، يمكنك توقّع أن تكون التكلفة الإجمالية معقولة، لأنّ تكلفة كل Cloud Schedulerمهمة تبلغ 0.10 دولار أمريكي شهريًا، ويُسمح بثلاث مهام لكل حساب على Google بدون أي رسوم. استخدِم حاسبة الأسعار في Blaze لإنشاء تقدير للتكلفة استنادًا إلى الاستخدام المتوقّع.
يجب تفعيل واجهة برمجة التطبيقات Cloud Scheduler لمشروعك. من المفترض أن تكون هذه الخدمة مفعّلة تلقائيًا لمعظم مشاريع 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.constadmin=require("firebase-admin");admin.initializeApp();// The es6-promise-pool to limit the concurrency of promises.constPromisePool=require("es6-promise-pool").default;// Maximum concurrent account deletions.constMAX_CONCURRENT=3;
# The Cloud Functions for Firebase SDK to set up triggers and logging.fromfirebase_functionsimportscheduler_fn# The Firebase Admin SDK to delete users.importfirebase_adminfromfirebase_adminimportauthfirebase_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/cloudschedulerexports.accountcleanup=onSchedule("every day 00:00",async(event)=>{// Fetch all user details.constinactiveUsers=awaitgetInactiveUsers();// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.constpromisePool=newPromisePool(()=>deleteInactiveUser(inactiveUsers),MAX_CONCURRENT,);awaitpromisePool.start();logger.log("User cleanup finished");});
# 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")defaccountcleanup(event:scheduler_fn.ScheduledEvent)-> None:"""Delete users who've been inactive for 30 days or more."""user_page:auth.ListUsersPage|None=auth.list_users()whileuser_pageisnotNone:inactive_uids=[user.uidforuserinuser_page.usersifis_inactive(user,timedelta(days=30))]auth.delete_users(inactive_uids)user_page=user_page.get_next_page()
عند نشر دالة مجدولة، يتم تلقائيًا إنشاء مهمة مجدولة ودالة HTTP. تعرض واجهة سطر الأوامر Firebase اسم الدالة، ويمكنك الاطّلاع على المهمة والدالة في وحدة تحكّم Google Cloud.
يتم تسمية الموضوع وفقًا للاتفاقية التالية:
firebase-schedule-function_name-region
على سبيل المثال:
firebase-schedule-accountcleanup-us-east1.
في الوقت المحدّد، يستدعي حساب خدمة Compute Engine التلقائي دالة HTTP المرتبطة. وهذا يعني أنّ مهمة Cloud Scheduler المرتبطة فقط هي التي لديها الإذن بتشغيل الدالة.
تاريخ التعديل الأخير: 2025-09-05 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-09-05 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["\u003cbr /\u003e\n\nIf you want to schedule functions to run at specified times, use\nthe `onSchedule` handler provided by `firebase-functions/v2/scheduler`.\nThese functions use [Cloud Scheduler](https://cloud.google.com/scheduler/)\nto invoke function logic at the times or intervals that you define.\n\nBefore you begin\n\nThough scheduled functions are billed, you can expect the overall\ncost to be manageable, as\neach Cloud Scheduler job costs $0.10 (USD) per month, and there is an\nallowance of three jobs per Google account, at no charge. Use the Blaze\n[pricing calculator](/pricing#blaze-calculator) to generate a cost estimate\nbased on your projected usage.\n\nThe Cloud Scheduler API must be enabled for your\nproject. It should already be enabled for most Firebase projects; you can\nverify in the [Google Cloud console](https://console.cloud.google.com/).\n\nWrite a scheduled function\n\nIn Cloud Functions for Firebase, scheduling logic resides in your functions code,\nwith no special deploy-time requirements.\nFor example, to clean up inactive user accounts once daily, you could write\na function starting with the following import statements: \n\nNode.js \n\n // The Cloud Functions for Firebase SDK to set up triggers and logging.\n const {onSchedule} = require(\"firebase-functions/v2/scheduler\");\n const {logger} = require(\"firebase-functions\");\n\n // The Firebase Admin SDK to delete inactive users.\n const admin = require(\"firebase-admin\");\n admin.initializeApp();\n\n // The es6-promise-pool to limit the concurrency of promises.\n const PromisePool = require(\"es6-promise-pool\").default;\n // Maximum concurrent account deletions.\n const MAX_CONCURRENT = 3; \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/delete-unused-accounts-cron/functions/index.js#L20-L31\n\nPython \n\n # The Cloud Functions for Firebase SDK to set up triggers and logging.\n from firebase_functions import scheduler_fn\n\n # The Firebase Admin SDK to delete users.\n import firebase_admin\n from firebase_admin import auth\n\n firebase_admin.initialize_app() \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/delete-unused-accounts-cron/functions/main.py#L19-L26\n\nThen, you could use `onSchedule` to start a Cloud Scheduler task: \n\nNode.js \n\n // Run once a day at midnight, to clean up the users\n // Manually run the task here https://console.cloud.google.com/cloudscheduler\n exports.accountcleanup = onSchedule(\"every day 00:00\", async (event) =\u003e {\n // Fetch all user details.\n const inactiveUsers = await getInactiveUsers();\n\n // Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.\n const promisePool = new PromisePool(\n () =\u003e deleteInactiveUser(inactiveUsers),\n MAX_CONCURRENT,\n );\n await promisePool.start();\n\n logger.log(\"User cleanup finished\");\n }); \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/delete-unused-accounts-cron/functions/index.js#L35-L49\n\nPython \n\n # Run once a day at midnight, to clean up inactive users.\n # Manually run the task here https://console.cloud.google.com/cloudscheduler\n @scheduler_fn.on_schedule(schedule=\"every day 00:00\")\n def accountcleanup(event: scheduler_fn.ScheduledEvent) -\u003e None:\n \"\"\"Delete users who've been inactive for 30 days or more.\"\"\"\n user_page: auth.ListUsersPage | None = auth.list_users()\n while user_page is not None:\n inactive_uids = [\n user.uid for user in user_page.users if is_inactive(user, timedelta(days=30))\n ]\n auth.delete_users(inactive_uids)\n user_page = user_page.get_next_page() \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/delete-unused-accounts-cron/functions/main.py#L31-L42\n\nBoth Unix Crontab and App Engine syntax\nare supported by Cloud Scheduler. For example, to use Crontab, do something like this: \n\nNode.js \n\n exports.scheduledFunctionCrontab = onSchedule(\"5 11 * * *\", async (event) =\u003e {\n // ...\n });\n\nPython \n\n @scheduler_fn.on_schedule(schedule=\"5 11 * * *\")\n\n| **Important:** Depending on how you design your scheduling logic, a function may be triggered multiple times, with the next instance running while the previous instance is still executing.\n\nDeploy a scheduled function\n\nWhen you deploy a scheduled function, a scheduler job and an HTTP function\nare created automatically. The Firebase CLI echoes the function name,\nand you can view the job and the function in the\n[Google Cloud console](https://console.cloud.google.com/project/_/cloudscheduler).\nThe topic is named according to the following convention:\n\n**firebase-schedule-\u003cvar translate=\"no\"\u003efunction_name\u003c/var\u003e-\u003cvar translate=\"no\"\u003eregion\u003c/var\u003e**\n\nFor example:\n\n**firebase-schedule-accountcleanup-us-east1.**\n\nAt the scheduled time, the default compute service account invokes the\nassociated HTTP function. This means that only the associated Cloud Scheduler\njob has permission to run the function.\n| **Important:** Make sure you do not manually delete or modify the function or scheduler job in the console. Doing this could cause errors in the execution of your scheduled function."]]