이 문서에서는 제한 시간을 설정하여 태스크 및 실행 가능 항목의 실행 시간을 제한하는 방법을 설명합니다.
제한 시간은 태스크 또는 실행 가능 항목을 실행할 수 있는 시간을 지정합니다.
Batch에서는 작업을 14일 이상 실행하는 것을 허용하지 않으며 개별 태스크 및 실행 가능 항목의 기본 제한 시간을 설정하지 않습니다. 따라서 개별 태스크 또는 실행 가능 항목이 자동 실패 전까지 최대 14일 동안 실행될 수 있습니다. 하지만 태스크와 실행 가능 항목이 이렇게 오랫동안 실행되지 않을 경우 이 구성으로 인해 예상치 못한 비용과 지연이 발생할 수 있습니다. 과도한 실행 시간을 방지하기 위해 태스크 및 실행 가능 항목에 제한 시간을 설정할 수 있습니다.
실행 가능 항목, 태스크 또는 둘 다에 제한 시간을 설정할 수 있습니다. 실행 가능 항목의 제한 시간은 해당 실행 가능 항목의 최대 실행 시간을 지정합니다.
태스크의 제한 시간은 해당 태스크의 최대 실행 시간을 지정하며, 이는 실행 가능 항목의 모든 개별 실행 시간의 합계입니다.
예를 들어 태스크에 1분 동안 동시에 실행되는 3개의 실행 가능 항목이 있는 경우 태스크 실행 시간은 1분이 아니라 3분입니다.
실행 가능 항목과 실행 가능 항목의 태스크 모두에 대한 제한 시간과 같이 겹치는 제한 시간을 설정한 경우 자동 실패를 트리거하려면 제한 시간을 하나만 초과하면 됩니다. 예를 들어 태스크 제한 시간을 60초로 설정하고 해당 태스크의 각 실행 가능 항목 제한 시간을 120초로 설정했다고 가정해 보겠습니다. 그런 다음 실행 가능 항목의 실행 시간 합계가 60초를 초과하면 이 예시 태스크와 모든 실행 가능 항목이 실패하고 120초 제한 시간을 트리거할 수 없습니다.
작업의 태스크와 실행 가능 항목에 설정할 적절한 제한 시간을 선택하려면 이전에 실행한 유사한 작업의 로그를 분석하여 유사한 워크로드에 대한 태스크와 실행 가능 항목의 일반적인 실행 시간을 확인합니다.
TIMEOUT을 태스크 실행을 허용할 최대 정수 또는 소수 값(초)으로 바꿉니다. 예를 들면 255s입니다.
태스크에 대해 255초 제한 시간을 설정하는 작업에는 다음과 유사한 JSON 구성 파일이 포함됩니다.
{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."}}],"maxRunDuration":"255s"},"taskCount":3}],"logsPolicy":{"destination":"CLOUD_LOGGING"}}
태스크의 제한 시간이 초과되면 태스크가 자동으로 실패하고 초과된 제한 시간은 작업의 상태 이벤트 및 로그에 종료 코드 50005로 표시됩니다. 초과된 제한 시간에 대한 자세한 내용은 종료 코드 50005 문제 해결 문서를 참조하세요.
실행 가능 항목 제한 시간 설정
Google Cloud CLI 또는 REST API를 사용하여 JSON 파일의 runnable 객체에서 timeout 필드를 포함하는 작업을 만듭니다.
TIMEOUT을 실행 가능 항목 실행을 허용할 최대 정수 또는 소수 값(초)으로 바꿉니다. 예를 들면 3.5s입니다.
실행 가능 항목에 대해 3.5초 제한 시간을 설정하는 작업에는 다음과 유사한 JSON 구성 파일이 포함됩니다.
{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."},"timeout":"3.5s"}]},"taskCount":3}],"logsPolicy":{"destination":"CLOUD_LOGGING"}}
실행 가능 항목의 제한 시간이 초과되면 실행 가능 항목이 자동으로 실패하고 초과된 제한 시간은 작업의 상태 이벤트 및 로그에 종료 코드 50005로 표시됩니다. 초과된 제한 시간에 대한 자세한 내용은 종료 코드 50005 문제 해결 문서를 참조하세요.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-02-11(UTC)"],[[["\u003cp\u003eTimeouts can be set for both tasks and runnables to limit their maximum execution time, preventing potential cost overruns and delays.\u003c/p\u003e\n"],["\u003cp\u003eBatch does not set default timeouts for individual tasks and runnables, so if not configured, they can run for up to 14 days before automatic failure.\u003c/p\u003e\n"],["\u003cp\u003eSetting a task timeout limits the total combined run time of all its runnables, while a runnable timeout limits the execution time of that specific runnable.\u003c/p\u003e\n"],["\u003cp\u003eOverlapping timeouts for tasks and runnables will cause a failure when either limit is exceeded, and exceeding a timeout results in exit code \u003ccode\u003e50005\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo determine appropriate timeouts, it's recommended to analyze the logs of similar previous jobs to understand the typical run times of tasks and runnables.\u003c/p\u003e\n"]]],[],null,["# Limit run times for tasks and runnables using timeouts\n\nThis document describes how to limit the run times of tasks and runnables by\nsetting timeouts. Alternatively, if you want a runnable to finish as soon as\nevery other runnable in its task has finished running, use a\n[background runnable](/batch/docs/reference/rest/v1/projects.locations.jobs#Runnable.FIELDS.background)\ninstead.\n\nA *timeout* specifies the amount of time that a task or runnable is permitted\nto run.\n[Batch doesn't allow jobs to run for longer than 14 days](/batch/quotas#max-job-duration)\nand doesn't set default timeouts for individual tasks and\nrunnables. Consequently, an individual task or runnable can run for as long as\n14 days before automatic failure. But, if your tasks and runnables aren't\nintended to run for that long, this configuration might cause unexpected costs\nand delays. To prevent excessive run times, you can set timeouts for tasks and\nrunnables.\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\nSet timeouts\n------------\n\nYou can set timeouts for runnables, tasks, or both. The timeout for a\nrunnable specifies the maximum run time for that runnable.\nThe timeout for a task specifies the maximum run time for that task,\nwhich is the sum of all the individual run times of its runnables.\nFor example, if a task has 3 runnables that all run at the same time for 1\nminute, then the task's run time is 3 minutes, *not* 1 minute.\n\nIf you set overlapping timeouts---such as a timeout for both a runnable and\nthe runnable's task---then only one timeout needs to be exceeded to trigger\nautomatic failure. For example, suppose you set a task's timeout to\n60 seconds and the timeout of each of that task's runnables to\n120 seconds. Then, this example task and all of its runnables fail when the\nsum of the run times of its runnables exceeds 60 seconds, and it's\nimpossible to trigger the 120-second timeouts.\n\nTo choose the appropriate timeout to set for your job's tasks and runnables,\n[analyze the logs](/batch/docs/analyze-job-using-logs) of similar jobs that you\nhave previously run to determine the typical run time for the tasks and\nrunnables for similar workloads.\n| **Important:** Due to a known issue, the logs generated by Batch for exceeded timeout don't indicate whether the task's timeout or the runnable's timeout was exceeded. For a workaround that explains how to identify which timeout was exceeded, see the [known issue](/batch/docs/known-issues#runnable-timeout).\n\n### Set timeout for a task\n\nUse the Google Cloud CLI or REST API to\n[create a job](/batch/docs/create-run-basic-job#create-basic-job) that\nincludes the\n[`maxRunDuration` field](/batch/docs/reference/rest/v1/projects.locations.jobs#taskspec.FIELDS.maxRunDuration)\nin the `taskSpec` object of the JSON file: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n ...\n \"maxRunDuration\": \"\u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e\"\n }\n }\n ]\n }\n\nReplace \u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e with the maximum number of seconds or\nfractional sections you want to permit the task to run for. For example, `255s`.\n\nA job that sets a 255 second timeout for a task would have a JSON configuration\nfile 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}. This job has a total of ${BATCH_TASK_COUNT} tasks.\"\n }\n }\n ],\n \"maxRunDuration\": \"255s\"\n },\n \"taskCount\": 3\n }\n ],\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\nIf the timeout for a task is exceeded, the task automatically fails and\nthe exceeded timeout is indicated by exit code `50005` in the job's\nstatus events and logs. For more information about exceeded timeouts, see the\n[troubleshooting documentation for exit code 50005](/batch/docs/troubleshooting#task_runs_over_the_maximum_runtime_50005).\n\n### Set timeout for a runnable\n\nUse the Google Cloud CLI or REST API to\n[create a job](/batch/docs/create-run-basic-job#create-basic-job) that\nincludes the\n[`timeout` field](/batch/docs/reference/rest/v1/projects.locations.jobs#runnable.FIELDS.timeout)\nin the `runnable` object of the JSON file: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n ...\n \"timeout\": \"\u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e\"\n }\n ]\n }\n }\n ]\n }\n\nReplace \u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e with the maximum number of seconds or\nfractional sections you want to permit the runnable to run for. For example,\n`3.5s`.\n\nA job that sets a 3.5 second timeout for a runnable would have a JSON\nconfiguration file 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}. This job has a total of ${BATCH_TASK_COUNT} tasks.\"\n },\n \"timeout\": \"3.5s\"\n }\n ]\n },\n \"taskCount\": 3\n }\n ],\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\nIf the timeout for a runnable is exceeded, the runnable automatically fails and\nthe exceeded timeout is indicated by exit code `50005` in the job's\nstatus events and logs. For more information about exceeded timeouts, see the\n[troubleshooting documentation for exit code 50005](/batch/docs/troubleshooting#task_runs_over_the_maximum_runtime_50005).\n\nWhat's next\n-----------\n\n- If you have issues creating or running a job, see [Troubleshooting](/batch/docs/troubleshooting).\n- [View jobs and tasks](/batch/docs/view-jobs-tasks).\n- Learn about more [job creation options](/batch/docs/create-run-job#job-creation-options).\n- Learn how to [analyze a job using logs](/batch/docs/analyze-job-using-logs)."]]