

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在队列环境中运行后台守护进程
<a name="run-a-background-daemon-process"></a>

 在许多渲染用例中，加载应用程序和场景数据可能需要大量时间。如果作业每帧都重新加载它们，则会将大部分时间花在开销上。通常可以将应用程序作为后台守护进程加载一次，让它加载场景数据，然后通过进程间通信 (IPC) 向其发送命令以执行渲染。

 许多开源 Deadline Cloud 集成都使用这种模式。Open Job Description 项目提供了一个[适配器运行时库](https://github.com/OpenJobDescription/openjd-adaptor-runtime-for-python)，该库在所有支持的操作系统上都具有强大的 IPC 模式。

 为了演示这种模式，有一个[独立的示例任务包](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 队列的 Deadline Cloud 场，请按照 De [adline Cloud 控制台](https://console.aws.amazon.com/deadlinecloud/home)中的指导性入门体验创建具有默认设置的群组。

1.  如果您的工作站上没有 Deadline Cloud CLI 和 Deadline Cloud 监控器，请按照用户指南中[设置 Deadline Cloud 提交者](https://docs.aws.amazon.com/deadline-cloud/latest/userguide/submitter.html)中的步骤进行操作。

1.  用于克隆 D `git` e [adline Cloud 示例 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.  使用 Deadline Cloud 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.  在 Deadline Cloud 监控应用程序中，您将看到新作业并可以监控其进度。一旦与Linux队列关联的队列有工作人员可以运行作业的任务，该任务将在大约一分钟后完成。选择其中一项任务后，在任务面板的右上角菜单中选择 “**查看日志**” 选项。

    右侧有两个会话操作：“**启动” DaemonProcess 和 “****任务运行”**。窗口中央的日志查看器对应于右侧选定的会话操作。

    选择 “**查看所有任务的日志**” 选项。时间轴显示了作为会话一部分运行的其余任务以及退出环境的`Shut down DaemonProcess`操作。

## 查看守护程序日志
<a name="daemon-view-logs"></a>

1. 在本节中，您将使用 Deadline Cloud 监视器将会话操作与作业模板中定义的会话操作进行比较。它延续了上一节。

    在文本编辑器中打开 [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.  在 Deadline Cloud 监视器中选择`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.  在 Deadline Cloud 监视器中选择 “任务运行：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
   ```