-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckpoint.go
More file actions
238 lines (197 loc) · 5.04 KB
/
checkpoint.go
File metadata and controls
238 lines (197 loc) · 5.04 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package flow
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"sync"
"time"
)
var (
ErrCheckpointNotFound = errors.New("checkpoint not found")
ErrInvalidCheckpoint = errors.New("invalid checkpoint data")
ErrCheckpointInvalidType = errors.New("checkpoint type mismatch")
ErrValueNotSerializable = errors.New("value is not serializable")
)
type FlowCheckpointable interface {
SaveCheckpoint() (*Checkpoint, error)
LoadCheckpoint(checkpoint *Checkpoint) error
SaveToStore(store CheckpointStore, key string) error
LoadFromStore(store CheckpointStore, key string) error
Reset()
}
type CheckpointStore interface {
Save(key string, checkpoint *Checkpoint) error
Load(key string) (*Checkpoint, error)
Delete(key string) error
List() ([]string, error)
}
type Checkpoint struct {
ID string `json:"id"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
Version int `json:"version"`
State FlowState `json:"state"`
Data FlowCheckpointData `json:"data"`
Metadata map[string]string `json:"metadata,omitempty"`
}
type FlowState int
const (
FlowStateIdle FlowState = iota
FlowStateRunning
FlowStatePaused
FlowStateCompleted
FlowStateFailed
)
const (
CheckpointTypeGraph = "graph"
CheckpointTypeChain = "chain"
defaultDirPerm = 0750
defaultFilePerm = 0600
)
type FlowCheckpointData struct {
Steps []StepState `json:"steps,omitempty"`
Current int `json:"current,omitempty"`
Values []any `json:"values,omitempty"`
Error string `json:"error,omitempty"`
Extra map[string]any `json:"extra,omitempty"`
}
type StepState struct {
Name string `json:"name"`
Status int `json:"status"`
Executed bool `json:"executed"`
}
func NewCheckpoint(flowType string) *Checkpoint {
return &Checkpoint{
Type: flowType,
Version: 1,
State: FlowStateIdle,
Data: FlowCheckpointData{
Steps: make([]StepState, 0),
Values: make([]any, 0),
},
}
}
func (c *Checkpoint) SetMetadata(key, value string) {
if c.Metadata == nil {
c.Metadata = make(map[string]string)
}
c.Metadata[key] = value
}
func (c *Checkpoint) GetMetadata(key string) (string, bool) {
if c.Metadata == nil {
return "", false
}
v, ok := c.Metadata[key]
return v, ok
}
type FileCheckpointStore struct {
dir string
mu sync.RWMutex
}
func NewFileCheckpointStore(dir string) (*FileCheckpointStore, error) {
if err := os.MkdirAll(dir, defaultDirPerm); err != nil {
return nil, err
}
return &FileCheckpointStore{dir: dir}, nil
}
func (s *FileCheckpointStore) Save(key string, checkpoint *Checkpoint) error {
s.mu.Lock()
defer s.mu.Unlock()
checkpoint.ID = key
checkpoint.CreatedAt = time.Now()
data, err := json.MarshalIndent(checkpoint, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.filePath(key), data, defaultFilePerm)
}
func (s *FileCheckpointStore) Load(key string) (*Checkpoint, error) {
s.mu.RLock()
defer s.mu.RUnlock()
path := s.filePath(key)
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, ErrCheckpointNotFound
}
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
var checkpoint Checkpoint
if err := json.Unmarshal(data, &checkpoint); err != nil {
return nil, err
}
return &checkpoint, nil
}
func (s *FileCheckpointStore) Delete(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
path := filepath.Clean(s.filePath(key))
if _, err := os.Stat(path); os.IsNotExist(err) {
return ErrCheckpointNotFound
}
return os.Remove(path)
}
func (s *FileCheckpointStore) List() ([]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
entries, err := os.ReadDir(s.dir)
if err != nil {
return nil, err
}
var keys []string
for _, entry := range entries {
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".json" {
name := entry.Name()
keys = append(keys, name[:len(name)-5])
}
}
return keys, nil
}
func (s *FileCheckpointStore) filePath(key string) string {
return filepath.Join(s.dir, key+".json")
}
type MemoryCheckpointStore struct {
data map[string]*Checkpoint
mu sync.RWMutex
}
func NewMemoryCheckpointStore() *MemoryCheckpointStore {
return &MemoryCheckpointStore{
data: make(map[string]*Checkpoint),
}
}
func (s *MemoryCheckpointStore) Save(key string, checkpoint *Checkpoint) error {
s.mu.Lock()
defer s.mu.Unlock()
checkpoint.ID = key
checkpoint.CreatedAt = time.Now()
s.data[key] = checkpoint
return nil
}
func (s *MemoryCheckpointStore) Load(key string) (*Checkpoint, error) {
s.mu.RLock()
defer s.mu.RUnlock()
checkpoint, ok := s.data[key]
if !ok {
return nil, ErrCheckpointNotFound
}
return checkpoint, nil
}
func (s *MemoryCheckpointStore) Delete(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.data[key]; !ok {
return ErrCheckpointNotFound
}
delete(s.data, key)
return nil
}
func (s *MemoryCheckpointStore) List() ([]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
keys := make([]string, 0, len(s.data))
for k := range s.data {
keys = append(keys, k)
}
return keys, nil
}