-
Notifications
You must be signed in to change notification settings - Fork 9
/
remote.go
233 lines (190 loc) · 6.59 KB
/
remote.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package wsremote
import (
"bytes"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"
"github.com/moby/patternmatcher"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnfs/digestfs"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
"namespacelabs.dev/foundation/internal/wscontents"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/tasks"
)
type Sink interface {
Deposit(context.Context, []*wscontents.FileEvent) (bool, error)
}
// Returns a wscontents.Versioned which will produce a local snapshot as expected
// but forwards all filesystem events (e.g. changes, removals) to the specified sink.
func ObserveAndPush(absPath string, excludes []string, sink Sink, digestMode bool, extraInputs ...compute.UntypedComputable) compute.Computable[any] {
return &observePath{absPath: absPath, excludes: excludes, sink: sink, digestMode: digestMode, extraInputs: extraInputs}
}
type observePath struct {
absPath string
excludes []string
sink Sink
digestMode bool
extraInputs []compute.UntypedComputable
compute.LocalScoped[any]
}
func (op *observePath) Action() *tasks.ActionEvent {
return tasks.Action("web.contents.observe")
}
func (op *observePath) Inputs() *compute.In {
in := compute.Inputs().Str("absPath", op.absPath).Indigestible("not cacheable", "true")
for k, extra := range op.extraInputs {
in = in.Computable(fmt.Sprintf("extra:%d", k), extra)
}
return in
}
func (op *observePath) Compute(ctx context.Context, _ compute.Resolved) (any, error) {
fmt.Fprintf(console.Debug(ctx), "wsremote: starting w/ snapshotting %q (excludes: %v)\n", op.absPath, op.excludes)
excludeMatcher, err := patternmatcher.New(op.excludes)
if err != nil {
return nil, err
}
snapshot, err := wscontents.SnapshotDirectory(ctx, op.absPath, excludeMatcher, op.digestMode)
if err != nil {
return nil, err
}
return localObserver{absPath: op.absPath, excludeMatcher: excludeMatcher, digestMode: op.digestMode, snapshot: snapshot, sink: op.sink}, nil
}
type localObserver struct {
absPath string
excludeMatcher *patternmatcher.PatternMatcher
digestMode bool
snapshot *memfs.FS
sink Sink
}
func (lo localObserver) Abs() string { return lo.absPath }
func (lo localObserver) FS() fs.FS { return lo.snapshot }
func (lo localObserver) ComputeDigest(ctx context.Context) (schema.Digest, error) {
return digestfs.Digest(ctx, lo.snapshot)
}
func (lo localObserver) Observe(ctx context.Context, onChange func(compute.ResultWithTimestamp[any], compute.ObserveNote)) (func(), error) {
// XXX we're doing polling for correctness; this needs to use filewatcher.
// This observer is special; if we know that the scheduler wants to observe
// the graph, then we trigger a syncing of local files to the destination
// sink. We don't actually ever emit a new snapshot.
closeCh := make(chan struct{})
last := lo.snapshot
go func() {
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-closeCh:
return
case <-t.C:
newSnapshot, deposited, err := checkSnapshot(ctx, last, lo.absPath, lo.excludeMatcher, lo.digestMode, lo.sink)
if err != nil {
fmt.Fprintf(console.Errors(ctx), "FileSync failed while snapshotting %q: %v\n", lo.absPath, err)
return
}
if !deposited {
r := compute.ResultWithTimestamp[any]{
Completed: time.Now(),
}
r.Value = localObserver{absPath: lo.absPath, snapshot: newSnapshot, sink: lo.sink}
onChange(r, compute.ObserveContinuing)
}
last = newSnapshot
}
}
}()
return func() { close(closeCh) }, nil
}
func checkSnapshot(ctx context.Context, previous *memfs.FS, absPath string, excludeMatcher *patternmatcher.PatternMatcher, digestMode bool, sink Sink) (*memfs.FS, bool, error) {
newSnapshot, err := wscontents.SnapshotDirectory(ctx, absPath, excludeMatcher, digestMode)
if err != nil {
return nil, false, err
}
// First we iterate over all files in the new snapshot. This index will be
// used to signal which files are actually new, and which files were removed
// from the old snapshot.
newFiles := map[string]bytestream.Static{}
newFilesModes := map[string]memfs.FileDirent{}
if err := newSnapshot.VisitFilesWithoutContext(func(path string, bs bytestream.Static, de memfs.FileDirent) error {
newFiles[path] = bs
newFilesModes[path] = de
return nil
}); err != nil {
return nil, false, err
}
var events []*wscontents.FileEvent
if err := previous.VisitFilesWithoutContext(func(path string, bs bytestream.Static, _ memfs.FileDirent) error {
if newFile, ok := newFiles[path]; !ok {
events = append(events, &wscontents.FileEvent{Path: path, Event: wscontents.FileEvent_REMOVE})
delete(newFiles, path)
} else {
if bytes.Equal(bs.Contents, newFile.Contents) {
// No changes, don't re-write.
delete(newFiles, path)
}
}
return nil
}); err != nil {
return nil, false, err
}
m := checkMkdir{previous: previous, newdirs: map[string]struct{}{}}
for filename, contents := range newFiles {
if err := m.check(filepath.Dir(filename)); err != nil {
return nil, false, err
}
ev := &wscontents.FileEvent{
Event: wscontents.FileEvent_WRITE,
Path: filename,
Mode: uint32(newFilesModes[filename].FileMode.Perm()),
}
if !digestMode {
ev.NewContents = contents.Contents
}
events = append(events, ev)
}
// Mkdirs come first.
events = append(m.events, events...)
var deposited bool
var depositErr error
if len(events) > 0 {
deposited, depositErr = sink.Deposit(ctx, events)
} else {
// If there are no changes consider everything done
// and avoid generating a new version in Observe() for nothing.
deposited = true
}
return newSnapshot, deposited, depositErr
}
type checkMkdir struct {
previous *memfs.FS
newdirs map[string]struct{}
events []*wscontents.FileEvent
}
// XXX this doesn't handle the case where a directory becomes a file, or vice-versa.
func (m *checkMkdir) check(dir string) error {
if dir == "." {
return nil
}
if err := m.check(filepath.Dir(dir)); err != nil {
return err
}
if _, ok := m.newdirs[dir]; ok {
return nil
}
if _, err := m.previous.Open(dir); err != nil {
if !os.IsNotExist(err) {
return err
}
m.events = append(m.events, &wscontents.FileEvent{Path: dir, Event: wscontents.FileEvent_MKDIR})
m.newdirs[dir] = struct{}{}
}
return nil
}