Skip to content
Open
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
29 changes: 29 additions & 0 deletions fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ func (f *Function) Fork(args ...interface{}) (err error) {
return
}

// Combine NewFork and Fork with previous function configuration
func (f *Function) ReFork(args ...interface{}) (err error) {
previous := f.c
f.c = exec.Cmd{}
f.c.Path, _ = os.Executable()
f.c.Args = previous.Args
f.c.Stderr = f.Stderr
f.c.Stdout = f.Stdout
f.c.Stdin = f.Stdin
f.c.SysProcAttr = f.SysProcAttr
f.c.Env = os.Environ()
f.c.Env = append(f.c.Env, nameVar+"="+f.Name)
af, err := ioutil.TempFile("", "gofork_*")
f.c.Env = append(f.c.Env, argsVar+"="+af.Name())
if err != nil {
return
}
enc := gob.NewEncoder(af)
for _, iv := range args {
enc.EncodeValue(reflect.ValueOf(iv))
}
af.Close()
if err = f.c.Start(); err != nil {
return
}
f.Process = f.c.Process
return
}

// Wait provides a wrapper around exec.Cmd.Wait()
func (f *Function) Wait() (err error) {
if err = f.c.Wait(); err != nil {
Expand Down