-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
177 lines (165 loc) · 3.63 KB
/
journal.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
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
package watched
import (
"bufio"
"bytes"
"context"
"errors"
"os"
"path/filepath"
"sync/atomic"
"git.fractalqb.de/fractalqb/qblog"
"git.fractalqb.de/fractalqb/sllm/v3"
"github.com/rjeczalik/notify"
"github.com/CmdrVasquess/watched/internal"
)
type Journal struct {
recv EventRecv
jdir string
serIndep []string
stop chan internal.StopEvent
runs int32
}
type JournalOptions struct {
SerialIndependent []string
}
func NewJournal(dir string, r EventRecv, opt *JournalOptions) *Journal {
res := &Journal{
recv: r,
jdir: dir,
stop: make(chan internal.StopEvent),
}
if opt != nil {
res.serIndep = opt.SerialIndependent
}
return res
}
func (ede *Journal) Start() (err error) {
if !atomic.CompareAndSwapInt32(&ede.runs, 0, 1) {
if atomic.LoadInt32(&ede.runs) > 0 {
return errors.New("jdir events already running")
}
return errors.New("cannot restart stopped jdir events")
}
log.Info("Start watching files in `dir`", `dir`, ede.jdir)
fsevents := make(chan notify.EventInfo, 32) // TODO eliminagte magic number
if err := notify.Watch(ede.jdir, fsevents, notify.Write); err != nil {
return err
}
var jfile *os.File
defer func() {
if err != nil {
log.Error(err.Error())
}
if fsevents != nil {
notify.Stop(fsevents)
}
if jfile != nil {
if err := jfile.Close(); err != nil {
log.Error("journal `file` close `error`",
`file`, jfile,
`error`, err,
)
}
}
close(ede.stop)
log.Info("Stopped watching files in `dir`", `dir`, ede.jdir)
}()
var (
jfileName string
jeventNo int
)
EVENT_LOOP:
for {
select {
case <-ede.stop:
break EVENT_LOOP
case e := <-fsevents:
log.Trace("FS `event`", `event`, e)
fileNm := filepath.Base(e.Path())
if IsJournalFile(fileNm) != 0 {
if fileNm != jfileName {
if jfile != nil {
if err := jfile.Close(); err != nil {
log.Error("journal `file` close `error`",
`file`, jfile,
`error`, err,
)
}
}
if jfile, err = os.Open(e.Path()); err != nil {
jfileName = ""
log.Error("journal `file` open `error`",
`file`, fileNm,
`error`, err,
)
continue
}
jfileName = fileNm
jeventNo = 0
}
jscan := bufio.NewScanner(jfile)
for jscan.Scan() {
data := bytes.TrimSpace(jscan.Bytes())
if len(data) > 0 {
if log.Enabled(context.Background(), qblog.LevelTrace) {
log.Trace("journal `data`", `data`, string(data))
}
jeventNo++
err = ede.recv.OnJournalEvent(JounalEvent{
File: jfileName,
EventNo: jeventNo,
Event: data,
})
if err != nil {
log.Error("`journal` `event` `error`",
`journal`, jfileName,
`event`, jeventNo,
`error`, err.Error(),
)
}
}
}
} else if sft := IsStatusFile(fileNm); sft > 0 {
err = ede.onStatus(sft, e.Path())
if err != nil {
log.Error("`status` `error`",
`status`, fileNm,
`error`, err.Error(),
)
}
} else {
log.Trace("Ignore FS event `on`", `on`, fileNm)
}
}
}
notify.Stop(fsevents)
fsevents = nil
return nil
}
func (ede *Journal) Stop() {
if !atomic.CompareAndSwapInt32(&ede.runs, 1, -1) {
return
}
ede.stop <- internal.StopEvent{}
<-ede.stop
}
func (ede *Journal) onStatus(t StatusType, file string) error {
raw, err := os.ReadFile(file)
if err != nil {
return sllm.ErrorIdx("`error` reading `file`", err, file)
}
for i, c := range raw {
switch c {
case '\n', '\r':
raw[i] = ' '
}
}
raw = bytes.TrimSpace(raw)
if len(raw) > 0 {
return ede.recv.OnStatusEvent(StatusEvent{
Type: t,
Event: raw,
})
}
return nil
}