如何将 workflow.Context 传递给 Cadence 活动(activity)?

huangapple go评论70阅读模式
英文:

How to pass workflow.Context to the Cadence activity

问题

我想在我的 Cadence 活动中使用 workflow.Sleep 调用,以便能够正确测试它(并模拟来自 Sleep 函数的错误结果)。

在实施之前,我注意到两个重要的事情:

  • context.Contextworkflow.Context 是不同的类型。
  • 每个活动的第一个参数 context.Context 是可选的,可以省略。

我的尝试:

1. 第一次尝试

import "go.uber.org/cadence/workflow"

// 活动:
func (s *MyActivities) WorkflowSleep(_ context.Context, workflowCtx workflow.Context, duration time.Duration) error {
    return workflow.Sleep(workflowCtx, duration)
}

// 在工作流中,ctx 的类型是 workflow.Context:
err := workflow.ExecuteActivity(ctx, myActivities.WorkflowSleep, ctx, duration).Get(ctx, nil)

错误:

"error":"无法解码活动函数输入字节,错误为:无法解码参数:0,*internal.Context,使用 json 错误:json: 无法将对象解组为类型为 internal.Context 的 Go 值的函数

2. 第二次尝试

import "go.uber.org/cadence/workflow"

// 活动:
func (s *MyActivities) WorkflowSleep(workflowCtx workflow.Context, duration time.Duration) error {
    return workflow.Sleep(workflowCtx, duration)
}

// 在工作流中,ctx 的类型是 workflow.Context:
err := workflow.ExecuteActivity(ctx, myActivities.WorkflowSleep, duration).Get(ctx, nil)

错误:

"PanicError":"reflect: 调用参数过少"

问题

是否可以在任何活动中使用 workflow.Sleep

英文:

I would like to put workflow.Sleep call in one of my Cadence activities to be able to test it properly (and simulate error result coming from Sleep function).

Two important things which I noticed before implementing:

My tries:

1. First try

import "go.uber.org/cadence/workflow"

// Activity:    
func (s *MyActivities) WorkflowSleep(_ context.Context, workflowCtx workflow.Context, duration time.Duration) error {
	return workflow.Sleep(workflowCtx, duration)
}

// In workflow; ctx has type workflow.Context:
	err := workflow.ExecuteActivity(ctx, myActivities.WorkflowSleep, ctx, duration).Get(ctx, nil)

Error:
> "error":"unable to decode the activity function input bytes with error: unable to decode argument: 0, *internal.Context, with json error: json: cannot unmarshal object into Go value of type internal.Context for function

2. Second try

import "go.uber.org/cadence/workflow"

// Activity:    
func (s *MyActivities) WorkflowSleep(workflowCtx workflow.Context, duration time.Duration) error {
	return workflow.Sleep(workflowCtx, duration)
}

// In workflow; ctx has type workflow.Context:
	err := workflow.ExecuteActivity(ctx, myActivities.WorkflowSleep, duration).Get(ctx, nil)

Error:
> "PanicError":"reflect: Call with too few input arguments"

Question

Is it possible to use workflow.Sleep inside any activity?

答案1

得分: 2

在任何活动中使用workflow.Sleep是不允许的。

workflow.Sleep只能在工作流代码中使用。
更一般地说,workflow包中的所有API只能在工作流代码中使用。

工作流代码将在决策任务中执行,并且工作流线程/协程(一种特殊类型的goroutine)由Cadence管理。这就是workflow包中这些API的工作原理。如果没有决策任务工作流线程/协程,这些API将无法正常工作。

另一方面,活动代码只是纯粹的普通代码,您应该使用常规的本地库或依赖项来实现逻辑。例如,您可以在活动代码中使用time.Sleep

英文:

> Is it possible to use workflow.Sleep inside any activity?

No, it's NOT allowed to use workflow.Sleep in activity code.

Workflow.Sleep is only allowed in workflow code.
More general, all the APIs in workflow package are ONLY allowed in workflow code.

The workflow code will be executed within decision task, and the workflow threads/coroutines(special kind of goroutines) are managed by Cadence. This is how those APIs in workflow package works. Without decision tasks and workflow threads/coroutines, those API won't work properly.

The activity code on the other hand, is just purely normal code that you should use regular native library or dependency to implement the logic. For example, you can use time.Sleep in activity code.

huangapple
  • 本文由 发表于 2022年3月8日 00:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71383875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定