Send SIGHUP to parent process when woke up
This commit is contained in:
parent
7663a73694
commit
cea2fb641d
39
main.go
39
main.go
@ -5,22 +5,26 @@ import (
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/faiface/beep"
|
||||
"github.com/faiface/beep/effects"
|
||||
"github.com/faiface/beep/speaker"
|
||||
"github.com/faiface/beep/flac"
|
||||
"github.com/faiface/beep/mp3"
|
||||
"github.com/faiface/beep/speaker"
|
||||
)
|
||||
|
||||
const (
|
||||
SampleRate = beep.SampleRate(48000)
|
||||
MaxRunTime = 1 * time.Hour
|
||||
)
|
||||
|
||||
func xPrintIdle() (idle uint64) {
|
||||
@ -72,7 +76,7 @@ func loadFile(path string) (s beep.Streamer, err error) {
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if len(flag.Args()) < 2 {
|
||||
if len(flag.Args()) < 1 {
|
||||
log.Println("missing required argument: input file name")
|
||||
return
|
||||
}
|
||||
@ -116,19 +120,36 @@ func main() {
|
||||
|
||||
launched := time.Now()
|
||||
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:
|
||||
for {
|
||||
select{
|
||||
case t := <-ticker.C:
|
||||
if volume.Volume < 0 {
|
||||
volume.Volume += 0.007
|
||||
}
|
||||
if t.Sub(launched) > 1 * time.Hour {
|
||||
select {
|
||||
case <-maxRun:
|
||||
break loop
|
||||
}
|
||||
case <-ticker.C:
|
||||
ntick += 1
|
||||
volume.Volume = -2 - math.Log(5/float64(ntick))/3
|
||||
|
||||
if xPrintIdle() < idle {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user