Skip to content

Commit f892c02

Browse files
Add attempt and firstAttemptTimestamp to context (#30)
1 parent dcdab81 commit f892c02

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

integ/basic_workflow_state1.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ func (b basicWorkflowState1) GetStateId() string {
1414
}
1515

1616
func (b basicWorkflowState1) Start(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) {
17+
if ctx.GetAttempt() <= 0 {
18+
panic("attempt should be greater than zero")
19+
}
20+
if ctx.GetFirstAttemptTimestampSeconds() <= 0 {
21+
panic("GetFirstAttemptTimestampSeconds should be greater than zero")
22+
}
1723
return iwf.EmptyCommandRequest(), nil
1824
}
1925

2026
func (b basicWorkflowState1) Decide(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
27+
if ctx.GetAttempt() <= 0 {
28+
panic("attempt should be greater than zero")
29+
}
30+
if ctx.GetFirstAttemptTimestampSeconds() <= 0 {
31+
panic("GetFirstAttemptTimestampSeconds should be greater than zero")
32+
}
2133
var i int
2234
err := input.Get(&i)
2335
if err != nil {

iwf/worker_service_impl.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ func (w *workerServiceImpl) HandleWorkflowStateStart(ctx context.Context, reques
1717
stateDef := w.registry.getWorkflowStateDef(wfType, request.GetWorkflowStateId())
1818
input := NewObject(request.StateInput, w.options.ObjectEncoder)
1919
reqContext := request.GetContext()
20-
wfCtx := newWorkflowContext(ctx, reqContext.GetWorkflowId(), reqContext.GetWorkflowRunId(), reqContext.GetStateExecutionId(), reqContext.GetWorkflowStartedTimestamp())
20+
wfCtx := newWorkflowContext(
21+
ctx, reqContext.GetWorkflowId(), reqContext.GetWorkflowRunId(), reqContext.GetStateExecutionId(), reqContext.GetWorkflowStartedTimestamp(),
22+
int(reqContext.GetAttempt()), reqContext.GetFirstAttemptTimestamp())
2123

2224
pers, err := newPersistence(w.options.ObjectEncoder, w.registry.getWorkflowDataObjectKeyStore(wfType), w.registry.getSearchAttributeTypeStore(wfType), request.DataObjects, request.SearchAttributes, nil)
2325
if err != nil {
@@ -96,7 +98,9 @@ func (w *workerServiceImpl) HandleWorkflowStateDecide(ctx context.Context, reque
9698
stateDef := w.registry.getWorkflowStateDef(wfType, request.GetWorkflowStateId())
9799
input := NewObject(request.StateInput, w.options.ObjectEncoder)
98100
reqContext := request.GetContext()
99-
wfCtx := newWorkflowContext(ctx, reqContext.GetWorkflowId(), reqContext.GetWorkflowRunId(), reqContext.GetStateExecutionId(), reqContext.GetWorkflowStartedTimestamp())
101+
wfCtx := newWorkflowContext(
102+
ctx, reqContext.GetWorkflowId(), reqContext.GetWorkflowRunId(), reqContext.GetStateExecutionId(), reqContext.GetWorkflowStartedTimestamp(),
103+
int(reqContext.GetAttempt()), reqContext.GetFirstAttemptTimestamp())
100104

101105
commandResults, err := fromIdlCommandResults(request.CommandResults, w.options.ObjectEncoder)
102106
if err != nil {

iwf/workflow_context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ type WorkflowContext interface {
88
GetWorkflowStartTimestampSeconds() int64
99
GetStateExecutionId() string
1010
GetWorkflowRunId() string
11+
// GetFirstAttemptTimestampSeconds returns the start time of the first attempt of the API call. It's from ScheduledTimestamp of Cadence/Temporal activity.GetInfo
12+
// require server version 1.2.2+, return 0 if server version is lower
13+
GetFirstAttemptTimestampSeconds() int64
14+
// GetAttempt returns an attempt number, which starts from 1, and increased by 1 for every retry if retry policy is specified. It's from Attempt of Cadence/Temporal activity.GetInfo
15+
// require server version 1.2.2+, return 0 if server version is lower
16+
GetAttempt() int
1117
}

iwf/workflow_context_impl.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ type workflowContextImpl struct {
88
workflowRunId string
99
stateExecutionId string
1010
workflowStartTimestampSeconds int64
11+
attempt int
12+
firstAttemptTimestampSeconds int64
1113
}
1214

13-
func newWorkflowContext(ctx context.Context, workflowId string, workflowRunId string, stateExecutionId string, workflowStartTimestampSeconds int64) WorkflowContext {
15+
func newWorkflowContext(
16+
ctx context.Context, workflowId string, workflowRunId string, stateExecutionId string, workflowStartTimestampSeconds int64,
17+
attempt int, firstAttemptTimestampSeconds int64,
18+
) WorkflowContext {
1419
return &workflowContextImpl{
1520
Context: ctx,
1621
workflowId: workflowId,
1722
workflowRunId: workflowRunId,
1823
stateExecutionId: stateExecutionId,
1924
workflowStartTimestampSeconds: workflowStartTimestampSeconds,
25+
attempt: attempt,
26+
firstAttemptTimestampSeconds: firstAttemptTimestampSeconds,
2027
}
2128
}
2229

@@ -35,3 +42,11 @@ func (w workflowContextImpl) GetStateExecutionId() string {
3542
func (w workflowContextImpl) GetWorkflowRunId() string {
3643
return w.workflowRunId
3744
}
45+
46+
func (w workflowContextImpl) GetFirstAttemptTimestampSeconds() int64 {
47+
return w.firstAttemptTimestampSeconds
48+
}
49+
50+
func (w workflowContextImpl) GetAttempt() int {
51+
return w.attempt
52+
}

0 commit comments

Comments
 (0)