Repository Public
1// Package journal records, durably, which commands the agent started and2// finished. After a crash, a command that was started but has no recorded3// end is in an unknown state: it is reported as failed ("outcome unknown")4// and never executed again.5package journal67import (8 "bufio"9 "encoding/json"10 "os"11 "sync"12 "time"13)1415// Record is one journal line.16type Record struct {17 ID string `json:"id"`18 Type string `json:"type,omitempty"`19 Event string `json:"event"` // started | finished | reported20 TS time.Time `json:"ts"`21}2223// Unresolved is a command whose outcome must still be reported to the server.24type Unresolved struct {25 ID string26 Type string27 Started time.Time28}2930// Journal is safe for concurrent use.31type Journal struct {32 path string33 mu sync.Mutex34 f *os.File35}3637// Open reads the journal and returns the commands that were started by a38// previous process and never finished. The file is compacted to those.39func Open(path string) (*Journal, []Unresolved, error) {40 type st struct {41 rec Record42 finished bool43 }44 seen := map[string]*st{}45 var order []string46 if f, err := os.Open(path); err == nil {47 sc := bufio.NewScanner(f)48 sc.Buffer(make([]byte, 64<<10), 1<<20)49 for sc.Scan() {50 var r Record51 if json.Unmarshal(sc.Bytes(), &r) != nil || r.ID == "" {52 continue // torn line53 }54 switch r.Event {55 case "started":56 if seen[r.ID] == nil {57 seen[r.ID] = &st{rec: r}58 order = append(order, r.ID)59 }60 case "finished", "reported":61 if s := seen[r.ID]; s != nil {62 s.finished = true63 }64 }65 }66 f.Close()67 } else if !os.IsNotExist(err) {68 return nil, nil, err69 }70 var open []Unresolved71 tmp := path + ".tmp"72 f, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)73 if err != nil {74 return nil, nil, err75 }76 enc := json.NewEncoder(f)77 for _, id := range order {78 if s := seen[id]; !s.finished {79 open = append(open, Unresolved{ID: id, Type: s.rec.Type, Started: s.rec.TS})80 enc.Encode(s.rec)81 }82 }83 if err := f.Sync(); err != nil {84 f.Close()85 return nil, nil, err86 }87 f.Close()88 if err := os.Rename(tmp, path); err != nil {89 return nil, nil, err90 }91 af, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o600)92 if err != nil {93 return nil, nil, err94 }95 return &Journal{path: path, f: af}, open, nil96}9798func (j *Journal) write(r Record) error {99 b, err := json.Marshal(r)100 if err != nil {101 return err102 }103 j.mu.Lock()104 defer j.mu.Unlock()105 if _, err := j.f.Write(append(b, '\n')); err != nil {106 return err107 }108 return j.f.Sync()109}110111// Started must succeed before a command runs.112func (j *Journal) Started(id, typ string) error {113 return j.write(Record{ID: id, Type: typ, Event: "started", TS: time.Now().UTC()})114}115116// Finished records that execution ended in this process (whatever the result).117func (j *Journal) Finished(id string) error {118 return j.write(Record{ID: id, Event: "finished", TS: time.Now().UTC()})119}120121// Reported records that an unknown outcome was delivered to the server.122func (j *Journal) Reported(id string) error {123 return j.write(Record{ID: id, Event: "reported", TS: time.Now().UTC()})124}125126func (j *Journal) Close() error {127 j.mu.Lock()128 defer j.mu.Unlock()129 return j.f.Close()130}131