Skip to content

Commit 6e01a68

Browse files
authored
Prevent double delays and add some docs (#411)
* Fix a few linter warnings * Prevent double delays and add some docs I just noticed that reusing `delay` for the poll interval will cause an unintended double delay (first the delay is used as interval, and when a change happened delay is used to wait before building after the change). Adding a dedicated `poll_interval` parameter allows people to prevent that and apply a more fine-grained configuration. Also added the new params to the example toml file with some docs which I forgot to do in the initial PR.
1 parent f6746cf commit 6e01a68

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

air_example.toml

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ exclude_unchanged = true
3030
follow_symlink = true
3131
# This log file places in your tmp_dir.
3232
log = "air.log"
33+
# Poll files for changes instead of using fsnotify.
34+
poll = false
35+
# Poll interval (defaults to the minimum interval of 500ms).
36+
poll_interval = 500 # ms
3337
# It's not necessary to trigger build each time file changes if it's too frequent.
3438
delay = 0 # ms
3539
# Stop running old binary when build errors occur.

runner/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type cfgBuild struct {
4848
ExcludeUnchanged bool `toml:"exclude_unchanged"`
4949
FollowSymlink bool `toml:"follow_symlink"`
5050
Poll bool `toml:"poll"`
51+
PollInterval int `toml:"poll_interval"`
5152
Delay int `toml:"delay"`
5253
StopOnError bool `toml:"stop_on_error"`
5354
SendInterrupt bool `toml:"send_interrupt"`

runner/engine.go

-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ func (e *Engine) start() {
345345
e.mainLog("%s has changed", e.config.rel(filename))
346346
case <-firstRunCh:
347347
// go down
348-
break
349348
}
350349

351350
// already build and run now
@@ -449,7 +448,6 @@ func (e *Engine) runBin() error {
449448
close(e.canExit)
450449
default:
451450
}
452-
453451
}()
454452

455453
killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}, wg *sync.WaitGroup) {

runner/engine_test.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io/ioutil"
7+
"log"
78
"net"
89
"os"
910
"os/exec"
@@ -508,10 +509,7 @@ func checkPortConnectionRefused(port int) bool {
508509
_ = conn.Close()
509510
}
510511
}()
511-
if errors.Is(err, syscall.ECONNREFUSED) {
512-
return true
513-
}
514-
return false
512+
return errors.Is(err, syscall.ECONNREFUSED)
515513
}
516514

517515
func checkPortHaveBeenUsed(port int) bool {
@@ -572,6 +570,9 @@ func main() {
572570
return err
573571
}
574572
_, err = file.WriteString(code)
573+
if err != nil {
574+
return err
575+
}
575576

576577
// generate go mod file
577578
mod := `module air.sample.com
@@ -604,6 +605,9 @@ func main() {
604605
return err
605606
}
606607
_, err = file.WriteString(code)
608+
if err != nil {
609+
return err
610+
}
607611

608612
// generate go mod file
609613
mod := `module air.sample.com
@@ -639,6 +643,9 @@ func main() {
639643
return err
640644
}
641645
_, err = file.WriteString(code)
646+
if err != nil {
647+
return err
648+
}
642649

643650
// generate go mod file
644651
mod := `module air.sample.com
@@ -688,12 +695,12 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
688695
go func() {
689696
file, err := os.OpenFile("main.go", os.O_APPEND|os.O_WRONLY, 0o644)
690697
if err != nil {
691-
t.Fatalf("Should not be fail: %s.", err)
698+
log.Fatalf("Should not be fail: %s.", err)
692699
}
693700
defer file.Close()
694701
_, err = file.WriteString("\n")
695702
if err != nil {
696-
t.Fatalf("Should not be fail: %s.", err)
703+
log.Fatalf("Should not be fail: %s.", err)
697704
}
698705
}()
699706
err = waitingPortConnectionRefused(t, port, time.Second*10)
@@ -895,11 +902,11 @@ include_ext = ["sh"]
895902
include_dir = ["nonexist"] # prevent default "." watch from taking effect
896903
include_file = ["main.sh"]
897904
`
898-
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
905+
if err := ioutil.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
899906
t.Fatal(err)
900907
}
901908

902-
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf original > output"), 0755)
909+
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf original > output"), 0o755)
903910
if err != nil {
904911
t.Fatal(err)
905912
}
@@ -922,9 +929,9 @@ include_file = ["main.sh"]
922929

923930
t.Logf("start change main.sh")
924931
go func() {
925-
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0755)
932+
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0o755)
926933
if err != nil {
927-
t.Fatalf("Error updating file: %s.", err)
934+
log.Fatalf("Error updating file: %s.", err)
928935
}
929936
}()
930937

runner/logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newLogFunc(colorname string, cfg cfgLog) logFunc {
5353
return func(msg string, v ...interface{}) {
5454
// There are some escape sequences to format color in terminal, so cannot
5555
// just trim new line from right.
56-
msg = strings.Replace(msg, "\n", "", -1)
56+
msg = strings.ReplaceAll(msg, "\n", "")
5757
msg = strings.TrimSpace(msg)
5858
if len(msg) == 0 {
5959
return

runner/watcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func newWatcher(cfg *Config) (filenotify.FileWatcher, error) {
1212
}
1313

1414
// Get the poll interval from the config.
15-
interval := cfg.Build.Delay
15+
interval := cfg.Build.PollInterval
1616

17-
// Configure a minimum poll interval of 500ms.
17+
// Make sure the interval is at least 500ms.
1818
if interval < 500 {
1919
interval = 500
2020
}

0 commit comments

Comments
 (0)