Send SIGHUP to parent process when woke up

This commit is contained in:
nemunaire 2019-04-04 12:29:02 +02:00
parent 7663a73694
commit cea2fb641d

41
main.go
View File

@ -5,22 +5,26 @@ import (
"flag" "flag"
"io" "io"
"log" "log"
"math"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"
"github.com/faiface/beep" "github.com/faiface/beep"
"github.com/faiface/beep/effects" "github.com/faiface/beep/effects"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/flac" "github.com/faiface/beep/flac"
"github.com/faiface/beep/mp3" "github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
) )
const ( const (
SampleRate = beep.SampleRate(48000) SampleRate = beep.SampleRate(48000)
MaxRunTime = 1 * time.Hour
) )
func xPrintIdle() (idle uint64) { func xPrintIdle() (idle uint64) {
@ -72,7 +76,7 @@ func loadFile(path string) (s beep.Streamer, err error) {
func main() { func main() {
flag.Parse() flag.Parse()
if len(flag.Args()) < 2 { if len(flag.Args()) < 1 {
log.Println("missing required argument: input file name") log.Println("missing required argument: input file name")
return return
} }
@ -116,19 +120,36 @@ func main() {
launched := time.Now() launched := time.Now()
idle := xPrintIdle() idle := xPrintIdle()
ntick := 0
// Prepare graceful shutdown
maxRun := time.After(MaxRunTime)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
loop: loop:
for { for {
select{ select {
case t := <-ticker.C: case <-maxRun:
if volume.Volume < 0 { break loop
volume.Volume += 0.007 case <-ticker.C:
} ntick += 1
if t.Sub(launched) > 1 * time.Hour { volume.Volume = -2 - math.Log(5/float64(ntick))/3
break loop
}
if xPrintIdle() < idle { if xPrintIdle() < idle {
break loop break loop
} }
case <-interrupt:
break loop
}
}
// Tell parent process that it can launch the wake up procedure
if time.Now().Sub(launched) < MaxRunTime {
if proc, err := os.FindProcess(os.Getppid()); err != nil {
log.Println(err)
} else if err := proc.Signal(syscall.SIGHUP); err != nil {
log.Println(err)
} }
} }