-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
357 lines (301 loc) · 9.49 KB
/
instance.go
File metadata and controls
357 lines (301 loc) · 9.49 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package durex
import (
"encoding/json"
"time"
)
// Instance represents a persisted command with runtime state.
// Instances are created from Specs and track execution progress.
type Instance struct {
// ID is the unique identifier for this instance.
ID string `json:"id"`
// Name is the command type identifier.
Name string `json:"name"`
// Data contains the command payload.
Data M `json:"data,omitempty"`
// Status is the current execution state.
Status Status `json:"status"`
// Retries is the remaining retry count.
Retries int `json:"retries"`
// Sequence is the remaining command chain.
Sequence []string `json:"sequence,omitempty"`
// ParentID links to the parent command that spawned this instance.
ParentID *string `json:"parent_id,omitempty"`
// Priority determines execution order.
Priority int `json:"priority"`
// Tags for categorization.
Tags []string `json:"tags,omitempty"`
// UniqueKey for deduplication.
// If set, only one active command with this key can exist at a time.
UniqueKey string `json:"unique_key,omitempty"`
// TraceID for distributed tracing.
// Propagated from parent commands.
TraceID string `json:"trace_id,omitempty"`
// CorrelationID links related commands together.
// Propagated from parent commands.
CorrelationID string `json:"correlation_id,omitempty"`
// CreatedAt is when the instance was created.
CreatedAt time.Time `json:"created_at"`
// ReadyAt is when the instance becomes eligible for execution.
ReadyAt time.Time `json:"ready_at"`
// StartedAt is when execution began (nil if not started).
StartedAt *time.Time `json:"started_at,omitempty"`
// CompletedAt is when execution finished (nil if not completed).
CompletedAt *time.Time `json:"completed_at,omitempty"`
// DeadlineAt is the execution deadline (nil if no deadline).
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
// Timeout is the maximum execution time per attempt.
// If the handler doesn't complete within this duration, the context is cancelled.
Timeout time.Duration `json:"timeout,omitempty"`
// Period is the repeat interval for recurring commands.
Period time.Duration `json:"period,omitempty"`
// Cron is the cron expression for scheduled commands.
// Uses standard cron format: "minute hour day-of-month month day-of-week"
Cron string `json:"cron,omitempty"`
// Error contains the error message if the command failed.
Error string `json:"error,omitempty"`
// Attempt tracks the current attempt number (starts at 1).
Attempt int `json:"attempt"`
// Metadata stores additional runtime information.
Metadata M `json:"metadata,omitempty"`
// History contains the execution history of this command.
// Events are appended as the command progresses through its lifecycle.
History []Event `json:"history,omitempty"`
}
// Get retrieves a value from the command data with type assertion.
// Returns nil if the key doesn't exist.
//
// Note: Data passes through JSON serialization during persistence and deep
// copies (Clone, ContinueSequence). This means Go integer types (int, int64)
// are coerced to float64 after a JSON round-trip. Use GetInt for integer
// values, which handles this coercion transparently.
func (i *Instance) Get(key string) any {
if i.Data == nil {
return nil
}
return i.Data[key]
}
// GetString retrieves a string value from command data.
func (i *Instance) GetString(key string) string {
v, _ := i.Data[key].(string)
return v
}
// GetInt retrieves an int value from command data.
// Handles both int and float64 (from JSON unmarshaling).
func (i *Instance) GetInt(key string) int {
switch v := i.Data[key].(type) {
case int:
return v
case int64:
return int(v)
case float64:
return int(v)
default:
return 0
}
}
// GetBool retrieves a bool value from command data.
func (i *Instance) GetBool(key string) bool {
v, _ := i.Data[key].(bool)
return v
}
// GetSlice retrieves a slice value from command data.
func (i *Instance) GetSlice(key string) []any {
v, _ := i.Data[key].([]any)
return v
}
// GetMap retrieves a map value from command data.
func (i *Instance) GetMap(key string) M {
v, _ := i.Data[key].(map[string]any)
return v
}
// Set stores a value in the command data.
// Changes are not automatically persisted. Call executor.Update() if needed.
//
// Values stored via Set pass through JSON serialization during persistence
// and deep copies. Integer types will be read back as float64 from JSON.
// Use GetInt to retrieve integer values safely regardless of the underlying type.
func (i *Instance) Set(key string, value any) {
if i.Data == nil {
i.Data = make(M)
}
i.Data[key] = value
}
// ContinueSequence creates a Result that spawns the next command in the sequence.
// The accumulated data is passed to the next command.
// Returns Empty() if the sequence is empty.
//
// Example:
//
// func (c *Step1Command) Execute(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
// cmd.Set("step1_result", "done")
// return cmd.ContinueSequence(nil), nil
// }
func (i *Instance) ContinueSequence(additionalData M) Result {
if len(i.Sequence) == 0 {
return Empty()
}
nextName := i.Sequence[0]
remaining := i.Sequence[1:]
// Deep copy data to prevent shared nested references between sequence steps.
// Note: deepCopyMap uses JSON round-trip, which coerces some Go types
// (e.g. int64 → float64). This is consistent with Clone() and storage
// serialization, where all data passes through JSON anyway.
data := deepCopyMap(i.Data)
if data == nil {
data = make(M)
}
for k, v := range additionalData {
data[k] = v
}
return Result{
Commands: []Spec{{
Name: nextName,
Data: data,
Sequence: remaining,
TraceID: i.TraceID,
CorrelationID: i.CorrelationID,
}},
}
}
// Clone creates a deep copy of the instance.
func (i *Instance) Clone() *Instance {
clone := *i
// Deep copy Data map using JSON marshal/unmarshal to handle nested structures
if i.Data != nil {
clone.Data = deepCopyMap(i.Data)
}
if i.Sequence != nil {
clone.Sequence = make([]string, len(i.Sequence))
copy(clone.Sequence, i.Sequence)
}
if i.Tags != nil {
clone.Tags = make([]string, len(i.Tags))
copy(clone.Tags, i.Tags)
}
// Deep copy Metadata map using JSON marshal/unmarshal to handle nested structures
if i.Metadata != nil {
clone.Metadata = deepCopyMap(i.Metadata)
}
if i.History != nil {
clone.History = make([]Event, len(i.History))
copy(clone.History, i.History)
}
return &clone
}
// deepCopyMap performs a deep copy of a map using JSON marshal/unmarshal.
// This ensures nested maps, slices, and objects are properly cloned.
func deepCopyMap(m M) M {
if m == nil {
return nil
}
// Use JSON marshal/unmarshal for deep copy
data, err := json.Marshal(m)
if err != nil {
// Fallback to shallow copy if marshal fails
result := make(M, len(m))
for k, v := range m {
result[k] = v
}
return result
}
var result M
if err := json.Unmarshal(data, &result); err != nil {
// Fallback to shallow copy if unmarshal fails
result = make(M, len(m))
for k, v := range m {
result[k] = v
}
return result
}
return result
}
// RecordEvent appends an event to the command's history.
func (i *Instance) RecordEvent(eventType EventType, message string) {
i.History = append(i.History, Event{
Type: eventType,
Timestamp: time.Now(),
Attempt: i.Attempt,
Message: message,
})
}
// RecordEventWithDuration appends an event with duration to the command's history.
func (i *Instance) RecordEventWithDuration(eventType EventType, duration time.Duration, message string) {
i.History = append(i.History, Event{
Type: eventType,
Timestamp: time.Now(),
Attempt: i.Attempt,
DurationMs: duration.Milliseconds(),
Message: message,
})
}
// RecordError appends a failed event with error details to the command's history.
func (i *Instance) RecordError(eventType EventType, err error) {
errMsg := ""
if err != nil {
errMsg = err.Error()
}
i.History = append(i.History, Event{
Type: eventType,
Timestamp: time.Now(),
Attempt: i.Attempt,
Error: errMsg,
})
}
// MarshalJSON implements json.Marshaler.
func (i *Instance) MarshalJSON() ([]byte, error) {
type Alias Instance
return json.Marshal(&struct {
*Alias
Period int64 `json:"period_ns,omitempty"`
Timeout int64 `json:"timeout_ns,omitempty"`
}{
Alias: (*Alias)(i),
Period: int64(i.Period),
Timeout: int64(i.Timeout),
})
}
// UnmarshalJSON implements json.Unmarshaler.
func (i *Instance) UnmarshalJSON(data []byte) error {
type Alias Instance
aux := &struct {
*Alias
Period int64 `json:"period_ns,omitempty"`
Timeout int64 `json:"timeout_ns,omitempty"`
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.Period = time.Duration(aux.Period)
i.Timeout = time.Duration(aux.Timeout)
return nil
}
// Duration returns how long the command took to execute.
// Returns 0 if the command hasn't completed.
func (i *Instance) Duration() time.Duration {
if i.StartedAt == nil || i.CompletedAt == nil {
return 0
}
return i.CompletedAt.Sub(*i.StartedAt)
}
// Age returns how long ago the command was created.
func (i *Instance) Age() time.Duration {
return time.Since(i.CreatedAt)
}
// IsOverdue returns true if the command has passed its deadline.
func (i *Instance) IsOverdue() bool {
if i.DeadlineAt == nil {
return false
}
return time.Now().After(*i.DeadlineAt)
}
// HasTag returns true if the instance has the given tag.
func (i *Instance) HasTag(tag string) bool {
for _, t := range i.Tags {
if t == tag {
return true
}
}
return false
}