1

我正在尝试学习 Google Workflow 重试机制。我的实际要求是请求一个http.get并且会得到一个文本响应,我必须检查那个文本,如果它不满足我的条件,它应该在 10 秒后重试。

因此,为了尝试一下,我制作了下面的示例,并从文档中了解到,标准重试不符合我的要求,因为它基于 http 返回代码,因此,我选择了自定义谓词并定义了一个子工作流程。并了解到我不能将任何参数传递给这个子工作流程,并尝试raise按照一个示例出错。但是,这不会在代码窗口中显示任何错误,但在运行时失败

main:
  steps:
    - first_step:
        try:
            steps:
                - getCurrentDay:
                    call: http.get
                    args:
                        url: https://us-central1-workflowsample.cloudfunctions.net/datetime
                    result: currentDateTime
                - setFromCallResult:
                    assign:
                        - dayName: ${currentDateTime.body.dayOfTheWeek}
                - whatIsToday:
                    call: sys.log
                    args:
                        data: ${dayName + " is actually a day"}
                        severity: "INFO" # Optional
                - callRetry:
                    switch:
                        - condition: ${dayName == "Thursday"}
                          raise: ${dayName}                                    
        retry:
            predicate: ${my_own_predicate}
            max_retries: 5
            backoff:
                initial_delay: 2
                max_delay: 60
                multiplier: 1
    - last_step:
        return: "OK"

my_own_predicate:
  params: [dayName]
  steps:
    - checkRetryCond:
        switch:
            - condition: ${dayName == "Thursday"}
              return: False
    - otherwise:
        return: True

以下是错误:

RuntimeError: "Thursday"
in step "first_step", routine "main", line: 24
"Thursday"

第 24 行是

            predicate: ${my_own_predicate}

在这一点上,我无法弄清楚问题是什么

4

2 回答 2

2
于 2021-10-21T22:44:48.183 回答
1

我无法解决重试问题。所以,我没有重试方法就继续正常进行。它有点冗长,它是我现在唯一的解决方法。

  1. 主要是分配 url、重试次数和 sleep_seconds
  2. 我正在调用函数 getDate 并传递上述值。
  3. 在 getDay
    • 显示参数
    • 进入一个看起来像嵌套步骤的循环
    • init:进行初始化
    • check_condition:检查重试条件。如果 OK 执行流程:否则失败:
    • 流程:调用http.get
    • showResult:显示接收到的结果
    • verifyResult:处理接收到的结果。如果看起来不错执行成功:
    • iterate: 是一个嵌套步骤 - 睡眠并减少重试次数并返回到 check_condition:
    • 再往下我就成功了:失败:和exit_fn:
main:
  params: [today] 
  steps:
    - initValues1:
        assign:
            - inParm: 
                url: https://us-central1-workflowsample.cloudfunctions.net/datetime
                retry: 5
                sleep_seconds: 10
                day: ${today.day}
    - callFunction1:
        call: getDay
        args:
            inParm: ${inParm}
        result: retData
    - showLog1:
        call: sys.log
        args:
            data: ${"Returned results are, today is "+ retData.dayName + " and it was expected to be " + today.day + "; Go-NoGo Decision=" + retData.gonogo}
            severity: "INFO" # Optional
    - verifyRetry1:
        switch:
            - condition: ${retData.gonogo == "NOGO"}
              next: exit_wf
        next: process2
    - process2:
        call: sys.log
        args:
            data: ${"Doing Process Two"}
            severity: "INFO" # Optional
    - exit_wf:
        return: ${retData}

getDay:
  params: [inParm]
  steps:
    - displayParms:
        call: sys.log
        args:
            data: ${inParm}
            severity: "INFO"
    - looping:
        steps:      
            - init: 
                assign:
                    - retry: ${inParm.retry}
                    - sleep_seconds: ${inParm.sleep_seconds}
            - check_condition:
                switch:
                - condition: ${retry > 0}
                  next: process
                next: fail
            - process:
                call: http.get
                args:
                    url: ${inParm.url}
                result: currentDateTime
            - showResult:
                call: sys.log
                args:
                    data: ${"Retry=" + string(retry) + "; Today is = " + currentDateTime.body.dayOfTheWeek}
                    severity: "INFO"
            - verifyResult:
                switch:
                - condition: ${currentDateTime.body.dayOfTheWeek == inParm.day}
                  next: success
                next: iterate
            - iterate:
                steps:
                    - sleep:
                        call: sys.sleep
                        args:
                            seconds: ${sleep_seconds}
                    - decrement_retry:
                        assign:
                            - retry: ${retry-1}
                        next: check_condition
    - success:
        assign:
            - retData:
                gonogo: GO
                dayName: ${currentDateTime.body.dayOfTheWeek}
        next: exit_fn
    - fail:
        assign:
            - retData:
                gonogo: NOGO
                dayName: SKIP
                retry: ${retry}
        next: exit_fn
    - exit_fn:
        return: ${retData}
于 2021-10-08T06:26:04.210 回答