

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 從佇列環境執行背景協助程式程序
<a name="run-a-background-daemon-process"></a>

 在許多轉譯使用案例中，載入應用程式和場景資料可能需要相當長的時間。如果任務為每個影格重新載入它們，它會花費大部分時間在額外負荷上。通常可以將應用程式載入一次做為背景協助程式程序，讓它載入場景資料，然後透過程序間通訊 (IPC) 傳送命令來執行轉譯。

 許多開放原始碼截止日期雲端整合都使用此模式。開放任務描述專案在所有支援的作業系統上，提供具有強大 IPC 模式的[轉接器執行期程式庫](https://github.com/OpenJobDescription/openjd-adaptor-runtime-for-python)。

 為了示範此模式，有一個[獨立的範例任務套件](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/job_env_daemon_process/template.yaml)，它使用 Python 和 bash 程式碼來實作背景協助程式和 IPC，以便任務與其通訊。協助程式是在 Python 中實作，並監聽 POSIX SIGUSR1 訊號，了解何時處理任務。任務詳細資訊會傳遞至特定 JSON 檔案中的協助程式，而執行任務的結果會傳回為另一個 JSON 檔案。

## 先決條件
<a name="daemon-prerequisites"></a>

 執行下列步驟，從 Deadline Cloud [範例 github 儲存庫使用協助程式程序執行範例任務套件](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/job_env_daemon_process/template.yaml)。

1.  如果您沒有具有佇列和相關聯 Linux 機群的截止日期雲端陣列，請遵循[截止日期雲端主控台](https://console.aws.amazon.com/deadlinecloud/home)中的引導式加入體驗，以使用預設設定建立。

1.  如果您的工作站上沒有截止日期雲端 CLI 和截止日期雲端監視器，請遵循使用者指南中[設定截止日期雲端提交者](https://docs.aws.amazon.com/deadline-cloud/latest/userguide/submitter.html)中的步驟。

1.  使用 `git`複製[截止日期雲端範例 GitHub 儲存庫](https://github.com/aws-deadline/deadline-cloud-samples)。

   ```
   git clone https://github.com/aws-deadline/deadline-cloud-samples.git
    Cloning into 'deadline-cloud-samples'...
    ...
   cd deadline-cloud-samples/job_bundles
   ```

## 執行協助程式範例
<a name="daemon-run-sample"></a>

1.  使用截止日期雲端 CLI 提交`job_env_daemon_process`範例。

   ```
    git clone https://github.com/aws-deadline/deadline-cloud-samples.git
   Cloning into 'deadline-cloud-samples'...
    ...
   cd deadline-cloud-samples/job_bundles
   ```

1.  在截止日期雲端監控應用程式中，您會看到新的任務，並且可以監控其進度。一旦與佇列相關聯的Linux機群有工作者可執行任務，大約會在一分鐘內完成。選取其中一個任務後，選擇任務面板右上角選單中的**檢視日誌**選項。

    右側有兩個工作階段動作：**啟動 DaemonProcess** 和**任務執行**。視窗中央的日誌檢視器對應至右側的所選工作階段動作。

    選取**檢視所有任務的日誌**選項。時間軸會顯示在工作階段中執行的其餘任務，以及結束環境`Shut down DaemonProcess`的動作。

## 檢視協助程式日誌
<a name="daemon-view-logs"></a>

1. 在本節中，您會使用截止日期雲端監視器來比較工作階段動作與任務範本中定義工作階段動作的位置。它從上一節繼續。

    在文字編輯器中開啟檔案 [job\_env\_daemon\_process/template.yaml。](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/job_env_daemon_process/template.yaml)將工作階段動作與任務範本中定義它們的位置進行比較。

1.  在截止日期雲端監視器中選取`Launch DaemonProcess`工作階段動作。您將看到如下所示的日誌輸出。

   ```
    2024/07/17 16:27:20-07:00
    2024/07/17 16:27:20-07:00 ==============================================
    2024/07/17 16:27:20-07:00 --------- Entering Environment: DaemonProcess
    2024/07/17 16:27:20-07:00 ==============================================
    2024/07/17 16:27:20-07:00 ----------------------------------------------
    2024/07/17 16:27:20-07:00 Phase: Setup
    2024/07/17 16:27:20-07:00 ----------------------------------------------
    2024/07/17 16:27:20-07:00 Writing embedded files for Environment to disk.
    2024/07/17 16:27:20-07:00 Mapping: Env.File.Enter -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/enter-daemon-process-env.sh
    2024/07/17 16:27:20-07:00 Mapping: Env.File.Exit -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/exit-daemon-process-env.sh
    2024/07/17 16:27:20-07:00 Mapping: Env.File.DaemonScript -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/daemon-script.py
    2024/07/17 16:27:20-07:00 Mapping: Env.File.DaemonHelperFunctions -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/daemon-helper-functions.sh
    2024/07/17 16:27:20-07:00 Wrote: Enter -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/enter-daemon-process-env.sh
    2024/07/17 16:27:20-07:00 Wrote: Exit -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/exit-daemon-process-env.sh
    2024/07/17 16:27:20-07:00 Wrote: DaemonScript -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/daemon-script.py
    2024/07/17 16:27:20-07:00 Wrote: DaemonHelperFunctions -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/daemon-helper-functions.sh
    2024/07/17 16:27:20-07:00 ----------------------------------------------
    2024/07/17 16:27:20-07:00 Phase: Running action
    2024/07/17 16:27:20-07:00 ----------------------------------------------
    2024/07/17 16:27:20-07:00 Running command sudo -u job-user -i setsid -w /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/tmp_u8slys3.sh
    2024/07/17 16:27:20-07:00 Command started as pid: 2187
    2024/07/17 16:27:20-07:00 Output:
    2024/07/17 16:27:21-07:00 openjd_env: DAEMON_LOG=/sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/daemon.log
    2024/07/17 16:27:21-07:00 openjd_env: DAEMON_PID=2223
    2024/07/17 16:27:21-07:00 openjd_env: DAEMON_BASH_HELPER_SCRIPT=/sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/daemon-helper-functions.sh
   ```

    任務範本的下列幾行指定了此動作。

   ```
      stepEnvironments:
      - name: DaemonProcess
        description: Runs a daemon process for the step's tasks to share.
        script:
          actions:
            onEnter:
              command: bash
              args:
              - "{{Env.File.Enter}}"
            onExit:
              command: bash
              args:
              - "{{Env.File.Exit}}"
          embeddedFiles:
          - name: Enter
            filename: enter-daemon-process-env.sh
            type: TEXT
            data: |
              #!/bin/env bash
              set -euo pipefail
   
              DAEMON_LOG='{{Session.WorkingDirectory}}/daemon.log'
              echo "openjd_env: DAEMON_LOG=$DAEMON_LOG"
              nohup python {{Env.File.DaemonScript}} > $DAEMON_LOG 2>&1 &
              echo "openjd_env: DAEMON_PID=$!"
              echo "openjd_env: DAEMON_BASH_HELPER_SCRIPT={{Env.File.DaemonHelperFunctions}}"
   
              echo 0 > 'daemon_log_cursor.txt'
        ...
   ```

1.  在截止日期雲端監視器中選取其中一個任務執行：N 個工作階段動作。您將看到如下所示的日誌輸出。

   ```
   2024/07/17 16:27:22-07:00
    2024/07/17 16:27:22-07:00 ==============================================
    2024/07/17 16:27:22-07:00 --------- Running Task
    2024/07/17 16:27:22-07:00 ==============================================
    2024/07/17 16:27:22-07:00 Parameter values:
    2024/07/17 16:27:22-07:00 Frame(INT) = 2
    2024/07/17 16:27:22-07:00 ----------------------------------------------
    2024/07/17 16:27:22-07:00 Phase: Setup
    2024/07/17 16:27:22-07:00 ----------------------------------------------
    2024/07/17 16:27:22-07:00 Writing embedded files for Task to disk.
    2024/07/17 16:27:22-07:00 Mapping: Task.File.Run -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/run-task.sh
    2024/07/17 16:27:22-07:00 Wrote: Run -> /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/embedded_fileswy00x5ra/run-task.sh
    2024/07/17 16:27:22-07:00 ----------------------------------------------
    2024/07/17 16:27:22-07:00 Phase: Running action
    2024/07/17 16:27:22-07:00 ----------------------------------------------
    2024/07/17 16:27:22-07:00 Running command sudo -u job-user -i setsid -w /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/tmpv4obfkhn.sh
    2024/07/17 16:27:22-07:00 Command started as pid: 2301
    2024/07/17 16:27:22-07:00 Output:
    2024/07/17 16:27:23-07:00 Daemon PID is 2223
    2024/07/17 16:27:23-07:00 Daemon log file is /sessions/session-972e21d98dde45e59c7153bd9258a64dohwg4yg1/daemon.log
    2024/07/17 16:27:23-07:00
    2024/07/17 16:27:23-07:00 === Previous output from daemon
    2024/07/17 16:27:23-07:00 ===
    2024/07/17 16:27:23-07:00
    2024/07/17 16:27:23-07:00 Sending command to daemon
    2024/07/17 16:27:23-07:00 Received task result:
    2024/07/17 16:27:23-07:00 {
    2024/07/17 16:27:23-07:00   "result": "SUCCESS",
    2024/07/17 16:27:23-07:00   "processedTaskCount": 1,
    2024/07/17 16:27:23-07:00   "randomValue": 0.2578537967668988,
    2024/07/17 16:27:23-07:00   "failureRate": 0.1
    2024/07/17 16:27:23-07:00 }
    2024/07/17 16:27:23-07:00
    2024/07/17 16:27:23-07:00 === Daemon log from running the task
    2024/07/17 16:27:23-07:00 Loading the task details file
    2024/07/17 16:27:23-07:00 Received task details:
    2024/07/17 16:27:23-07:00 {
    2024/07/17 16:27:23-07:00  "pid": 2329,
    2024/07/17 16:27:23-07:00  "frame": 2
    2024/07/17 16:27:23-07:00 }
    2024/07/17 16:27:23-07:00 Processing frame number 2
    2024/07/17 16:27:23-07:00 Writing result
    2024/07/17 16:27:23-07:00 Waiting until a USR1 signal is sent...
    2024/07/17 16:27:23-07:00 ===
    2024/07/17 16:27:23-07:00
    2024/07/17 16:27:23-07:00 ----------------------------------------------
    2024/07/17 16:27:23-07:00 Uploading output files to Job Attachments
    2024/07/17 16:27:23-07:00 ----------------------------------------------
   ```

    任務範本中的下列幾行指定了此動作。``` 步驟：

   ```
    steps:
    - name: EnvWithDaemonProcess
      parameterSpace:
        taskParameterDefinitions:
        - name: Frame
          type: INT
          range: "{{Param.Frames}}"
   
      stepEnvironments:
        ...
   
      script:
        actions:
          onRun:
            timeout: 60
            command: bash
            args:
            - '{{Task.File.Run}}'
        embeddedFiles:
        - name: Run
          filename: run-task.sh
          type: TEXT
          data: |
            # This bash script sends a task to the background daemon process,
            # then waits for it to respond with the output result.
   
            set -euo pipefail
   
            source "$DAEMON_BASH_HELPER_SCRIPT"
   
            echo "Daemon PID is $DAEMON_PID"
            echo "Daemon log file is $DAEMON_LOG"
   
            print_daemon_log "Previous output from daemon"
   
            send_task_to_daemon "{\"pid\": $$, \"frame\": {{Task.Param.Frame}} }"
            wait_for_daemon_task_result
   
            echo Received task result:
            echo "$TASK_RESULT" | jq .
   
            print_daemon_log "Daemon log from running the task"
   
      hostRequirements:
        attributes:
        - name: attr.worker.os.family
          anyOf:
          - linux
   ```