Skip to content

Commit

Permalink
longer timeouts, various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Dec 11, 2021
1 parent 45c6620 commit 3d18e16
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
13 changes: 7 additions & 6 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 {
Expand All @@ -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!")
Expand All @@ -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")
}
5 changes: 4 additions & 1 deletion cmd/daemon/run-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cmd/daemon/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"

"github.com/curusarn/resh/pkg/httpclient"
"github.com/curusarn/resh/pkg/msg"
)

Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion cmd/inspect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"
"net/http"
"time"

"github.com/BurntSushi/toml"
"github.com/curusarn/resh/pkg/cfg"
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions pkg/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

"github.com/curusarn/resh/pkg/httpclient"
"github.com/curusarn/resh/pkg/records"
)

Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (

func New() *http.Client {
return &http.Client{
Timeout: 100 * time.Millisecond,
Timeout: 500 * time.Millisecond,
}
}
6 changes: 3 additions & 3 deletions pkg/signalhandler/signalhander.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 2 additions & 3 deletions scripts/reshctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions scripts/widgets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 3d18e16

Please sign in to comment.