-
Notifications
You must be signed in to change notification settings - Fork 15
/
record.go
70 lines (60 loc) · 1.6 KB
/
record.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
64
65
66
67
68
69
70
package cke
import (
"time"
)
// RecordStatus is status of an operation
type RecordStatus string
// Record statuses
const (
StatusNew = RecordStatus("new")
StatusRunning = RecordStatus("running")
StatusCancelled = RecordStatus("cancelled")
StatusCompleted = RecordStatus("completed")
)
// Record represents a record of an operation
type Record struct {
ID int64 `json:"id,string"`
Status RecordStatus `json:"status"`
Operation string `json:"operation"`
Command Command `json:"command"`
Targets []string `json:"targets"`
Info string `json:"info"`
Error string `json:"error"`
StartAt time.Time `json:"start-at"`
EndAt time.Time `json:"end-at"`
}
// NewRecord creates new `Record`
func NewRecord(id int64, op string, targets []string) *Record {
return &Record{
ID: id,
Status: StatusNew,
Operation: op,
Targets: targets,
StartAt: time.Now().UTC(),
}
}
// Cancel cancels the operation
func (r *Record) Cancel() {
r.Status = StatusCancelled
r.EndAt = time.Now().UTC()
}
// Complete completes the operation
func (r *Record) Complete() {
r.Status = StatusCompleted
r.EndAt = time.Now().UTC()
}
// SetCommand updates the record for the new command
func (r *Record) SetCommand(c Command) {
r.Status = StatusRunning
r.Command = c
}
// SetInfo records some information of the operation result
func (r *Record) SetInfo(i string) {
r.Info = i
}
// SetError cancels the operation with error information
func (r *Record) SetError(e error) {
r.Status = StatusCancelled
r.Error = e.Error()
r.EndAt = time.Now().UTC()
}