Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
4a70260ea5
|
|||
|
a62bfdb01d
|
49
main.go
49
main.go
@@ -8,6 +8,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
@@ -194,6 +196,11 @@ func (s *Script) Stop(lock bool) error {
|
||||
signal = syscall.SIGKILL
|
||||
}
|
||||
|
||||
// make sure the process is not dead
|
||||
if s.command.ProcessState != nil && s.command.ProcessState.Exited() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := syscall.Kill(-s.command.Process.Pid, signal); err != nil {
|
||||
log.Printf("error killing previous process: %v", err)
|
||||
return err
|
||||
@@ -226,6 +233,8 @@ func (s *Script) Stop(lock bool) error {
|
||||
}
|
||||
|
||||
func (s *Script) Restart() error {
|
||||
slog.Debug("Restarting script", "script", s.scriptName)
|
||||
|
||||
s.mutex.Lock()
|
||||
|
||||
err := s.Stop(false)
|
||||
@@ -320,6 +329,9 @@ func (zqdgr *ZQDGR) loadConfig() error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
var debugMode bool
|
||||
|
||||
noWs := flag.Bool("no-ws", false, "Disable WebSocket server")
|
||||
configDir := flag.String("config", ".", "Path to the config directory")
|
||||
disableReloadConfig := flag.Bool("no-reload-config", false, "Do not restart ZQDGR on config file change")
|
||||
@@ -327,6 +339,18 @@ func main() {
|
||||
|
||||
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
|
||||
os.Args = flag.Args()
|
||||
|
||||
@@ -520,19 +544,7 @@ func main() {
|
||||
|
||||
log.Println("Received signal, exiting...")
|
||||
if script.command != nil {
|
||||
var signal syscall.Signal
|
||||
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)
|
||||
script.Stop(true)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
@@ -640,15 +652,15 @@ func main() {
|
||||
|
||||
if !ok {
|
||||
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 os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
fmt.Println("File changed:", event.Name)
|
||||
}
|
||||
slog.Debug("File changed", "file", event.Name)
|
||||
|
||||
if strings.HasSuffix(event.Name, "zqdgr.config.json") {
|
||||
// re-exec the exact same command
|
||||
if !*disableReloadConfig {
|
||||
log.Println("zqdgr.config.json has changed, restarting...")
|
||||
fmt.Println("zqdgr.config.json has changed, restarting...")
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -668,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)
|
||||
}
|
||||
|
||||
|
||||
48
watcher.go
48
watcher.go
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -34,7 +34,9 @@ func (g *globList) Matches(value string) bool {
|
||||
|
||||
func matchesPattern(pattern []string, path string) bool {
|
||||
for _, p := range pattern {
|
||||
slog.Debug("checking path against pattern", "pattern", p, "path", path)
|
||||
if matched, _ := doublestar.Match(p, path); matched {
|
||||
slog.Debug("path matches pattern", "pattern", p, "path", path)
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -42,17 +44,13 @@ func matchesPattern(pattern []string, path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
|
||||
func pathShouldBeTracked(cfg *WatcherConfig, path string) bool {
|
||||
base := filepath.Dir(path)
|
||||
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("checking %s against %s %v\n", path, base, *cfg)
|
||||
}
|
||||
slog.Debug("checking file against path", "path", path, "base", base)
|
||||
|
||||
if cfg.excludedGlobs.Matches(base) {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("%s is excluded\n", base)
|
||||
}
|
||||
slog.Debug("file is excluded", "path", path)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -86,9 +84,7 @@ func (n NotifyWatcher) Close() error {
|
||||
}
|
||||
|
||||
func (n NotifyWatcher) AddFile(path string) error {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("manually adding file\n")
|
||||
}
|
||||
slog.Debug("manually adding file", "file", path)
|
||||
|
||||
return n.add(path)
|
||||
}
|
||||
@@ -125,24 +121,36 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
|
||||
func addFiles(fw FileWatcher) error {
|
||||
cfg := fw.getConfig()
|
||||
for _, pattern := range cfg.pattern {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
fmt.Printf("processing glob %s\n", pattern)
|
||||
}
|
||||
slog.Debug("adding pattern", "pattern", pattern)
|
||||
|
||||
matches, err := doublestar.Glob(pattern)
|
||||
if err != nil {
|
||||
log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error())
|
||||
}
|
||||
|
||||
trackedDirs := make(map[string]bool)
|
||||
for _, match := range matches {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("checking %s\n", match)
|
||||
base := filepath.Dir(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
|
||||
}
|
||||
|
||||
trackedDirs[base] = true
|
||||
|
||||
slog.Debug("adding directory", "dir", base)
|
||||
|
||||
if err := fw.add(base); err != nil {
|
||||
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if directoryShouldBeTracked(cfg, match) {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("%s is not excluded\n", match)
|
||||
}
|
||||
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 {
|
||||
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "zqdgr",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"description": "zqdgr is a quick and dirty Golang runner",
|
||||
"author": "juls0730",
|
||||
"license": "BSL-1.0",
|
||||
|
||||
Reference in New Issue
Block a user