Skip to content

Commit

Permalink
Cleanup code and fix installation.md (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdouchement authored May 1, 2020
1 parent 7cd7785 commit 1120730
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version: '2'

vars:
VERSION: 0.1.2
VERSION: 0.1.3
REVISION: { sh: git rev-parse HEAD }

env:
Expand Down
9 changes: 6 additions & 3 deletions cmd/shigoto/daemon/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (

sock := socket.New(konf.String("socket"))
defer sock.Close()

go func() {
err = sock.Listen(func(event []byte) []byte {
if bytes.Equal(event, socket.SignalReload) {
Expand All @@ -78,8 +79,10 @@ var (
return []byte("Unsupported signal")
})

fmt.Println(err)
os.Exit(1)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()

//
Expand All @@ -90,12 +93,12 @@ var (
return err
}
pool.Start()
defer pool.Stop()

signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
<-signals

pool.Stop()
return nil
},
}
Expand Down
7 changes: 6 additions & 1 deletion docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ After=network.target

[Service]
PIDFile=/run/shigoto.pid
Restart=on-failure
KillSignal=SIGINT

ExecStart=/usr/sbin/shigoto daemon
ExecStartPost=/bin/sh -c '/usr/sbin/pidof shigoto > /run/shigoto.pid'

ExecReload=/usr/sbin/shigoto reload
ExecStop=/bin/kill -s TERM $MAINPID
ExecStop=/bin/kill -s INT $MAINPID

[Install]
WantedBy=multi-user.target
Expand Down
23 changes: 15 additions & 8 deletions internal/socket/shigoto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"net"
"os"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -60,10 +59,7 @@ func (s *Socket) Listen(handler func(event []byte) []byte) error {
for {
conn, err := ln.Accept()
if err != nil {
if strings.HasSuffix(err.Error(), "use of closed network connection") {
return nil // Close has been trigged
}
return err
return s.hide(err)
}
reader := bufio.NewReader(conn)
if event, err := reader.ReadBytes(ControlCharacter); err == nil {
Expand All @@ -77,10 +73,21 @@ func (s *Socket) Listen(handler func(event []byte) []byte) error {

// Close closes the listener if Listen as been called.
func (s *Socket) Close() error {
defer os.Remove(s.socket)

if s.close == nil {
return nil
}
return s.close()

return s.hide(s.close())
}

func (s *Socket) hide(err error) error {
if err == nil {
return nil
}

if strings.HasSuffix(err.Error(), "use of closed network connection") {
return nil // Close has been trigged
}

return err
}

0 comments on commit 1120730

Please sign in to comment.