Skip to content

Commit

Permalink
refactor: standardize log & error messages
Browse files Browse the repository at this point in the history
Standardize a lot of the logging and error message strings. Include some
additional state when logging at trace and debug levels.

Signed-off-by: Link Dupont <[email protected]>
  • Loading branch information
subpop committed Jan 26, 2022
1 parent 5d66c42 commit bbc72d9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/yggd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *Client) DataReceiveHandlerFunc(data []byte, dest string) {
func (c *Client) ReceiveData() {
for msg := range c.d.recvQ {
if err := c.SendDataMessage(&msg); err != nil {
log.Errorf("failed to send data message: %v", err)
log.Errorf("cannot send data message: %v", err)
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/yggd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import (
// process has been started.
func startProcess(file string, args []string, env []string, started func(pid int, stdout io.ReadCloser, stderr io.ReadCloser)) error {
if _, err := os.Stat(file); os.IsNotExist(err) {
return fmt.Errorf("cannot find file: %v", err)
return fmt.Errorf("cannot find file: %w", err)
}

cmd := exec.Command(file, args...)
cmd.Env = env

stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("cannot connect to stdout: %v", err)
return fmt.Errorf("cannot connect to stdout: %w", err)
}

stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("cannot connect to stderr: %v", err)
return fmt.Errorf("cannot connect to stderr: %w", err)
}

if err := cmd.Start(); err != nil {
return fmt.Errorf("cannot start process: %v: %v", file, err)
return fmt.Errorf("cannot start process: %v: %w", file, err)
}

if started != nil {
Expand All @@ -44,12 +44,12 @@ func startProcess(file string, args []string, env []string, started func(pid int
func waitProcess(pid int, died func(pid int, state *os.ProcessState)) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("cannot find process with pid: %v", err)
return fmt.Errorf("cannot find process with pid %v: %w", pid, err)
}

state, err := process.Wait()
if err != nil {
return fmt.Errorf("process %v exited with error: %v", process.Pid, err)
return fmt.Errorf("process %v exited with error: %w", process.Pid, err)
}

if died != nil {
Expand All @@ -63,11 +63,11 @@ func waitProcess(pid int, died func(pid int, state *os.ProcessState)) error {
func stopProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("cannot find process with pid: %v", err)
return fmt.Errorf("cannot find process with pid %v: %w", pid, err)
}

if err := process.Kill(); err != nil {
return fmt.Errorf("cannot stop process: %v", err)
return fmt.Errorf("cannot stop process: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/yggd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (d *dispatcher) disconnectWorker(w *worker) error {

_, err = workerClient.NotifyEvent(ctx, &pb.EventNotification{Name: pb.Event_RECEIVED_DISCONNECT})
if err != nil {
log.Errorf("cannot disconnect worker %v", err)
log.Errorf("cannot disconnect worker %v: %v", w, err)
return err
}
return nil
Expand All @@ -150,7 +150,7 @@ func (d *dispatcher) sendData() {
w := d.reg.get(data.Directive)

if w == nil {
log.Warnf("cannot route message to directive: %v", data.Directive)
log.Warnf("cannot route message %v to directive: %v", data.MessageID, data.Directive)
return
}

Expand Down
1 change: 1 addition & 0 deletions cmd/yggd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func main() {
continue
}
if ExcludeWorkers[config.directive] {
log.Tracef("skipping excluded worker %v", config.directive)
continue
}
log.Debugf("starting worker: %v", config.directive)
Expand Down
5 changes: 3 additions & 2 deletions cmd/yggd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,23 @@ func watchWorkerDir(dir string, died chan int) {
log.Errorf("cannot load worker config: %v", err)
}
if ExcludeWorkers[config.directive] {
log.Tracef("skipping excluded worker %v", config.directive)
continue
}
log.Debugf("starting worker: %v", config.directive)
go func() {
if err := startWorker(*config, nil, func(pid int) {
died <- pid
}); err != nil {
log.Errorf("cannot start worker: %v", err)
log.Errorf("cannot start worker %v: %v", config.directive, err)
return
}
}()
case notify.InDelete, notify.InMovedFrom:
name := strings.TrimSuffix(filepath.Base(e.Path()), filepath.Ext(e.Path()))

if err := stopWorker(name); err != nil {
log.Errorf("cannot kill worker: %v", err)
log.Errorf("cannot kill worker %v: %v", name, err)
continue
}
}
Expand Down

0 comments on commit bbc72d9

Please sign in to comment.