Skip to content

Commit

Permalink
Prevent double delays and add some docs (#411)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
svanharmelen committed Apr 13, 2023
1 parent f6746cf commit 6e01a68
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
4 changes: 4 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ exclude_unchanged = true
follow_symlink = true
# This log file places in your tmp_dir.
log = "air.log"
# Poll files for changes instead of using fsnotify.
poll = false
# Poll interval (defaults to the minimum interval of 500ms).
poll_interval = 500 # ms
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 0 # ms
# Stop running old binary when build errors occur.
Expand Down
1 change: 1 addition & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type cfgBuild struct {
ExcludeUnchanged bool `toml:"exclude_unchanged"`
FollowSymlink bool `toml:"follow_symlink"`
Poll bool `toml:"poll"`
PollInterval int `toml:"poll_interval"`
Delay int `toml:"delay"`
StopOnError bool `toml:"stop_on_error"`
SendInterrupt bool `toml:"send_interrupt"`
Expand Down
2 changes: 0 additions & 2 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ func (e *Engine) start() {
e.mainLog("%s has changed", e.config.rel(filename))
case <-firstRunCh:
// go down
break
}

// already build and run now
Expand Down Expand Up @@ -449,7 +448,6 @@ func (e *Engine) runBin() error {
close(e.canExit)
default:
}

}()

killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}, wg *sync.WaitGroup) {
Expand Down
27 changes: 17 additions & 10 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -508,10 +509,7 @@ func checkPortConnectionRefused(port int) bool {
_ = conn.Close()
}
}()
if errors.Is(err, syscall.ECONNREFUSED) {
return true
}
return false
return errors.Is(err, syscall.ECONNREFUSED)
}

func checkPortHaveBeenUsed(port int) bool {
Expand Down Expand Up @@ -572,6 +570,9 @@ func main() {
return err
}
_, err = file.WriteString(code)
if err != nil {
return err
}

// generate go mod file
mod := `module air.sample.com
Expand Down Expand Up @@ -604,6 +605,9 @@ func main() {
return err
}
_, err = file.WriteString(code)
if err != nil {
return err
}

// generate go mod file
mod := `module air.sample.com
Expand Down Expand Up @@ -639,6 +643,9 @@ func main() {
return err
}
_, err = file.WriteString(code)
if err != nil {
return err
}

// generate go mod file
mod := `module air.sample.com
Expand Down Expand Up @@ -688,12 +695,12 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
go func() {
file, err := os.OpenFile("main.go", os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
log.Fatalf("Should not be fail: %s.", err)
}
defer file.Close()
_, err = file.WriteString("\n")
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
log.Fatalf("Should not be fail: %s.", err)
}
}()
err = waitingPortConnectionRefused(t, port, time.Second*10)
Expand Down Expand Up @@ -895,11 +902,11 @@ include_ext = ["sh"]
include_dir = ["nonexist"] # prevent default "." watch from taking effect
include_file = ["main.sh"]
`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
if err := ioutil.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
t.Fatal(err)
}

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

t.Logf("start change main.sh")
go func() {
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0755)
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0o755)
if err != nil {
t.Fatalf("Error updating file: %s.", err)
log.Fatalf("Error updating file: %s.", err)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newLogFunc(colorname string, cfg cfgLog) logFunc {
return func(msg string, v ...interface{}) {
// There are some escape sequences to format color in terminal, so cannot
// just trim new line from right.
msg = strings.Replace(msg, "\n", "", -1)
msg = strings.ReplaceAll(msg, "\n", "")
msg = strings.TrimSpace(msg)
if len(msg) == 0 {
return
Expand Down
4 changes: 2 additions & 2 deletions runner/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func newWatcher(cfg *Config) (filenotify.FileWatcher, error) {
}

// Get the poll interval from the config.
interval := cfg.Build.Delay
interval := cfg.Build.PollInterval

// Configure a minimum poll interval of 500ms.
// Make sure the interval is at least 500ms.
if interval < 500 {
interval = 500
}
Expand Down

0 comments on commit 6e01a68

Please sign in to comment.