-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
63 lines (50 loc) · 1.1 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package stepper
import (
"context"
"encoding/json"
"time"
)
type Context interface {
Task() *Task
Context() context.Context
CreateSubtask(sub CreateTask)
BindState(state any) error
SetState(state any) error
SetRetryAfter(timeout time.Duration)
SetContext(ctx context.Context)
}
type taskContext struct {
ctx context.Context
task *Task
subtasks []CreateTask
retryAfter time.Duration
taskEngine Engine
}
func (c *taskContext) Task() *Task {
return c.task
}
func (c *taskContext) Context() context.Context {
return c.ctx
}
func (c *taskContext) SetContext(ctx context.Context) {
c.ctx = ctx
}
func (c *taskContext) CreateSubtask(sub CreateTask) {
c.subtasks = append(c.subtasks, sub)
}
func (c *taskContext) SetRetryAfter(timeout time.Duration) {
c.retryAfter = timeout
}
func (c *taskContext) BindState(state any) error {
if len(c.task.State) == 0 {
return nil
}
return json.Unmarshal(c.task.State, state)
}
func (c *taskContext) SetState(state any) error {
b, err := json.Marshal(state)
if err != nil {
return err
}
return c.taskEngine.SetState(c.ctx, c.task, b)
}