-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.go
More file actions
113 lines (104 loc) · 3.02 KB
/
Copy pathnode.go
File metadata and controls
113 lines (104 loc) · 3.02 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package flow
import (
"slices"
"time"
"github.com/goware/flow/internal/durable"
)
// StagedCommand is an ephemeral builder for a command staged by a worker decision. It
// is valid only for the duration of that decision.
type StagedCommand struct {
scope *scopeState
key string
}
func (node *StagedCommand) Key() string {
if node == nil {
return ""
}
return node.key
}
func (node *StagedCommand) WaitFor(event EventRef, key string) *StagedCommand {
if node == nil {
return node
}
wait, err := makeCommandEventWait(event, key)
if err != nil {
node.poison(err)
return node
}
if command, ok := node.decisionCommand("wait for"); ok {
if !slices.Contains(command.waits, wait) && len(command.waits) >= maxCommandEventWaits {
node.scope.poison(newError(ErrInvalid, "enqueue", "wait", node.key, "command exceeds the 256 event-wait limit"))
return node
}
command.waits = addCommandEventWait(command.waits, wait)
node.scope.decision.commands[node.key] = command
}
return node
}
func (node *StagedCommand) Within(duration time.Duration) *StagedCommand {
if node == nil {
return node
}
if duration <= 0 {
node.poison(newError(ErrInvalid, "enqueue", "within", node.key, "within must be positive"))
return node
}
normalized, _, err := durable.CeilMilliseconds("within", duration)
if err != nil {
node.poison(newError(ErrInvalid, "enqueue", "within", node.key, err.Error()))
return node
}
if command, ok := node.decisionCommand("within"); ok {
if command.within == normalized {
return node
}
if command.within > 0 {
node.scope.poison(newError(ErrInvalid, "enqueue", "within", node.key, "within configured with different values"))
return node
}
command.within = normalized
node.scope.decision.commands[node.key] = command
}
return node
}
func (node *StagedCommand) Delay(duration time.Duration) *StagedCommand {
if node == nil {
return node
}
if duration <= 0 {
node.poison(newError(ErrInvalid, "enqueue", "delay", node.key, "delay must be positive"))
return node
}
normalized, _, err := durable.CeilMilliseconds("delay", duration)
if err != nil {
node.poison(newError(ErrInvalid, "enqueue", "delay", node.key, err.Error()))
return node
}
if command, ok := node.decisionCommand("delay"); ok {
if command.startAfter == normalized {
return node
}
if command.startAfter > 0 {
node.scope.poison(newError(ErrInvalid, "enqueue", "delay", node.key, "delay configured more than once"))
return node
}
command.startAfter = normalized
node.scope.decision.commands[node.key] = command
}
return node
}
func (node *StagedCommand) decisionCommand(operation string) (stagedCommand, bool) {
if node.scope == nil || node.scope.firstError != nil {
return stagedCommand{}, false
}
command, ok := node.scope.decision.commands[node.key]
if !ok {
node.scope.poison(newError(ErrInvalidState, "enqueue", operation, node.key, "command is unavailable"))
}
return command, ok
}
func (node *StagedCommand) poison(err error) {
if node != nil && node.scope != nil {
node.scope.poison(err)
}
}