add pid file, add context for go routines + add proper logging

This commit is contained in:
LeonardoTrapani
2025-08-07 18:52:45 +02:00
parent f7936610c9
commit 1acd007be0
7 changed files with 212 additions and 86 deletions
+68
View File
@@ -2,12 +2,15 @@ package bus
import (
"bufio"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
)
const SockName = "control.sock"
const PidName = "hyprvoice.pid"
const ProtoVer = "0.1"
// ~/.cache/hyprvoice/control.sock
@@ -20,6 +23,16 @@ func SockPath() (string, error) {
return filepath.Join(hd, SockName), nil
}
// ~/.cache/hyprvoice/hyprvoice.pid
func PidPath() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
hd := filepath.Join(dir, "hyprvoice")
return filepath.Join(hd, PidName), nil
}
func Listen() (net.Listener, error) {
sp, err := SockPath()
if err != nil {
@@ -55,3 +68,58 @@ func SendCommand(cmd byte) (string, error) {
resp, err := bufio.NewReader(c).ReadString('\n')
return resp, err
}
func CheckExistingDaemon() error {
pidPath, err := PidPath()
if err != nil {
return err
}
pidData, err := os.ReadFile(pidPath)
if os.IsNotExist(err) {
return nil // no existing daemon
}
if err != nil {
return err
}
pid, err := strconv.Atoi(string(pidData))
if err != nil {
return nil // invalid pid file, assume stale
}
// Check if process exists
proc, err := os.FindProcess(pid)
if err != nil {
return nil
}
// Try to signal the process to check if it's alive
if err := proc.Signal(os.Signal(nil)); err != nil {
return nil // process not alive, stale pid file
}
return fmt.Errorf("daemon already running with PID %d", pid)
}
func CreatePidFile() error {
pidPath, err := PidPath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(pidPath), 0o700); err != nil {
return err
}
pid := os.Getpid()
return os.WriteFile(pidPath, []byte(strconv.Itoa(pid)), 0o600)
}
func RemovePidFile() error {
pidPath, err := PidPath()
if err != nil {
return err
}
return os.Remove(pidPath)
}
+61 -2
View File
@@ -2,9 +2,14 @@ package hotkeydaemon
import (
"bufio"
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/leonardotrapani/hyprvoice/internal/bus"
@@ -15,14 +20,19 @@ type Daemon struct {
mu sync.Mutex
recording bool
notifier notify.Notifier
ctx context.Context
cancel context.CancelFunc
}
func New(n notify.Notifier) *Daemon {
if n == nil {
n = notify.Desktop{}
}
ctx, cancel := context.WithCancel(context.Background())
return &Daemon{
notifier: n,
ctx: ctx,
cancel: cancel,
}
}
@@ -33,13 +43,54 @@ func (d *Daemon) Rec() bool {
}
func (d *Daemon) Run() error {
// Check if daemon is already running
if err := bus.CheckExistingDaemon(); err != nil {
return err
}
ln, err := bus.Listen()
if err != nil {
return err
}
defer ln.Close()
// Create PID file
if err := bus.CreatePidFile(); err != nil {
return fmt.Errorf("failed to create PID file: %w", err)
}
defer bus.RemovePidFile()
// Set up signal handling for graceful shutdown
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-sigCh
log.Printf("Received signal %v, shutting down gracefully", sig)
d.cancel()
}()
log.Printf("Daemon started, listening on socket")
for {
select {
case <-d.ctx.Done():
log.Printf("Shutdown requested, exiting")
return nil
default:
}
// Set a timeout for Accept to make it cancellable
if tcpListener, ok := ln.(*net.UnixListener); ok {
tcpListener.SetDeadline(time.Now().Add(100 * time.Millisecond))
}
c, err := ln.Accept()
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue // timeout, check for shutdown
}
log.Printf("Accept error: %v", err)
time.Sleep(100 * time.Millisecond)
continue
}
go d.handle(c)
@@ -49,7 +100,12 @@ func (d *Daemon) Run() error {
func (d *Daemon) handle(c net.Conn) {
defer c.Close()
line, _ := bufio.NewReader(c).ReadString('\n')
line, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
log.Printf("Client read error: %v", err)
fmt.Fprintf(c, "ERR read_error: %v\n", err)
return
}
if len(line) == 0 {
fmt.Fprint(c, "ERR empty\n")
return
@@ -63,18 +119,21 @@ func (d *Daemon) handle(c net.Conn) {
case 't': // toggle
d.recording = !d.recording
d.notifier.RecordingChanged(d.recording)
log.Printf("Recording toggled: %t", d.recording)
fmt.Fprintf(c, "STATUS recording=%t\n", d.recording)
case 's': // status
fmt.Fprintf(c, "STATUS recording=%t\n", d.recording)
case 'v': // protocol version
fmt.Fprintf(c, "STATUS proto=%s\n", bus.ProtoVer)
case 'q': // quit daemon
log.Printf("Shutdown requested")
fmt.Fprint(c, "OK quitting\n")
go func() {
time.Sleep(100 * time.Millisecond) // give time for client to read
panic("daemon exit requested")
d.cancel() // trigger graceful shutdown
}()
default:
log.Printf("Unknown command: %c", cmd)
fmt.Fprintf(c, "ERR unknown=%q\n", cmd)
}
}
+66
View File
@@ -0,0 +1,66 @@
package hotkeydaemon
import (
"testing"
"time"
"github.com/leonardotrapani/hyprvoice/internal/bus"
"github.com/leonardotrapani/hyprvoice/internal/notify"
)
func TestToggle(t *testing.T) {
// Clean up any existing daemon
bus.RemovePidFile()
d := New(notify.Nop{})
// Start daemon in goroutine
errCh := make(chan error, 1)
go func() {
errCh <- d.Run()
}()
// Wait for daemon to be ready by trying to connect
maxAttempts := 50
for i := range maxAttempts {
if _, err := bus.SendCommand('s'); err == nil {
break // daemon is ready
}
if i == maxAttempts-1 {
t.Fatal("daemon failed to start within timeout")
}
time.Sleep(10 * time.Millisecond)
}
defer func() {
bus.SendCommand('q')
// Wait for daemon to exit
select {
case <-errCh:
case <-time.After(3 * time.Second):
t.Error("daemon did not exit within timeout")
}
}()
// Test first toggle
if out, err := bus.SendCommand('t'); err != nil {
t.Fatalf("first toggle failed: %v", err)
} else if out != "STATUS recording=true\n" {
t.Fatalf("unexpected first toggle response: %s", out)
}
if !d.Rec() {
t.Fatalf("state should be true after first toggle")
}
// Test second toggle
if out, err := bus.SendCommand('t'); err != nil {
t.Fatalf("second toggle failed: %v", err)
} else if out != "STATUS recording=false\n" {
t.Fatalf("unexpected second toggle response: %s", out)
}
if d.Rec() {
t.Fatalf("state should be false after second toggle")
}
}
-29
View File
@@ -1,29 +0,0 @@
package hotkeydaemon
import (
"testing"
"time"
"github.com/leonardotrapani/hyprvoice/internal/bus"
"github.com/leonardotrapani/hyprvoice/internal/notify"
)
func TestToggle(t *testing.T) {
d := New(notify.Nop{})
go d.Run()
time.Sleep(50 * time.Millisecond) // give listener time to start
defer bus.SendCommand('q')
if out, _ := bus.SendCommand('t'); out != "STATUS recording=true\n" {
t.Fatalf("unexpected: %s", out)
}
if !d.Rec() {
t.Fatalf("state should be true after first toggle")
}
if out, _ := bus.SendCommand('t'); out != "STATUS recording=false\n" {
t.Fatalf("unexpected: %s", out)
}
if d.Rec() {
t.Fatalf("state should be false after second toggle")
}
}
+10 -3
View File
@@ -2,6 +2,7 @@ package notify
import (
"fmt"
"log"
"os/exec"
)
@@ -17,12 +18,18 @@ func (Desktop) RecordingChanged(on bool) {
if on {
state = "Started"
}
exec.Command("notify-send", "-a", "Hyprvoice",
fmt.Sprintf("Hyprvoice: %s Recording", state)).Run()
cmd := exec.Command("notify-send", "-a", "Hyprvoice",
fmt.Sprintf("Hyprvoice: %s Recording", state))
if err := cmd.Run(); err != nil {
log.Printf("Failed to send notification: %v", err)
}
}
func (Desktop) Error(msg string) {
exec.Command("notify-send", "-a", "Hyprvoice", "-u", "critical", msg).Run()
cmd := exec.Command("notify-send", "-a", "Hyprvoice", "-u", "critical", msg)
if err := cmd.Run(); err != nil {
log.Printf("Failed to send error notification: %v", err)
}
}
// Nop is a Notifier that does absolutely nothing.