Skip to content
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

Prevent double delays and add some docs #411

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
cosmtrek marked this conversation as resolved.
Show resolved Hide resolved
}
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 {
cosmtrek marked this conversation as resolved.
Show resolved Hide resolved
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