Close kills the child api_server.py process, releasing its resources.
()
| 125 | |
| 126 | // Close kills the child api_server.py process, releasing its resources. |
| 127 | func (i *instance) Close() (err error) { |
| 128 | child := i.child |
| 129 | if child == nil { |
| 130 | return nil |
| 131 | } |
| 132 | defer func() { |
| 133 | i.child = nil |
| 134 | err1 := os.RemoveAll(i.appDir) |
| 135 | if err == nil { |
| 136 | err = err1 |
| 137 | } |
| 138 | }() |
| 139 | |
| 140 | if p := child.Process; p != nil { |
| 141 | errc := make(chan error, 1) |
| 142 | go func() { |
| 143 | errc <- child.Wait() |
| 144 | }() |
| 145 | |
| 146 | // Call the quit handler on the admin server. |
| 147 | res, err := http.Get(i.adminURL + "/quit") |
| 148 | if err != nil { |
| 149 | p.Kill() |
| 150 | return fmt.Errorf("unable to call /quit handler: %v", err) |
| 151 | } |
| 152 | res.Body.Close() |
| 153 | select { |
| 154 | case <-time.After(15 * time.Second): |
| 155 | p.Kill() |
| 156 | return errors.New("timeout killing child process") |
| 157 | case err = <-errc: |
| 158 | // Do nothing. |
| 159 | } |
| 160 | } |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | func fileExists(path string) bool { |
| 165 | _, err := os.Stat(path) |