根据外部(例如用户)输入更改 Uber Cadence 睡眠时间

Changing the Uber Cadence Sleep time based on external (e.g. user) input

是否有 Cadence 工作流根据外部输入更改睡眠持续时间的示例?

我正在寻找的场景是这样的:工作流进入睡眠状态直到时间 X 但在睡眠开始之后但在它完成之前,有一个外部触发器导致时间 X 更改为时间 Y。Y 可以要么晚于X,要么早于X。它甚至可以早于"now",这应该会立即释放睡眠。

这是您要查找的代码片段

func SampleTimerWorkflow(ctx workflow.Context, timerDelay time.Duration) error 
{
    logger := workflow.GetLogger(ctx)
    resetCh := workflow.GetSignalChannel(ctx, "reset")

    timerFired := false
    delay := timerDelay
    for ;!timerFired; {
        selector := workflow.NewSelector(ctx)

        logger.Sugar().Infof("Setting up a timer to fire after: %v", delay)
        timerCancelCtx, cancelTimerHandler := workflow.WithCancel(ctx)
        timerFuture := workflow.NewTimer(timerCancelCtx, delay)
        selector.AddFuture(timerFuture, func(f workflow.Future) {
            logger.Info("Timer Fired.")
            timerFired = true
        })

        selector.AddReceive(resetCh, func(c workflow.Channel, more bool) {
            logger.Info("Reset signal received.")
            logger.Info("Cancel outstanding timer.")
            cancelTimerHandler()

            var t int
            c.Receive(ctx, &t)
            logger.Sugar().Infof("Reset delay: %v seconds", t)
            delay = time.Second * time.Duration(t)
        })

        logger.Info("Waiting for timer to fire.")
        selector.Select(ctx)
    }

    workflow.GetLogger(ctx).Info("Workflow completed.")
    return nil
}

现在您可以发送信号将计时器重置为 10 秒,如下所示:

cadence-cli --domain <domain> wf signal -w <workflow_id>  --name reset --input 10