Cloud Tasks 큐에 App Engine 태스크 추가

이 빠른 시작에서는 Cloud Tasks API를 사용하여 Cloud Tasks 큐에 App Engine 태스크를 추가하는 방법을 설명합니다.

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  4. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Resource Manager and Cloud Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  8. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Install the Google Cloud CLI.

  10. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  11. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project.

  14. Enable the Cloud Resource Manager and Cloud Tasks API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  15. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  16. App Engine 기본 서비스 계정은 App Engine을 사용할 때 자동으로 생성됩니다. 이 빠른 시작을 사용해 볼 때 이 서비스 계정을 사용할 수 있습니다. 그러나 조직 정책 구성에 따라 프로젝트에 대한 Editor 역할이 기본 서비스 계정에 자동으로 부여되지 않을 수 있습니다. 이러한 경우 다음 역할을 서비스 계정에 부여해야 합니다.
    1. Artifact Registry 관리자(roles/artifactregistry.admin)
    2. Artifact Registry Create-on-Push 작성자(roles/artifactregistry.createOnPushWriter)
    3. Compute 관리자(roles/compute.admin)
    4. 로그 작성자(roles/logging.logWriter)
    5. 스토리지 객체 뷰어(roles/storage.objectViewer)
  17. App Engine 애플리케이션 추가

    App Engine 태스크를 타겟팅하는 경우 App Engine 표준 환경에 앱을 배포하기 전에 프로젝트에 App Engine 애플리케이션 추가해야 합니다.

    1. Google Cloud 콘솔에서 App Engine 페이지로 이동합니다.

      App Engine으로 이동

    2. App Engine 시작하기 대화상자에서 다음 중 하나를 수행합니다.

      • 이미 App Engine 애플리케이션을 만들었고 App Engine 애플리케이션 생성됨 메시지가 표시되면 이 섹션의 나머지 단계를 건너뛰고 샘플 설치 및 배포 섹션의 단계를 진행하면 됩니다.

        또는

      • 아직 App Engine 애플리케이션을 만들지 않은 경우 애플리케이션 만들기를 클릭하고 이 섹션의 나머지 단계를 계속 진행합니다.

    3. 애플리케이션의 리전을 선택하고 이를 기록해 둡니다.

      europe-westus-central은 Cloud Tasks 명령어에서 europe-west1us-central1로 각각 호출됩니다.

    4. 서비스 계정을 선택하지 마세요. 기본 App Engine 서비스 계정이 사용됩니다.

    5. 다음을 클릭합니다.

      애플리케이션이 구성되고 생성됩니다. 몇 분 정도 걸릴 수 있습니다.

    6. Cloud SDK를 다운로드하지 마세요. 나중에를 클릭합니다.

      App Engine 애플리케이션 생성됨 메시지가 표시됩니다.

    샘플 설치 및 배포

    이 빠른 시작에서 사용되는 Node.js 샘플은 두 개의 파일로 구성됩니다. createTask.js는 로컬에서 명령줄 도구로 실행되어 태스크를 만들고 태스크 대기열에 추가합니다. server.js는 App Engine에 작업자 서비스로 배포되어 태스크를 처리합니다.

    1. 터미널에서 샘플 애플리케이션 저장소를 로컬 머신에 클론합니다.

      git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
      
    2. 샘플 코드가 있는 디렉터리로 이동합니다.

      cd nodejs-docs-samples/cloud-tasks/snippets
      
    3. Node.js 패키지 관리자를 사용하여 모든 종속 항목을 설치합니다.

      NPM을 사용할 수 있습니다.

      npm install
      

      또는 Yarn을 사용할 수 있습니다.

      yarn install
      
    4. 작업자 서비스(server.js)를 App Engine 표준 환경에 배포합니다.

      gcloud app deploy app.yaml
      
    5. 서비스가 포함된 앱이 실행 중인지 확인합니다.

      gcloud app browse
      
    6. 브라우저에서 제공된 링크로 이동합니다. 예를 들면 다음과 같습니다.

      https://PROJECT_ID.uc.r.appspot.com/
      

      Hello, World!가 표시됩니다.

    Cloud Tasks 큐 만들기

    gcloud tasks queues create 명령어를 사용하여 준비한 환경에서 큐를 만듭니다.

    1. 터미널에서 모든 작업을 로깅하는 대기열을 만듭니다.

      gcloud tasks queues create QUEUE_NAME \
          --log-sampling-ratio=1.0 \
          --location=REGION
      

      다음을 바꿉니다.

      • QUEUE_NAME: Cloud Tasks 큐의 이름
      • REGION: 앱을 배포한 리전
    2. 대기열이 초기화될 때까지 기다린 다음 성공적으로 만들어졌는지 확인합니다.

      gcloud tasks queues describe QUEUE_NAME \
          --location=REGION
      

      출력은 다음과 비슷하게 표시됩니다.

       name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
       rateLimits:
         maxBurstSize: 100
         maxConcurrentDispatches: 1000
         maxDispatchesPerSecond: 500.0
       retryConfig:
         maxAttempts: 100
         maxBackoff: 3600s
         maxDoublings: 16
         minBackoff: 0.100s
       state: RUNNING
      

    Cloud Tasks 큐에 태스크 추가

    태스크를 만들고 만든 대기열에 추가한 다음 태스크를 작업자 서비스로 전달합니다.

    1. 다음 환경 변수를 설정합니다. 클라이언트는 이 정보를 사용하여 요청을 만듭니다.

      export PROJECT_ID=PROJECT_ID
      export LOCATION_ID=REGION
      export QUEUE_ID=QUEUE_NAME
      
    2. hello의 페이로드로 태스크를 만들어서 큐에 추가합니다. 페이로드는 작업자 서비스가 작업을 처리하는 데 필요한 요청의 모든 데이터가 될 수 있습니다.

      node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello
      
    3. 작업자 서비스 로그를 표시하여 태스크가 실행되었는지 확인합니다.

      gcloud app logs read
      

      로그는 다음과 비슷하게 표시됩니다.

      2024-06-20 15:00:00 default[20240620t143852]  "POST /log_payload HTTP/1.1" 200
      2024-06-20 15:00:00 default[20240620t143852]  App listening on port 8081
      2024-06-20 15:00:00 default[20240620t143852]  Press Ctrl+C to quit.
      2024-06-20 15:00:00 default[20240620t143852]  Received task with payload: hello
      

    삭제

    이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 Google Cloud 프로젝트를 삭제합니다.

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    또는 만든 리소스를 삭제할 수 있습니다.

    1. Cloud Tasks 큐 삭제:

      gcloud tasks queues delete QUEUE_NAME \
          --location=REGION
      
    2. App Engine 애플리케이션을 사용 중지합니다.

    다음 단계