-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.go
318 lines (283 loc) · 9.66 KB
/
updater.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
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
package sdk
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
)
type UpdateType string
const (
Timer UpdateType = "timer"
File UpdateType = "file"
Folder UpdateType = "folder"
FileSize UpdateType = "fileSize"
FolderSize UpdateType = "folderSize"
)
func (u *UpdateType) String() string {
return map[UpdateType]string{
Timer: "Timer",
File: "File",
Folder: "Folder",
FileSize: "FileSize",
FolderSize: "FolderSize",
}[*u]
}
type Updater struct {
Name string `json:"name"`
LastTimeStamp int64 `json:"lastTimeStamp"`
LastTotalSize int64 `json:"lastTotalSize"`
Items []UpdaterItem `json:"items"`
}
type UpdaterItem struct {
Path string `json:"path"`
Duration time.Duration `json:"duration"`
Type UpdateType `json:"type"`
}
func (u *Updater) String() string {
bytes, _ := json.MarshalIndent(u, "", " ")
return string(bytes)
}
// NewUpdater creates a new Updater instance. It logs a fatal error if invalid parameters are provided.
func NewUpdater(name string, items []UpdaterItem) (Updater, error) {
if len(items) == 0 {
logger.Fatal("must provide at least one UpdaterItem")
}
now := time.Now()
ret := Updater{
Name: name,
Items: items,
LastTimeStamp: now.Unix(),
LastTotalSize: 0,
}
ret.debugV("NewUpdater created with LastTimeStamp:", ret.LastTimeStamp, "LastTotalSize:", ret.LastTotalSize)
return ret, nil
}
// NeedsUpdate checks if the updater needs to be updated based on the paths or duration.
//
// Summary:
// - If the Timer type has exceeded its duration, it will trigger an update.
// - If the File or Folder types find any file whose modification time is later than LastTimeStamp, it will trigger an update.
// - If the FileSize or FolderSize items notice an increase in the total file size of all FileSize or FolderSize items, it will trigger an update.
//
// Returns:
// - Updater: A potentially updated Updater instance with updated LastTimeStamp and/or LastTotalSize.
// - bool: A boolean indicating whether an update is needed (true) or not (false).
// - error: An error object containing any errors encountered during the update check.
func (u *Updater) NeedsUpdate() (Updater, bool, error) {
var needsUpdate bool
var errs []error
var mostRecentTime int64
var totalSize int64
u.debugV("NeedsUpdate called with LastTimeStamp:", u.LastTimeStamp, "LastTotalSize:", u.LastTotalSize)
for _, item := range u.Items {
switch item.Type {
case Timer:
updated, needed, err := u.needsUpdateTime(item.Duration)
if err != nil {
errs = append(errs, err)
}
if needed {
needsUpdate = true
if updated.LastTimeStamp > mostRecentTime {
mostRecentTime = updated.LastTimeStamp
}
}
case File:
updated, needed, err := u.needsUpdateFiles(item.Path)
if err != nil {
errs = append(errs, err)
}
if needed {
needsUpdate = true
if updated.LastTimeStamp > mostRecentTime {
mostRecentTime = updated.LastTimeStamp
}
}
case Folder:
updated, needed, err := u.needsUpdateFolder(item.Path)
if err != nil {
errs = append(errs, err)
}
if needed {
needsUpdate = true
if updated.LastTimeStamp > mostRecentTime {
mostRecentTime = updated.LastTimeStamp
}
}
case FileSize:
// Skip FileSize type items in this loop
continue
case FolderSize:
// Skip FolderSize type items in this loop
continue
default:
logger.Fatal("unknown path type" + fmt.Sprintf(" %s", item.Type))
return Updater{}, false, errors.New("unknown path type")
}
}
// Handle FileSize and FolderSize type items in a separate loop
for _, item := range u.Items {
switch item.Type {
case FileSize:
fileSize := file.FileSize(item.Path)
totalSize += fileSize
case FolderSize:
if err := walk.ForEveryFileInFolder(item.Path, func(filePath string, _ any) (bool, error) {
fileSize := file.FileSize(filePath)
totalSize += fileSize
return true, nil
}, nil); err != nil {
errs = append(errs, fmt.Errorf("failed to process folder %s: %v", item.Path, err))
}
}
}
u.debugV("Total size calculated:", totalSize, "LastTotalSize:", u.LastTotalSize)
if totalSize != u.LastTotalSize {
u.debug(mark("File size condition met", u.LastTotalSize, totalSize))
needsUpdate = true
}
if needsUpdate {
newUpdater := *u
if mostRecentTime == 0 {
mostRecentTime = time.Now().Unix()
}
newUpdater.LastTimeStamp = mostRecentTime
newUpdater.LastTotalSize = totalSize
u.debugV("Updating LastTimeStamp to:", newUpdater.LastTimeStamp, "and LastTotalSize to:", newUpdater.LastTotalSize)
return newUpdater, true, combineErrors(errs)
}
return *u, false, combineErrors(errs)
}
// needsUpdateTime checks if the specified duration has passed since the last update.
func (u *Updater) needsUpdateTime(duration time.Duration) (Updater, bool, error) {
now := time.Now().Unix()
u.debugV("Current time:", now, "Last update timestamp:", u.LastTimeStamp, "Duration (seconds):", duration.Seconds())
if now-int64(duration.Seconds()) >= u.LastTimeStamp {
u.debug(mark("Duration condition met", u.LastTimeStamp, now))
newUpdater := *u
newUpdater.LastTimeStamp = now
return newUpdater, true, nil
}
u.debugV("Duration condition not met, no update needed")
return *u, false, nil
}
// needsUpdateFiles checks if the file's modification time is more recent than the last update.
func (u *Updater) needsUpdateFiles(path string) (Updater, bool, error) {
u.debugV("Checking files for updates")
modTime, err := file.GetModTime(path)
if err != nil {
return *u, false, fmt.Errorf("failed to get modification time for file %s: %v", path, err)
}
u.debugV("File:", relativize(path), "Modification time:", modTime.Unix())
if modTime.Unix() > u.LastTimeStamp {
u.debug(mark("File time condition met", u.LastTimeStamp, modTime.Unix()))
newUpdater := *u
newUpdater.LastTimeStamp = modTime.Unix()
return newUpdater, true, nil
}
u.debugV("File modification condition not met, no update needed")
return *u, false, nil
}
// needsUpdateFolder checks if any file within the folder has a modification time more recent than the last update.
func (u *Updater) needsUpdateFolder(path string) (Updater, bool, error) {
u.debugV("Checking folders for updates")
var maxLastTs int64
var errs []error
if err := walk.ForEveryFileInFolder(path, func(filePath string, _ any) (bool, error) {
modTime, err := file.GetModTime(filePath)
if err != nil {
errs = append(errs, fmt.Errorf("failed to get modification time for file %s: %v", filePath, err))
return true, nil
}
u.debugV("File in folder:", relativize(filePath), "Modification time:", modTime.Unix())
if modTime.Unix() > maxLastTs {
maxLastTs = modTime.Unix()
}
return true, nil
}, nil); err != nil {
errs = append(errs, fmt.Errorf("failed to process folder %s: %v", path, err))
}
if maxLastTs > u.LastTimeStamp {
u.debug(mark("Folder modification condition met", u.LastTimeStamp, maxLastTs))
newUpdater := *u
newUpdater.LastTimeStamp = maxLastTs
return newUpdater, true, combineErrors(errs)
}
u.debugV("Folder modification condition not met, no update needed")
return *u, false, combineErrors(errs)
}
// combineErrors combines multiple errors into a single error.
func combineErrors(errs []error) error {
if len(errs) == 0 {
return nil
}
errStrings := make([]string, len(errs))
for i, err := range errs {
errStrings[i] = err.Error()
}
return errors.New(strings.Join(errStrings, "\n"))
}
// SetChain updates the paths by replacing an old chain segment with a new one.
func (u *Updater) SetChain(oldChain, newChain string) {
newItems := []UpdaterItem{}
for _, item := range u.Items {
ps := string(os.PathSeparator)
oc := ps + oldChain
nc := ps + newChain
newPath := strings.ReplaceAll(item.Path, oc, nc)
newItems = append(newItems, UpdaterItem{Path: newPath, Duration: item.Duration, Type: item.Type})
}
u.Items = newItems
u.Reset()
}
// Reset sets the LastTimeStamp and LastTotalSize to 0 which causes a reload on the next call to NeedsUpdate.
func (u *Updater) Reset() {
u.debugV("Reset called, setting LastTimeStamp and LastTotalSize to 0")
u.LastTimeStamp = 0
u.LastTotalSize = 0
}
var debugging bool
var debugType = ""
var debugVerbose = false
func init() {
debugType = os.Getenv("TB_DEBUG_UPDATE")
debugging = len(debugType) > 0
debugVerbose = os.Getenv("TB_VERBOSE") == "true"
}
func mark(msg string, t1, t2 int64) string {
return fmt.Sprintf("%s%s: updating ts %d ----> %d%s", colors.BrightRed, msg, t1, t2, colors.Off)
}
func (u *Updater) debug(args ...interface{}) {
if debugging && (debugType == "true" || strings.Contains(debugType, u.Name)) {
head := colors.Green + fmt.Sprintf("%10.10s:", u.Name) + colors.BrightYellow
modifiedArgs := append([]interface{}{head}, args...)
modifiedArgs = append(modifiedArgs, colors.Off)
logger.Info(modifiedArgs...)
}
}
// debugV is a verbose debug function that calls u.debug.
func (u *Updater) debugV(args ...interface{}) {
if debugVerbose {
u.debug(args...)
}
}
// relativize modifies the given path by relativizing it with the specified partial paths.
func relativize(path string) string {
partialPaths := []string{
"/Users/jrush/Data/trueblocks/v1.0.0/cache/",
"/Users/jrush/Data/trueblocks/v1.0.0/unchained/",
"/Users/jrush/Library/Application Support/TrueBlocks/",
}
for _, partialPath := range partialPaths {
if strings.HasPrefix(path, partialPath) {
return "./" + strings.TrimPrefix(path, partialPath)
}
}
return path
}