MCPcopy Index your code
hub / github.com/coder/envbox / startProcess

Method startProcess

background/process.go:104–156  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

102}
103
104func (d *Process) startProcess() error {
105 var (
106 buf bytes.Buffer
107
108 pr, pw = io.Pipe()
109 // Wrap our buffer in a limiter to
110 // avoid ballooning our memory use over time.
111 psw = &xio.PrefixSuffixWriter{
112 N: 1 << 10,
113 W: &buf,
114 }
115
116 w = &xio.SyncWriter{W: pw}
117 out = &xio.SyncWriter{W: psw}
118
119 mw = io.MultiWriter(w, out)
120 cmd = d.cmd
121 )
122
123 cmd.SetStdout(mw)
124 cmd.SetStderr(mw)
125
126 go scanIntoLog(d.ctx, d.log, bufio.NewScanner(pr), d.binName)
127
128 err := d.cmd.Start()
129 if err != nil {
130 return xerrors.Errorf("start: %w", err)
131 }
132
133 userKilled := d.userKilled
134
135 go func() {
136 defer d.cancel()
137 defer close(d.waitCh)
138
139 err := cmd.Wait()
140 _ = psw.Flush()
141
142 // If the user killed the application the actual error returned
143 // from wait doesn't really matter.
144 if atomic.LoadInt64(userKilled) == 1 {
145 d.waitCh <- ErrUserKilled
146 return
147 }
148
149 if err == nil {
150 d.waitCh <- nil
151 } else {
152 d.waitCh <- xerrors.Errorf("%s: %w", buf.Bytes(), err)
153 }
154 }()
155 return nil
156}
157
158func (d *Process) kill(sig syscall.Signal) error {
159 if d.cmd.OSProcess() == nil {

Callers 2

StartMethod · 0.95
RestartMethod · 0.95

Calls 7

scanIntoLogFunction · 0.85
SetStdoutMethod · 0.80
SetStderrMethod · 0.80
FlushMethod · 0.80
ErrorfMethod · 0.65
StartMethod · 0.45
WaitMethod · 0.45

Tested by

no test coverage detected