Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menjelaskan cara membuat dan menjalankan tugas yang tidak dijadwalkan
hingga tugas tertentu berhasil atau gagal. Untuk mempelajari lebih lanjut status tugas, lihat Ringkasan pembuatan dan eksekusi tugas.
Jika Anda memiliki workload dengan persyaratan resource yang bervariasi, pertimbangkan untuk menggunakan
tugas dependen untuk membuat rangkaian tugas otomatis yang masing-masing menggunakan VM terpisah.
Misalnya, pisahkan jenis VM yang digunakan untuk operasi dengan permintaan rendah (seperti penyiapan data) dan operasi yang intensif komputasi (seperti pemrosesan data).
Dengan menggunakan tugas dependen untuk membantu mengoptimalkan konsumsi resource, Anda dapat mengurangi biaya dan penggunaan kuota.
Anda mungkin juga bisa mendapatkan
izin yang diperlukan melalui peran
khusus atau peran
bawaan lainnya.
Pembatasan
Tugas dependen memiliki batasan berikut:
Tugas dependen dapat memiliki hingga empat dependensi. Setiap dependensi harus berisi
nama tugas unik dan salah satu status wajib berikut:
SUCCEEDED: berhasil
FAILED: gagal
FINISHED: berhasil atau gagal
Saat Anda membuat tugas dependen, semua tugas dependensinya harus ada.
Tugas dependen tidak dapat memasuki status terjadwal (SCHEDULED) hingga setiap tugas dependensi memasuki status yang diperlukan. Jika tugas dependensi tidak dapat memasuki status yang diperlukan, tugas dependen akan langsung gagal tanpa dijadwalkan.
Seperti semua tugas, tugas dependen tidak dapat memasuki status terjadwal (SCHEDULED) jika tugas melebihi
waktu tunggu maksimum dalam antrean.
Batch menerapkan batas ini untuk mencegah tugas Anda menjadi tidak valid. Oleh karena itu, jika Anda memperkirakan waktu yang tersisa yang diperlukan untuk menyelesaikan tugas dependensi mungkin melebihi batas ini, pertimbangkan untuk melakukan salah satu atau beberapa hal berikut:
Jika Anda belum membuat tugas dependensi, coba kurangi perkiraan waktu
proses tugas dependensi. Misalnya, Anda dapat meningkatkan paralelisme
beban kerja atau membatasi waktu proses tugas
dan runnable yang tidak perlu berhasil.
Tunggu hingga waktu berjalan yang diharapkan dari tugas
dependensi lebih rendah dari batas sebelum membuat tugas dependen.
Membuat tugas dependen
Anda dapat membuat tugas yang
bersifat dependen menggunakan metode berikut:
Untuk menentukan bahwa tugas bersifat dependen, sertakan
kolom dependencies[].items,
yang mendukung satu atau beberapa dependensi yang ditentukan sebagai key-value pair, dalam
isi utama di file JSON:
DEPENDENCY_JOB_NAME: nama tugas dependensi,
yang harus mencapai status yang diperlukan sebelum tugas dependen ini diizinkan untuk
dijadwalkan.
REQUIRED_STATE: status yang diperlukan untuk
tugas dependensi yang sesuai, yang harus berupa SUCCEEDED, FAILED, atau
FINISHED.
Misalnya, tugas dependen dengan tiga dependensi dapat memiliki file konfigurasi JSON yang mirip dengan berikut ini:
{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello World! This is task $BATCH_TASK_INDEX."}}]},"taskCount":3}],"dependencies":[{"items":{"DEPENDENCY_JOB_NAME_1":"REQUIRED_STATE_1","DEPENDENCY_JOB_NAME_2":"REQUIRED_STATE_2","DEPENDENCY_JOB_NAME_3":"REQUIRED_STATE_3"}}]}
Langkah berikutnya
Jika Anda mengalami masalah saat membuat atau menjalankan tugas, lihat
Pemecahan masalah.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-12 UTC."],[[["\u003cp\u003eThis document explains how to create dependent jobs, which are jobs that begin only after specific prior jobs have either succeeded, failed, or finished.\u003c/p\u003e\n"],["\u003cp\u003eUsing dependent jobs helps optimize resource allocation by allowing different VM types to be used for different job phases, such as data preparation versus data processing.\u003c/p\u003e\n"],["\u003cp\u003eDependent jobs have restrictions, such as a maximum of four dependencies, and the required states for dependencies can be \u003ccode\u003eSUCCEEDED\u003c/code\u003e, \u003ccode\u003eFAILED\u003c/code\u003e, or \u003ccode\u003eFINISHED\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating a dependent job, all dependency jobs must already exist, and the dependent job won't schedule until each dependency reaches its designated state.\u003c/p\u003e\n"],["\u003cp\u003eYou can create a dependent job using the Google Cloud CLI's \u003ccode\u003egcloud alpha batch jobs submit\u003c/code\u003e command or the Batch API's \u003ccode\u003ev1alpha jobs.create\u003c/code\u003e method, specifying dependencies in the \u003ccode\u003edependencies[].items\u003c/code\u003e field.\u003c/p\u003e\n"]]],[],null,["# Schedule dependent jobs\n\nThis document describes how to create and run a job that isn't scheduled\nuntil specific jobs have succeeded or failed. To learn more about job states,\nsee [Job creation and execution overview](/batch/docs/create-run-job).\n\nIf you have a workload with varying resource requirements, consider using\ndependent jobs to create an automated chain of jobs that each use separate VMs.\nFor example, separate the types of VMs used for low-demand operations\n(like data preparation) and compute-intensive operations (like data processing).\nBy using dependent jobs to help optimize resource consumption, you can reduce\ncosts and quota usage.\n\nBefore you begin\n----------------\n\n1. If you haven't used Batch before, review [Get started with Batch](/batch/docs/get-started) and enable Batch by completing the [prerequisites for projects and users](/batch/docs/get-started#prerequisites).\n2.\n\n To get the permissions that\n you need to create a job,\n\n ask your administrator to grant you the\n following IAM roles:\n\n - [Batch Job Editor](/iam/docs/roles-permissions/batch#batch.jobsEditor) (`roles/batch.jobsEditor`) on the project\n - [Service Account User](/iam/docs/roles-permissions/iam#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the [job's service account](/batch/docs/create-run-job-custom-service-account#create-job-service-account), which by default is the [default Compute Engine service account](/compute/docs/access/service-accounts#default_service_account)\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\n\u003cbr /\u003e\n\n### Restrictions\n\nDependent jobs have the following restrictions:\n\n- A dependent job can have up to four dependencies. Each dependency must contain\n a unique job name and one of the following required states:\n\n - `SUCCEEDED`: succeeded\n - `FAILED`: failed\n - `FINISHED`: succeeded or failed\n- When you create a dependent job, all of its dependency jobs must exist.\n\n- A dependent job can't enter the scheduled (`SCHEDULED`) state until each\n dependency job has entered its required state. If it becomes impossible for a\n dependency job to enter its required state, then the dependent job immediately\n fails without being scheduled.\n\n | **Note:** You don't need to wait to delete finished dependency jobs. A dependent job automatically remembers if a deleted dependency job reached its required state.\n- Like all jobs, a dependent job can't enter the scheduled (`SCHEDULED`) state\n if the job exceeds the\n [maximum wait time in the queue](/batch/quotas#max-job-queue).\n Batch enforces this limit to prevent your jobs from becoming\n stale. Consequently, if you expect the remaining time required for any of the\n dependency jobs to finish might exceed this limit, then consider doing one\n or more of the following:\n\n - If you haven't created a dependency job yet, try to reduce the expected run time of the dependency job. For example, you might increase the parallelism of the workload or [limit the run time](/batch/docs/set-timeouts) of any tasks and runnables that you don't need to succeed.\n - Wait to create the dependent job until the expected run time of the dependency jobs is lower than the limit.\n\n### Create a dependent job\n\nYou can [create a job](/batch/docs/create-run-basic-job#create-basic-job) that\nis dependent using the following methods:\n\n- For the Google Cloud CLI, use the [`gcloud alpha batch jobs submit` command](/sdk/gcloud/reference/alpha/batch/jobs/submit).\n- For the Batch API, use the [v1alpha `jobs.create` method](/batch/docs/reference/rest/v1alpha/projects.locations.jobs/create).\n\nTo specify that a job is dependent, include the\n[`dependencies[].items` field](/batch/docs/reference/rest/v1alpha/projects.locations.jobs#Job.FIELDS.dependencies),\nwhich supports one or more dependencies specified as key-value pairs, in the\nmain body in the JSON file: \n\n \"dependencies\": [\n {\n \"items\": {\n \"\u003cvar translate=\"no\"\u003eDEPENDENCY_JOB_NAME\u003c/var\u003e\": \"\u003cvar translate=\"no\"\u003eREQUIRED_STATE\u003c/var\u003e\"\n }\n }\n ]\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eDEPENDENCY_JOB_NAME\u003c/var\u003e: the name of a dependency job,\n which must reach its required state before this dependent job is allowed to\n be scheduled.\n\n- \u003cvar translate=\"no\"\u003eREQUIRED_STATE\u003c/var\u003e: the required state for the\n corresponding dependency job, which must be `SUCCEEDED`, `FAILED`, or\n `FINISHED`.\n\nFor example, a dependent job with three dependencies can have a JSON\nconfiguration file that is similar to the following: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n \"script\": {\n \"text\": \"echo Hello World! This is task $BATCH_TASK_INDEX.\"\n }\n }\n ]\n },\n \"taskCount\": 3\n }\n ],\n \"dependencies\": [\n {\n \"items\": {\n \"\u003cvar translate=\"no\"\u003eDEPENDENCY_JOB_NAME_1\u003c/var\u003e\": \"\u003cvar translate=\"no\"\u003eREQUIRED_STATE_1\u003c/var\u003e\",\n \"\u003cvar translate=\"no\"\u003eDEPENDENCY_JOB_NAME_2\u003c/var\u003e\": \"\u003cvar translate=\"no\"\u003eREQUIRED_STATE_2\u003c/var\u003e\",\n \"\u003cvar translate=\"no\"\u003eDEPENDENCY_JOB_NAME_3\u003c/var\u003e\": \"\u003cvar translate=\"no\"\u003eREQUIRED_STATE_3\u003c/var\u003e\"\n }\n }\n ]\n }\n\nWhat's next\n-----------\n\n- If you have issues creating or running a job, see [Troubleshooting](/batch/docs/troubleshooting).\n- Learn how to [view jobs and tasks](/batch/docs/view-jobs-tasks).\n- Learn about more [job creation options](/batch/docs/create-run-job#job-creation-options)."]]