-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_delay.go
61 lines (46 loc) · 1.42 KB
/
cmd_delay.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
package flowstate
import (
"fmt"
"time"
)
var DelayAtAnnotation = `flowstate.delay.at`
var DelayDurationAnnotation = `flowstate.delay.duration`
var DelayCommitAnnotation = `flowstate.delay.commit`
func Delayed(state State) bool {
return state.Transition.Annotations[DelayAtAnnotation] != ``
}
func Delay(stateCtx *StateCtx, dur time.Duration) *DelayCommand {
return &DelayCommand{
StateCtx: stateCtx,
Duration: dur,
}
}
type DelayCommand struct {
command
StateCtx *StateCtx
DelayStateCtx *StateCtx
Duration time.Duration
Commit bool
}
func (cmd *DelayCommand) WithCommit(commit bool) *DelayCommand {
cmd.Commit = commit
return cmd
}
func (cmd *DelayCommand) Prepare() error {
delayedStateCtx := cmd.StateCtx.CopyTo(&StateCtx{})
delayedStateCtx.Transitions = append(delayedStateCtx.Transitions, delayedStateCtx.Current.Transition)
nextTs := Transition{
FromID: delayedStateCtx.Current.Transition.ToID,
ToID: delayedStateCtx.Current.Transition.ToID,
Annotations: nil,
}
if Paused(delayedStateCtx.Current) {
nextTs.SetAnnotation(StateAnnotation, `resumed`)
}
nextTs.SetAnnotation(DelayAtAnnotation, time.Now().Format(time.RFC3339Nano))
nextTs.SetAnnotation(DelayDurationAnnotation, cmd.Duration.String())
nextTs.SetAnnotation(DelayCommitAnnotation, fmt.Sprintf("%v", cmd.Commit))
delayedStateCtx.Current.Transition = nextTs
cmd.DelayStateCtx = delayedStateCtx
return nil
}