Better debugging, and several bug fixes
This commit brings a greater experience for developers working on ZQDGR, and also fixes a number of bugs. The following bugs have been fixed: - Scripts that died in watch mode did not restart once changes were made - Created and deleted files did not cause a reload - script.Stop is now used in every place where we kill the process, this ensures that the process is *eventually* actually killed
This commit is contained in:
44
main.go
44
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -231,6 +233,8 @@ func (s *Script) Stop(lock bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Script) Restart() error {
|
func (s *Script) Restart() error {
|
||||||
|
slog.Debug("Restarting script", "script", s.scriptName)
|
||||||
|
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
|
|
||||||
err := s.Stop(false)
|
err := s.Stop(false)
|
||||||
@@ -325,6 +329,9 @@ func (zqdgr *ZQDGR) loadConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var err error
|
||||||
|
var debugMode bool
|
||||||
|
|
||||||
noWs := flag.Bool("no-ws", false, "Disable WebSocket server")
|
noWs := flag.Bool("no-ws", false, "Disable WebSocket server")
|
||||||
configDir := flag.String("config", ".", "Path to the config directory")
|
configDir := flag.String("config", ".", "Path to the config directory")
|
||||||
disableReloadConfig := flag.Bool("no-reload-config", false, "Do not restart ZQDGR on config file change")
|
disableReloadConfig := flag.Bool("no-reload-config", false, "Do not restart ZQDGR on config file change")
|
||||||
@@ -332,6 +339,18 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
debugModeVal, ok := os.LookupEnv("ZQDGR_DEBUG")
|
||||||
|
if ok {
|
||||||
|
debugMode, err = strconv.ParseBool(debugModeVal)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if debugMode {
|
||||||
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
originalArgs := os.Args
|
originalArgs := os.Args
|
||||||
os.Args = flag.Args()
|
os.Args = flag.Args()
|
||||||
|
|
||||||
@@ -525,19 +544,7 @@ func main() {
|
|||||||
|
|
||||||
log.Println("Received signal, exiting...")
|
log.Println("Received signal, exiting...")
|
||||||
if script.command != nil {
|
if script.command != nil {
|
||||||
var signal syscall.Signal
|
script.Stop(true)
|
||||||
switch zqdgr.Config.ShutdownSignal {
|
|
||||||
case "SIGINT":
|
|
||||||
signal = syscall.SIGINT
|
|
||||||
case "SIGTERM":
|
|
||||||
signal = syscall.SIGTERM
|
|
||||||
case "SIGQUIT":
|
|
||||||
signal = syscall.SIGQUIT
|
|
||||||
default:
|
|
||||||
signal = syscall.SIGKILL
|
|
||||||
}
|
|
||||||
|
|
||||||
syscall.Kill(-script.command.Process.Pid, signal)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
@@ -645,15 +652,15 @@ func main() {
|
|||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
timer = time.AfterFunc(waitFor, func() {
|
timer = time.AfterFunc(waitFor, func() {
|
||||||
|
slog.Debug("FSnotify event received", "event", event)
|
||||||
|
|
||||||
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
slog.Debug("File changed", "file", event.Name)
|
||||||
fmt.Println("File changed:", event.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(event.Name, "zqdgr.config.json") {
|
if strings.HasSuffix(event.Name, "zqdgr.config.json") {
|
||||||
// re-exec the exact same command
|
// re-exec the exact same command
|
||||||
if !*disableReloadConfig {
|
if !*disableReloadConfig {
|
||||||
log.Println("zqdgr.config.json has changed, restarting...")
|
fmt.Println("zqdgr.config.json has changed, restarting...")
|
||||||
executable, err := os.Executable()
|
executable, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -673,7 +680,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if directoryShouldBeTracked(&watcherConfig, event.Name) {
|
if pathShouldBeTracked(&watcherConfig, event.Name) && event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
|
slog.Debug("Adding new file to watcher", "file", event.Name)
|
||||||
watcher.(NotifyWatcher).watcher.Add(event.Name)
|
watcher.(NotifyWatcher).watcher.Add(event.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
46
watcher.go
46
watcher.go
@@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -34,7 +34,9 @@ func (g *globList) Matches(value string) bool {
|
|||||||
|
|
||||||
func matchesPattern(pattern []string, path string) bool {
|
func matchesPattern(pattern []string, path string) bool {
|
||||||
for _, p := range pattern {
|
for _, p := range pattern {
|
||||||
|
slog.Debug("checking path against pattern", "pattern", p, "path", path)
|
||||||
if matched, _ := doublestar.Match(p, path); matched {
|
if matched, _ := doublestar.Match(p, path); matched {
|
||||||
|
slog.Debug("path matches pattern", "pattern", p, "path", path)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,17 +44,13 @@ func matchesPattern(pattern []string, path string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
|
func pathShouldBeTracked(cfg *WatcherConfig, path string) bool {
|
||||||
base := filepath.Dir(path)
|
base := filepath.Dir(path)
|
||||||
|
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
slog.Debug("checking file against path", "path", path, "base", base)
|
||||||
log.Printf("checking %s against %s %v\n", path, base, *cfg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.excludedGlobs.Matches(base) {
|
if cfg.excludedGlobs.Matches(base) {
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
slog.Debug("file is excluded", "path", path)
|
||||||
log.Printf("%s is excluded\n", base)
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,9 +84,7 @@ func (n NotifyWatcher) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n NotifyWatcher) AddFile(path string) error {
|
func (n NotifyWatcher) AddFile(path string) error {
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
slog.Debug("manually adding file", "file", path)
|
||||||
log.Printf("manually adding file\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
return n.add(path)
|
return n.add(path)
|
||||||
}
|
}
|
||||||
@@ -125,24 +121,36 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
|
|||||||
func addFiles(fw FileWatcher) error {
|
func addFiles(fw FileWatcher) error {
|
||||||
cfg := fw.getConfig()
|
cfg := fw.getConfig()
|
||||||
for _, pattern := range cfg.pattern {
|
for _, pattern := range cfg.pattern {
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
slog.Debug("adding pattern", "pattern", pattern)
|
||||||
fmt.Printf("processing glob %s\n", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
matches, err := doublestar.Glob(pattern)
|
matches, err := doublestar.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error())
|
log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trackedDirs := make(map[string]bool)
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
base := filepath.Dir(match)
|
||||||
log.Printf("checking %s\n", match)
|
// this allows us to track file creations and deletions
|
||||||
|
if !trackedDirs[base] {
|
||||||
|
if cfg.excludedGlobs.Matches(base) {
|
||||||
|
slog.Debug("directory is excluded", "file", match)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if directoryShouldBeTracked(cfg, match) {
|
trackedDirs[base] = true
|
||||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
|
||||||
log.Printf("%s is not excluded\n", match)
|
slog.Debug("adding directory", "dir", base)
|
||||||
|
|
||||||
|
if err := fw.add(base); err != nil {
|
||||||
|
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("adding file", "file", match)
|
||||||
|
|
||||||
|
if pathShouldBeTracked(cfg, match) {
|
||||||
|
slog.Debug("path should be tracked", "file", match)
|
||||||
|
|
||||||
if err := fw.add(match); err != nil {
|
if err := fw.add(match); err != nil {
|
||||||
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "zqdgr",
|
"name": "zqdgr",
|
||||||
"version": "0.0.4a",
|
"version": "0.0.5",
|
||||||
"description": "zqdgr is a quick and dirty Golang runner",
|
"description": "zqdgr is a quick and dirty Golang runner",
|
||||||
"author": "juls0730",
|
"author": "juls0730",
|
||||||
"license": "BSL-1.0",
|
"license": "BSL-1.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user