Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo documento descrive come creare ed eseguire un job non pianificato
fino a quando non vengono completati o non riescono job specifici. Per scoprire di più sugli stati dei job,
vedi Panoramica della creazione e dell'esecuzione dei job.
Se hai un workload con requisiti di risorse variabili, valuta la possibilità di utilizzare
job dipendenti per creare una catena automatizzata di job che utilizzano VM separate.
Ad esempio, separa i tipi di VM utilizzati per operazioni a bassa domanda (come la preparazione dei dati) e operazioni che richiedono molte risorse di calcolo (come l'elaborazione dei dati).
Utilizzando i job dipendenti per ottimizzare il consumo di risorse, puoi ridurre
i costi e l'utilizzo della quota.
I job dipendenti presentano le seguenti limitazioni:
Un job dipendente può avere fino a quattro dipendenze. Ogni dipendenza deve contenere
un nome del job univoco e uno dei seguenti stati obbligatori:
SUCCEEDED: riuscito
FAILED: non riuscito
FINISHED: riuscita o non riuscita
Quando crei un job dipendente, tutti i relativi job di dipendenza devono esistere.
Un job dipendente non può passare allo stato Pianificato (SCHEDULED) finché ogni job dipendente non ha raggiunto lo stato richiesto. Se un job di dipendenza non può entrare nello stato richiesto, il job dipendente ha esito negativo immediatamente senza essere pianificato.
Come tutti i job, un job dipendente non può entrare nello stato pianificato (SCHEDULED)
se supera il
tempo di attesa massimo nella coda.
Batch applica questo limite per evitare che i job diventino
obsoleti. Di conseguenza, se prevedi che il tempo rimanente necessario per il completamento di uno dei job di dipendenza potrebbe superare questo limite, valuta la possibilità di eseguire una o più delle seguenti operazioni:
Se non hai ancora creato un job di dipendenza, prova a ridurre il tempo di esecuzione previsto del job di dipendenza. Ad esempio, puoi aumentare il parallelismo
del carico di lavoro o limitare il tempo di esecuzione di qualsiasi
attività e runnable che non devono essere completati.
Attendi di creare il job dipendente finché il tempo di esecuzione previsto dei job dipendenti non sia inferiore al limite.
Creare un job dipendente
Puoi creare un job dipendente utilizzando i seguenti metodi:
Per specificare che un job è dipendente, includi il
campo dependencies[].items,
che supporta una o più dipendenze specificate come coppie chiave-valore, nel
corpo principale del file JSON:
DEPENDENCY_JOB_NAME: il nome di un job di dipendenza, che deve raggiungere lo stato richiesto prima che sia consentita la pianificazione di questo job dipendente.
REQUIRED_STATE: lo stato richiesto per il
job di dipendenza corrispondente, che deve essere SUCCEEDED, FAILED o
FINISHED.
Ad esempio, un job dipendente con tre dipendenze può avere un file di configurazione JSON simile al seguente:
{"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"}}]}
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-05 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)."]]