-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathservice_process.go
More file actions
337 lines (314 loc) · 11.2 KB
/
Copy pathservice_process.go
File metadata and controls
337 lines (314 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"maps"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/google/shlex"
)
type serviceLoggingWriter struct {
prefix string
logger *log.Logger
buf []byte // holds an incomplete line between Write calls
writeMutex sync.Mutex
}
func (w *serviceLoggingWriter) FinalFlush() {
if w == nil {
return
}
w.writeMutex.Lock()
defer w.writeMutex.Unlock()
if len(w.buf) == 0 {
return
}
w.logger.Print(w.prefix + string(w.buf))
w.buf = nil
}
func findLowerIndexThatIsNotMinusOne(indexOne int, indexTwo int) int {
if indexOne == -1 {
return indexTwo
}
if indexTwo == -1 {
return indexOne
}
if indexOne > indexTwo {
return indexTwo
}
return indexOne
}
func (w *serviceLoggingWriter) Write(b []byte) (int, error) {
w.writeMutex.Lock()
defer w.writeMutex.Unlock()
// append new bytes to anything left over from the previous call
data := append(w.buf, b...)
for {
returnIndex := bytes.IndexByte(data, '\r')
newLineIndex := bytes.IndexByte(data, '\n')
var cutOffIndex int
if returnIndex != -1 && newLineIndex != -1 && newLineIndex-returnIndex == 1 {
//CRLF
cutOffIndex = newLineIndex
} else {
cutOffIndex = findLowerIndexThatIsNotMinusOne(newLineIndex, returnIndex)
}
if cutOffIndex == -1 {
// no complete line yet – remember what we have and return
w.buf = data
return len(b), nil
}
// strip the trailing '\r' and log the line
line := strings.TrimRight(string(data[:cutOffIndex]), "\r\n")
w.logger.Print(w.prefix + line)
// advance past the newline and continue scanning
data = data[cutOffIndex+1:]
}
}
func runServiceCommand(serviceConfig ServiceConfig) (
*exec.Cmd,
*serviceLoggingWriter,
*serviceLoggingWriter,
) {
if serviceConfig.LogFilePath == "" {
serviceConfig.LogFilePath = "logs/" + serviceConfig.Name + ".log"
}
logDir := filepath.Dir(serviceConfig.LogFilePath)
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
log.Printf("[%s] Failed to create log directory %s: %v", serviceConfig.Name, logDir, err)
return nil, nil, nil
}
args, err := shlex.Split(serviceConfig.Args)
if err != nil {
log.Printf("[%s] Failed to parse service arguments %s: %v", serviceConfig.Name, serviceConfig.Args, err)
return nil, nil, nil
}
logFormatString, logArguments := produceStartCommandLogString(serviceConfig)
log.Printf(logFormatString, logArguments...)
cmd := exec.Command(serviceConfig.Command, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}
if serviceConfig.Workdir != "" {
cmd.Dir = serviceConfig.Workdir
}
logFile, err := os.OpenFile(serviceConfig.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("[%s] Error opening log file: %v", serviceConfig.Name, err)
return nil, nil, nil
}
var stdoutSLW, stderrSLW *serviceLoggingWriter
if *config.OutputServiceLogs {
stdoutSLW = &serviceLoggingWriter{
prefix: fmt.Sprintf("[%s/stdout] ", serviceConfig.Name),
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lmicroseconds),
}
stderrSLW = &serviceLoggingWriter{
prefix: fmt.Sprintf("[%s/stderr] ", serviceConfig.Name),
logger: log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lmicroseconds),
}
cmd.Stdout = io.MultiWriter(logFile, stdoutSLW)
cmd.Stderr = io.MultiWriter(logFile, stderrSLW)
} else {
cmd.Stdout, cmd.Stderr = logFile, logFile
}
if err := cmd.Start(); err != nil {
log.Printf("[%s] Error starting command: %v", serviceConfig.Name, err)
return nil, nil, nil
}
return cmd, stdoutSLW, stderrSLW
}
func produceStartCommandLogString(serviceConfig ServiceConfig) (string, []any) {
logFormatString := "[%s] Starting \"%s"
logArguments := []any{
serviceConfig.Name,
serviceConfig.Command,
}
if serviceConfig.Args != "" {
logFormatString += " %s"
logArguments = append(logArguments, serviceConfig.Args)
}
logFormatString += "\""
if serviceConfig.LogFilePath != "" {
logFormatString += ", log file: %s"
logArguments = append(logArguments, serviceConfig.LogFilePath)
}
if serviceConfig.Workdir != "" {
logFormatString += ", workdir: %s"
logArguments = append(logArguments, serviceConfig.Workdir)
}
return logFormatString, logArguments
}
func stopService(service ServiceConfig) {
runningService, ok := resourceManager.maybeGetRunningService(service.Name)
if !ok {
log.Printf("[%s] Warning: Failed to find a service in a list of running services while stopping it, multiple stops requested or service already died. Stop aborted.", service.Name)
return
}
stopRunningService(service, runningService)
}
// stopRunningService is the body of stopService with the map lookup factored out,
// so shutdown can stop each service directly from the held runningServices map
// without re-acquiring serviceMutex via maybeGetRunningService.
func stopRunningService(service ServiceConfig, runningService *RunningService) {
if interrupted.Load() {
//If the process is being interrupted, we want to stop the service no matter what, even if it's currently locked
runningService.manageMutex.TryLock()
} else {
runningService.manageMutex.Lock()
defer runningService.manageMutex.Unlock()
}
// idleTimer is also accessed (and nilled) under serviceMutex in
// cleanUpStoppedServiceWhenServiceMutexIsLocked; take serviceMutex here so
// the read/Stop agrees with that write on the same lock. stopService already
// establishes a manageMutex -> serviceMutex ordering elsewhere (the
// cleanUpStoppedServiceWhenServiceMutexIsLocked call below acquires
// serviceMutex while manageMutex is held), so this is consistent.
//
// Guard with !interrupted: during shutdown the signal handler itself holds
// serviceMutex and calls stopService, so re-Locking here would self-deadlock
// (Go mutexes are not re-entrant). Skipping is safe on that path because the
// idle-timer callback returns immediately when interrupted is set, and
// cleanUp is skipped on the shutdown path too (see the !interrupted guard
// below) — so nothing nils the timer concurrently while we skip.
if !interrupted.Load() {
resourceManager.serviceMutex.Lock()
if runningService.idleTimer != nil {
runningService.idleTimer.Stop()
}
resourceManager.serviceMutex.Unlock()
}
if runningService.cmd != nil && runningService.cmd.Process != nil {
if service.KillCommand != nil {
log.Printf("[%s] Sending custom kill command: %s", service.Name, *service.KillCommand)
cmd := exec.Command("sh", "-c", *service.KillCommand)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}
err := cmd.Start()
if err != nil {
log.Printf("[%s] Failed to start custom kill command: %v", service.Name, err)
}
err = cmd.Wait()
if err != nil {
log.Printf("[%s] Failed to wait for custom kill command: %v", service.Name, err)
}
}
log.Printf("[%s] Sending SIGTERM to service process group: -%d", service.Name, runningService.cmd.Process.Pid)
err := syscall.Kill(-runningService.cmd.Process.Pid, syscall.SIGTERM)
if err != nil {
log.Printf("[%s] Failed to send SIGTERM to -%d: %v", service.Name, runningService.cmd.Process.Pid, err)
}
processExitedCleanly := waitForProcessToTerminate(runningService.exitWaitGroup)
if !processExitedCleanly {
log.Printf("[%s] Timed out waiting, sending SIGKILL to service process group -%d", service.Name, runningService.cmd.Process.Pid)
err := syscall.Kill(-runningService.cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
log.Printf("[%s] Failed to kill service: %v", service.Name, err)
if runningService.cmd.ProcessState == nil && !errors.Is(err, syscall.ESRCH) { //ESRCH means process not found
log.Printf("[%s] Manual action required due to error when killing process", service.Name)
return
}
}
}
}
if !interrupted.Load() && !*runningService.resourcesReleased {
resourceManager.serviceMutex.Lock()
cleanUpStoppedServiceWhenServiceMutexIsLocked(&service, runningService, true)
resourceManager.serviceMutex.Unlock()
}
}
func monitorProcess(serviceName string, process *os.Process, runningService *RunningService) {
exitProcessState, err := process.Wait()
exitMessage := fmt.Sprintf("[%s] Process with pid %d terminated", serviceName, process.Pid)
if exitProcessState == nil {
exitMessage += " with unknown exit code"
} else {
exitMessage += fmt.Sprintf(" with exit code %d", exitProcessState.ExitCode())
}
if err != nil {
exitMessage += fmt.Sprintf(" and an error: %v", err)
}
// Signal process exit immediately, before any mutex acquisition.
// This ensures stopService's waitForProcessToTerminate is not blocked
// by monitorProcess waiting for serviceMutex.
log.Print(exitMessage)
runningService.exitWaitGroup.Done()
if interrupted.Load() {
if resourceManager.serviceMutex.TryLock() {
defer resourceManager.serviceMutex.Unlock()
} else {
if config.LogLevel == LogLevelDebug {
log.Printf("[%s] Not cleaning up resources due to large-model-proxy being interrupted", serviceName)
}
return
}
} else {
// Test-only synchronization point (see waitForProcessExitHook): in
// production builds this is a no-op, so no test scaffolding runs in the
// hot path and the hook cannot be triggered accidentally.
waitForProcessExitHook(serviceName)
if config.LogLevel == LogLevelDebug {
log.Printf("[%s] Acquiring a serviceMutex lock to clean up resources", serviceName)
}
resourceManager.serviceMutex.Lock()
if config.LogLevel == LogLevelDebug {
log.Printf("[%s] Acquired serviceMutex lock to clean up resources", serviceName)
}
defer resourceManager.serviceMutex.Unlock()
}
service := findServiceConfigByName(serviceName)
cleanUpStoppedServiceWhenServiceMutexIsLocked(service, runningService, *service.ConsiderStoppedOnProcessExit)
}
func cleanUpStoppedServiceWhenServiceMutexIsLocked(service *ServiceConfig, runningService *RunningService, shouldReleaseResources bool) {
if !shouldReleaseResources || *runningService.resourcesReleased {
return
}
if config.LogLevel == LogLevelDebug {
log.Printf("[%s] Cleaning up resources for stopped service", service.Name)
}
*runningService.resourcesReleased = true
if runningService.idleTimer != nil {
if config.LogLevel == LogLevelDebug {
log.Printf("[%s] Stopping the timer for stopped service", service.Name)
}
runningService.idleTimer.Stop()
runningService.idleTimer = nil
}
runningService.stdoutWriter.FinalFlush()
runningService.stderrWriter.FinalFlush()
if runningService.resourcesReserved {
releaseResourcesWhenServiceMutexIsLocked(service.ResourceRequirements)
}
runningServiceInRM := resourceManager.runningServices[service.Name]
if runningServiceInRM != runningService {
log.Printf("[%s] ERROR: Running service pointer present in resourceManager.runningServices is not the same instance as the one for which clean up was called", service.Name)
} else {
delete(resourceManager.runningServices, service.Name)
}
resourceManager.broadcastResourceChanges(maps.Keys(service.ResourceRequirements), true)
}
func waitForProcessToTerminate(exitWaitGroup *sync.WaitGroup) bool {
const ProcessCheckTimeout = 10 * time.Second
exitChannel := make(chan struct{})
go func() {
exitWaitGroup.Wait()
close(exitChannel)
}()
select {
case <-exitChannel:
return true
case <-time.After(ProcessCheckTimeout):
return false
}
}