使用輪詢等待

暫停工作流程執行作業後,您可以輪詢外部服務,或等待長時間執行的作業完成。sys.sleep 標準程式庫函式 會暫停執行指定秒數,最多 31536000 秒 (一年)。

暫停工作流程

您可以在工作流程定義中新增休眠步驟,暫停執行工作流程。這個步驟包含對 sys.sleep 的呼叫,並以秒為單位指定要暫停工作流程的時間長度:

YAML

  - STEP_NAME:
      call: sys.sleep
      args:
          seconds: SLEEP_IN_SECONDS
    

JSON

    [
      {
        "STEP_NAME": {
          "call": "sys.sleep",
          "args": {
            "seconds": "SLEEP_IN_SECONDS"
          }
        }
      }
    ]
    

輪詢資料

您也可以使用 sys.sleep,在指定間隔輪詢資料。舉例來說,您可能想輪詢 API,直到符合特定條件為止:

YAML

  main:
      params: [jobId]
      steps:
        - checkJob:
            call: http.get
            args:
                url: ${"https://example.com/jobs/" + jobId}
                auth:
                    type: OAuth2
            result: jobStatus
        - checkIfDone:
            switch:
              - condition: ${jobStatus.complete}
                return: ${jobStatus}
        - wait:
            call: sys.sleep
            args:
                seconds: 60
            next: checkJob
    

JSON

  {
    "main": {
      "params": [
        "jobId"
      ],
      "steps": [
        {
          "checkJob": {
            "call": "http.get",
            "args": {
              "url": "${\"https://example.com/jobs/\" + jobId}",
              "auth": {
                "type": "OAuth2"
              }
            },
            "result": "jobStatus"
          }
        },
        {
          "checkIfDone": {
            "switch": [
              {
                "condition": "${jobStatus.complete}",
                "return": "${jobStatus}"
              }
            ]
          }
        },
        {
          "wait": {
            "call": "sys.sleep",
            "args": {
              "seconds": 60
            },
            "next": "checkJob"
          }
        }
      ]
    }
  }