Skip to content

Add option for launching in own process group #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions embedded_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions execopts_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
}
18 changes: 18 additions & 0 deletions execopts_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
}