diff --git a/config.go b/config.go index 35e9a98..a55766c 100644 --- a/config.go +++ b/config.go @@ -24,6 +24,7 @@ type Config struct { binaryRepositoryURL string startTimeout time.Duration logger io.Writer + ownProcessGroup bool } // DefaultConfig provides a default set of configuration to be used "as is" or modified using the provided builders. @@ -144,6 +145,12 @@ func (c Config) BinaryRepositoryURL(binaryRepositoryURL string) Config { return c } +// OwnProcessGroup configures whether the server should be started in its own process group. +func (c Config) OwnProcessGroup(ownProcessGroup bool) Config { + c.ownProcessGroup = ownProcessGroup + return c +} + func (c Config) GetConnectionURL() string { return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", c.username, c.password, "localhost", c.port, c.database) } diff --git a/embedded_postgres.go b/embedded_postgres.go index afe8497..de09423 100644 --- a/embedded_postgres.go +++ b/embedded_postgres.go @@ -216,6 +216,7 @@ func startPostgres(ep *EmbeddedPostgres) error { "-o", encodeOptions(ep.config.port, ep.config.startParameters)) postgresProcess.Stdout = ep.syncedLogger.file postgresProcess.Stderr = ep.syncedLogger.file + applyPlatformSpecificOptions(postgresProcess, ep.config) if err := postgresProcess.Run(); err != nil { _ = ep.syncedLogger.flush() @@ -233,6 +234,7 @@ func stopPostgres(ep *EmbeddedPostgres) error { "-D", ep.config.dataPath) postgresProcess.Stderr = ep.syncedLogger.file postgresProcess.Stdout = ep.syncedLogger.file + applyPlatformSpecificOptions(postgresProcess, ep.config) if err := postgresProcess.Run(); err != nil { return err diff --git a/embedded_postgres_test.go b/embedded_postgres_test.go index e7e98b3..393ffe3 100644 --- a/embedded_postgres_test.go +++ b/embedded_postgres_test.go @@ -258,6 +258,7 @@ func Test_CustomConfig(t *testing.T) { StartTimeout(10 * time.Second). Locale("C"). Encoding("UTF8"). + OwnProcessGroup(true). Logger(nil)) if err := database.Start(); err != nil { diff --git a/execopts_unix.go b/execopts_unix.go new file mode 100644 index 0000000..90f9d7e --- /dev/null +++ b/execopts_unix.go @@ -0,0 +1,18 @@ +//go:build !windows +// +build !windows + +package embeddedpostgres + +import ( + "os/exec" + "syscall" +) + +func applyPlatformSpecificOptions(cmd *exec.Cmd, config Config) { + if config.ownProcessGroup { + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.Setpgid = true + } +} diff --git a/execopts_windows.go b/execopts_windows.go new file mode 100644 index 0000000..cdeb0f7 --- /dev/null +++ b/execopts_windows.go @@ -0,0 +1,18 @@ +//go:build windows +// +build windows + +package embeddedpostgres + +import ( + "os/exec" + "syscall" +) + +func applyPlatformSpecificOptions(cmd *exec.Cmd, config Config) { + if config.ownProcessGroup { + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.CreationFlags = syscall.CREATE_NEW_PROCESS_GROUP + } +}