delay closing output log file

This commit is contained in:
Shreyas Goenka 2025-02-18 14:32:16 +01:00
parent 6f63e14cb7
commit 11af4ba964
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 16 additions and 7 deletions

View File

@ -26,8 +26,9 @@ type Daemon struct {
// Log file to write the child process's output to. // Log file to write the child process's output to.
LogFile string LogFile string
cmd *exec.Cmd outFile *os.File
stdin io.WriteCloser cmd *exec.Cmd
stdin io.WriteCloser
} }
func (d *Daemon) Start() error { func (d *Daemon) Start() error {
@ -43,7 +44,9 @@ func (d *Daemon) Start() error {
d.cmd = exec.Command(executable, d.Args...) d.cmd = exec.Command(executable, d.Args...)
// Set environment variable so that the child process know's it's parent's PID. // Set environment variable so that the child process knows it's parent's PID.
// In unix systems orphaned processes are automatically re-parented to init (pid 1)
// so we cannot rely on os.Getppid() to get the original parent's pid.
d.Env = append(d.Env, fmt.Sprintf("%s=%d", DatabricksCliParentPid, os.Getpid())) d.Env = append(d.Env, fmt.Sprintf("%s=%d", DatabricksCliParentPid, os.Getpid()))
d.cmd.Env = d.Env d.cmd.Env = d.Env
@ -55,14 +58,13 @@ func (d *Daemon) Start() error {
// If a log file is provided, redirect stdout and stderr to the log file. // If a log file is provided, redirect stdout and stderr to the log file.
if d.LogFile != "" { if d.LogFile != "" {
f, err := os.OpenFile(d.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) d.outFile, err = os.OpenFile(d.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("failed to open log file: %w", err) return fmt.Errorf("failed to open log file: %w", err)
} }
defer f.Close()
d.cmd.Stdout = f d.cmd.Stdout = d.outFile
d.cmd.Stderr = f d.cmd.Stderr = d.outFile
} }
d.stdin, err = d.cmd.StdinPipe() d.stdin, err = d.cmd.StdinPipe()
@ -98,6 +100,13 @@ func (d *Daemon) Release() error {
} }
} }
if d.outFile != nil {
err := d.outFile.Close()
if err != nil {
return fmt.Errorf("failed to close log file: %w", err)
}
}
if d.cmd == nil { if d.cmd == nil {
return nil return nil
} }