8000 feat(agent/agentcontainers): add file watcher and dirty status by mafredri · Pull Request #17573 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(agent/agentcontainers): add file watcher and dirty status #17573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix pr suggestions and improve start loop
  • Loading branch information
mafredri committed Apr 28, 2025
commit aed7c3023cee9dd12ebd18de4f030afb0e81092b
6 changes: 5 additions & 1 deletion agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,14 @@ func (api *API) start() {
for {
event, err := api.watcher.Next(api.ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
if errors.Is(err, watcher.ErrWatcherClosed) {
api.logger.Debug(api.ctx, "watcher closed")
return
}
if api.ctx.Err() != nil {
api.logger.Debug(api.ctx, "api context canceled")
return
}
api.logger.Error(api.ctx, "watcher error waiting for next event", slog.Error(err))
continue
}
Expand Down
20 changes: 15 additions & 5 deletions agent/agentcontainers/watcher/watcher.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this package could be moved outside of agentcontainers for later re-use, but that doesn't have to be done in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a valid suggestion, but I'd like to defer until the need arises to keep devcontainer related stuff contained (heh).

8000
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"golang.org/x/xerrors"
)

var ErrWatcherClosed = xerrors.New("watcher closed")

// Watcher defines an interface for monitoring file system changes.
// Implementations track file modifications and provide an event stream
// that clients can consume to react to changes.
Expand Down Expand Up @@ -63,28 +65,36 @@ func (f *fsnotifyWatcher) Remove(file string) error {
return nil
}

func (f *fsnotifyWatcher) Next(ctx context.Context) (*fsnotify.Event, error) {
func (f *fsnotifyWatcher) Next(ctx context.Context) (event *fsnotify.Event, err error) {
defer func() {
if ctx.Err() != nil {
event = nil
err = ctx.Err()
}
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case event, ok := <-f.Events:
if !ok {
return nil, xerrors.New("watcher closed")
return nil, ErrWatcherClosed
}
return &event, nil
case err, ok := <-f.Errors:
if !ok {
return nil, xerrors.New("watcher closed")
return nil, ErrWatcherClosed
}
return nil, xerrors.Errorf("watcher error: %w", err)
case <-f.closed:
return nil, xerrors.New("watcher closed")
return nil, ErrWatcherClosed
}
}

func (f *fsnotifyWatcher) Close() (err error) {
err = ErrWatcherClosed
f.closeOnce.Do(func() {
if err := f.Watcher.Close(); err != nil {
if err = f.Watcher.Close(); err != nil {
err = xerrors.Errorf("close watcher: %w", err)
}
close(f.closed)
Expand Down
Loading
0