Skip to content

Commit 93266df

Browse files
feat(watcher): log last changed file (#1447)
* logs last changed file. * Fixes race condition. --------- Co-authored-by: Alliballibaba <[email protected]>
1 parent 6203d20 commit 93266df

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

debugstate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func DebugState() FrankenPHPDebugState {
3737
func threadDebugState(thread *phpThread) ThreadDebugState {
3838
return ThreadDebugState{
3939
Index: thread.threadIndex,
40-
Name: thread.handler.name(),
40+
Name: thread.name(),
4141
State: thread.state.name(),
4242
IsWaiting: thread.state.isInWaitingState(),
4343
IsBusy: !thread.state.isInWaitingState(),

frankenphp_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,12 @@ func TestFailingWorker(t *testing.T) {
672672
}
673673

674674
func TestEnv(t *testing.T) {
675-
testEnv(t, &testOptions{nbParallelRequests:1})
675+
testEnv(t, &testOptions{nbParallelRequests: 1})
676676
}
677677
func TestEnvWorker(t *testing.T) {
678-
testEnv(t, &testOptions{nbParallelRequests:1, workerScript: "env/test-env.php"})
678+
testEnv(t, &testOptions{nbParallelRequests: 1, workerScript: "env/test-env.php"})
679679
}
680+
680681
// testEnv cannot be run in parallel due to https://github.com/golang/go/issues/63567
681682
func testEnv(t *testing.T, opts *testOptions) {
682683
assert.NoError(t, os.Setenv("EMPTY", ""))

internal/watcher/watch_pattern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type watchPattern struct {
1414
dir string
1515
patterns []string
16-
trigger chan struct{}
16+
trigger chan string
1717
failureCount int
1818
}
1919

internal/watcher/watch_pattern_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,14 @@ func TestInValidExtendedPatterns(t *testing.T) {
149149
func TestAnAssociatedEventTriggersTheWatcher(t *testing.T) {
150150
watchPattern, err := parseFilePattern("/**/*.php")
151151
assert.NoError(t, err)
152-
watchPattern.trigger = make(chan struct{})
152+
watchPattern.trigger = make(chan string)
153153

154154
go handleWatcherEvent(watchPattern, "/path/temorary_file", "/path/file.php", 0, 0)
155155

156+
var path string
156157
select {
157-
case <-watchPattern.trigger:
158+
case path = <-watchPattern.trigger:
159+
assert.Equal(t, "/path/file.php", path, "should be associated file path")
158160
case <-time.After(2 * time.Second):
159161
assert.Fail(t, "associated watchPattern did not trigger after 2s")
160162
}

internal/watcher/watcher.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
type watcher struct {
2323
sessions []C.uintptr_t
2424
callback func()
25-
trigger chan struct{}
25+
trigger chan string
2626
stop chan struct{}
2727
}
2828

@@ -105,7 +105,7 @@ func retryWatching(watchPattern *watchPattern) {
105105
}
106106

107107
func (w *watcher) startWatching(filePatterns []string) error {
108-
w.trigger = make(chan struct{})
108+
w.trigger = make(chan string)
109109
w.stop = make(chan struct{})
110110
w.sessions = make([]C.uintptr_t, len(filePatterns))
111111
watchPatterns, err := parseFilePatterns(filePatterns)
@@ -166,37 +166,38 @@ func handleWatcherEvent(watchPattern *watchPattern, path string, associatedPath
166166
}
167167

168168
if watchPattern.allowReload(path, eventType, pathType) {
169-
watchPattern.trigger <- struct{}{}
169+
watchPattern.trigger <- path
170170
return
171171
}
172172

173173
// some editors create temporary files and never actually modify the original file
174174
// so we need to also check the associated path of an event
175175
// see https://github.com/dunglas/frankenphp/issues/1375
176176
if associatedPath != "" && watchPattern.allowReload(associatedPath, eventType, pathType) {
177-
watchPattern.trigger <- struct{}{}
177+
watchPattern.trigger <- associatedPath
178178
}
179179
}
180180

181-
func listenForFileEvents(triggerWatcher chan struct{}, stopWatcher chan struct{}) {
181+
func listenForFileEvents(triggerWatcher chan string, stopWatcher chan struct{}) {
182182
timer := time.NewTimer(debounceDuration)
183183
timer.Stop()
184+
lastChangedFile := ""
184185
defer timer.Stop()
185186
for {
186187
select {
187188
case <-stopWatcher:
188189
break
189-
case <-triggerWatcher:
190+
case lastChangedFile = <-triggerWatcher:
190191
timer.Reset(debounceDuration)
191192
case <-timer.C:
192193
timer.Stop()
194+
logger.Info("filesystem change detected", zap.String("file", lastChangedFile))
193195
scheduleReload()
194196
}
195197
}
196198
}
197199

198200
func scheduleReload() {
199-
logger.Info("filesystem change detected")
200201
reloadWaitGroup.Add(1)
201202
activeWatcher.callback()
202203
reloadWaitGroup.Done()

0 commit comments

Comments
 (0)