2022-08-29 07:20:36 +04:00
|
|
|
package liquidsoap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNotRunning = errors.New("liquidsoap is not running")
|
|
|
|
|
|
|
|
type Liquidsoap struct {
|
|
|
|
command *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLiquidsoap(liquidsoapPath, scriptPath string) (*Liquidsoap, error) {
|
2022-08-29 22:20:05 +04:00
|
|
|
if _, err := exec.LookPath(liquidsoapPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-29 07:20:36 +04:00
|
|
|
cmd := exec.Command(liquidsoapPath, scriptPath)
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are errors in a script this time will be sufficient to wait
|
|
|
|
// for them to occure.
|
|
|
|
time.Sleep(4 * time.Second)
|
|
|
|
|
|
|
|
if _, err := os.FindProcess(cmd.Process.Pid); err != nil {
|
|
|
|
if err_wait := cmd.Wait(); err != nil {
|
2022-08-29 22:20:05 +04:00
|
|
|
return nil, errors.Wrap(err, err_wait.Error())
|
2022-08-29 07:20:36 +04:00
|
|
|
}
|
2022-08-29 22:20:05 +04:00
|
|
|
return nil, err
|
2022-08-29 07:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Liquidsoap{
|
|
|
|
command: cmd}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Liquidsoap) Stop() error {
|
|
|
|
if !l.IsRunning() {
|
|
|
|
return ErrNotRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.command.Process.Signal(syscall.SIGINT); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := l.command.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Liquidsoap) IsRunning() bool {
|
|
|
|
return l.command.Process != nil && l.command.ProcessState == nil
|
|
|
|
}
|