mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-16 04:12:16 +01:00
office: no more orphan processes (hopefully)
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/phayes/freeport"
|
"github.com/phayes/freeport"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
@@ -104,8 +106,7 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string,
|
|||||||
args = append(args, "--printer", "PaperOrientation=landscape")
|
args = append(args, "--printer", "PaperOrientation=landscape")
|
||||||
}
|
}
|
||||||
args = append(args, "--output", destination, fpath)
|
args = append(args, "--output", destination, fpath)
|
||||||
cmd, err := xexec.CommandContext(
|
cmd, err := xexec.Command(
|
||||||
ctx,
|
|
||||||
logger,
|
logger,
|
||||||
"unoconv",
|
"unoconv",
|
||||||
args...,
|
args...,
|
||||||
@@ -114,7 +115,34 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
xexec.LogBeforeExecute(logger, cmd)
|
xexec.LogBeforeExecute(logger, cmd)
|
||||||
return cmd.Run()
|
// see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773.
|
||||||
|
kill := func() {
|
||||||
|
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "no such process") {
|
||||||
|
logger.ErrorOp(op, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
result := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
result <- cmd.Wait()
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case err := <-result:
|
||||||
|
logger.DebugfOp(op, "command '%s' finished", strings.Join(cmd.Args, " "))
|
||||||
|
kill()
|
||||||
|
return err
|
||||||
|
case <-ctx.Done():
|
||||||
|
logger.DebugfOp(op, "command '%s' failed to finish before context.Context deadline", strings.Join(cmd.Args, " "))
|
||||||
|
kill()
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := resolver(); err != nil {
|
if err := resolver(); err != nil {
|
||||||
return xerror.New(op, err)
|
return xerror.New(op, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user