From 3d18e160152b36e8b45c092c623f62b1170db6d0 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sat, 11 Dec 2021 22:56:48 +0100 Subject: [PATCH] longer timeouts, various improvements --- cmd/daemon/main.go | 13 +++++++------ cmd/daemon/run-server.go | 5 ++++- cmd/daemon/status.go | 8 +++++--- cmd/inspect/main.go | 5 ++++- pkg/collect/collect.go | 5 +++-- pkg/httpclient/httpclient.go | 2 +- pkg/signalhandler/signalhander.go | 6 +++--- scripts/reshctl.sh | 5 ++--- scripts/widgets.sh | 5 +++-- 9 files changed, 32 insertions(+), 22 deletions(-) diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index 2fefcbf..fc7b5bd 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -38,7 +38,7 @@ func main() { f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { - log.Fatal("Error opening file:", err) + log.Fatalf("Error opening file: %v\n", err) } defer f.Close() @@ -47,7 +47,7 @@ func main() { var config cfg.Config if _, err := toml.DecodeFile(configPath, &config); err != nil { - log.Println("Error reading config", err) + log.Printf("Error reading config: %v\n", err) return } if config.Debug { @@ -57,7 +57,8 @@ func main() { res, err := isDaemonRunning(config.Port) if err != nil { - log.Println("Error while checking if the daemon is runnnig", err) + log.Printf("Error while checking if the daemon is runnnig"+ + " - it's probably not running: %v\n", err) } if res { log.Println("Daemon is already running - exiting!") @@ -69,18 +70,18 @@ func main() { // kill daemon err = killDaemon(pidfilePath) if err != nil { - log.Println("Error while killing daemon", err) + log.Printf("Error while killing daemon: %v\n", err) } } err = ioutil.WriteFile(pidfilePath, []byte(strconv.Itoa(os.Getpid())), 0644) if err != nil { - log.Fatal("Could not create pidfile", err) + log.Fatalf("Could not create pidfile: %v\n", err) } runServer(config, reshHistoryPath, bashHistoryPath, zshHistoryPath) log.Println("main: Removing pidfile ...") err = os.Remove(pidfilePath) if err != nil { - log.Println("Could not delete pidfile", err) + log.Printf("Could not delete pidfile: %v\n", err) } log.Println("main: Shutdown - bye") } diff --git a/cmd/daemon/run-server.go b/cmd/daemon/run-server.go index 1dba979..fc70825 100644 --- a/cmd/daemon/run-server.go +++ b/cmd/daemon/run-server.go @@ -64,7 +64,10 @@ func runServer(config cfg.Config, reshHistoryPath, bashHistoryPath, zshHistoryPa mux.Handle("/inspect", &inspectHandler{sesshistDispatch: sesshistDispatch}) mux.Handle("/dump", &dumpHandler{histfileBox: histfileBox}) - server := &http.Server{Addr: "localhost:" + strconv.Itoa(config.Port), Handler: mux} + server := &http.Server{ + Addr: "localhost:" + strconv.Itoa(config.Port), + Handler: mux, + } go server.ListenAndServe() // signalhandler - takes over the main goroutine so when signal handler exists the whole program exits diff --git a/cmd/daemon/status.go b/cmd/daemon/status.go index f824549..599b8dd 100644 --- a/cmd/daemon/status.go +++ b/cmd/daemon/status.go @@ -6,6 +6,7 @@ import ( "net/http" "strconv" + "github.com/curusarn/resh/pkg/httpclient" "github.com/curusarn/resh/pkg/msg" ) @@ -28,10 +29,11 @@ func statusHandler(w http.ResponseWriter, r *http.Request) { func isDaemonRunning(port int) (bool, error) { url := "http://localhost:" + strconv.Itoa(port) + "/status" - resp, err := http.Get(url) + client := httpclient.New() + resp, err := client.Get(url) if err != nil { - log.Println("Error while checking daemon status - "+ - "it's probably not running!", err) + log.Printf("Error while checking daemon status - "+ + "it's probably not running: %v\n", err) return false, err } defer resp.Body.Close() diff --git a/cmd/inspect/main.go b/cmd/inspect/main.go index 6474ed3..7b76a40 100644 --- a/cmd/inspect/main.go +++ b/cmd/inspect/main.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "time" "github.com/BurntSushi/toml" "github.com/curusarn/resh/pkg/cfg" @@ -63,7 +64,9 @@ func SendInspectMsg(m msg.InspectMsg, port string) msg.MultiResponse { } req.Header.Set("Content-Type", "application/json") - client := &http.Client{} + client := http.Client{ + Timeout: 3 * time.Second, + } resp, err := client.Do(req) if err != nil { log.Fatal("resh-daemon is not running - try restarting this terminal") diff --git a/pkg/collect/collect.go b/pkg/collect/collect.go index 685b544..5fd849b 100644 --- a/pkg/collect/collect.go +++ b/pkg/collect/collect.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + "github.com/curusarn/resh/pkg/httpclient" "github.com/curusarn/resh/pkg/records" ) @@ -33,7 +34,7 @@ func SendRecallRequest(r records.SlimRecord, port string) (string, bool) { } req.Header.Set("Content-Type", "application/json") - client := &http.Client{} + client := httpclient.New() resp, err := client.Do(req) if err != nil { log.Fatal("resh-daemon is not running - try restarting this terminal") @@ -68,7 +69,7 @@ func SendRecord(r records.Record, port, path string) { } req.Header.Set("Content-Type", "application/json") - client := &http.Client{} + client := httpclient.New() _, err = client.Do(req) if err != nil { log.Fatal("resh-daemon is not running - try restarting this terminal") diff --git a/pkg/httpclient/httpclient.go b/pkg/httpclient/httpclient.go index c2d1702..1e7fb8f 100644 --- a/pkg/httpclient/httpclient.go +++ b/pkg/httpclient/httpclient.go @@ -7,6 +7,6 @@ import ( func New() *http.Client { return &http.Client{ - Timeout: 100 * time.Millisecond, + Timeout: 500 * time.Millisecond, } } diff --git a/pkg/signalhandler/signalhander.go b/pkg/signalhandler/signalhander.go index 22e0c6a..5d2233e 100644 --- a/pkg/signalhandler/signalhander.go +++ b/pkg/signalhandler/signalhander.go @@ -42,17 +42,17 @@ func sendSignals(sig os.Signal, subscribers []chan os.Signal, done chan string) func Run(subscribers []chan os.Signal, done chan string, server *http.Server) { signals := make(chan os.Signal, 1) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGQUIT) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) var sig os.Signal for { sig := <-signals - log.Println("signalhandler: Got signal " + sig.String()) + log.Printf("signalhandler: Got signal '%s'\n", sig.String()) if sig == syscall.SIGTERM { // Shutdown daemon on SIGTERM break } - log.Printf("signalhandler: Ignoring signal %s. Send SIGTERM to trigger shutdown.\n", sig.String()) + log.Printf("signalhandler: Ignoring signal '%s'. Send SIGTERM to trigger shutdown.\n", sig.String()) } log.Println("signalhandler: Sending shutdown signals to components") diff --git a/scripts/reshctl.sh b/scripts/reshctl.sh index 6e85bfd..2c225ce 100644 --- a/scripts/reshctl.sh +++ b/scripts/reshctl.sh @@ -73,10 +73,9 @@ resh() { elif [ $status_code = 130 ]; then true else - local tmp_file="$__RESH_XDG_CACHE_HOME/search_last_run_out.txt" + local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" echo "$buffer" >| "$tmp_file" - echo "resh-cli ERROR:" - cat "$tmp_file" + echo "resh-cli failed - check '$tmp_file' and '~/.resh/cli.log'" fi } diff --git a/scripts/widgets.sh b/scripts/widgets.sh index 2a7fcd3..2c49471 100644 --- a/scripts/widgets.sh +++ b/scripts/widgets.sh @@ -35,8 +35,9 @@ __resh_widget_control_R() { bind -x '"\u[32~": __resh_nop' fi else - echo "$BUFFER" >| ~/.resh/cli_last_run_out.txt - echo "# RESH SEARCH APP failed - sorry for the inconvinience (error output was saved to ~/.resh/cli_last_run_out.txt)" + local tmp_file="$__RESH_XDG_CACHE_HOME/cli_last_run_out.txt" + echo "$BUFFER" >| "$tmp_file" + echo "# RESH SEARCH APP failed - sorry for the inconvinience - check '$tmp_file' and '~/.resh/cli.log'" BUFFER="$PREVBUFFER" fi CURSOR=${#BUFFER}