-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
466 lines (380 loc) · 10.7 KB
/
process.go
File metadata and controls
466 lines (380 loc) · 10.7 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package process
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"robinplatform.dev/internal/identity"
"robinplatform.dev/internal/log"
"robinplatform.dev/internal/model"
"robinplatform.dev/internal/process/health"
"robinplatform.dev/internal/pubsub"
)
var (
logger = log.New("process")
)
// An identifier for a process.
type ProcessId identity.Id
func (p ProcessId) String() string {
return (identity.Id)(p).String()
}
type ProcessConfig struct {
Id ProcessId
WorkDir string
Env map[string]string
Command string
Args []string
Port int
HealthCheck health.HealthCheck
}
type Process struct {
Id ProcessId `json:"id"`
Pid int `json:"pid"`
StartedAt time.Time `json:"startedAt"`
WorkDir string `json:"workDir"`
Env map[string]string `json:"env"`
Command string `json:"command"`
Args []string `json:"args"`
Port int `json:"port"`
HealthCheck health.SerializableHealthCheck `json:"healthCheck"`
// NOTE: The fields below are only valid because
// the store doesn't re-load data from disk when the file is updated.
// They're not serializable, and get filled in at startup.
logsTopic *pubsub.Topic[string] `json:"-"`
Context context.Context `json:"-"` // This Context gets canceled when the process dies.
cancel func() `json:"-"` // Cancel the context
}
func waitForExit(process pollPidContext) {
proc, err := os.FindProcess(process.pid)
if err != nil {
logger.Debug("Failed to find process to wait on", log.Ctx{
"process": process,
"err": err,
})
return
}
if exitCode, err := proc.Wait(); err != nil {
logger.Debug("Process exited with error", log.Ctx{
"process": process,
"exitCode": exitCode,
"err": err,
})
} else {
logger.Debug("Process exited", log.Ctx{
"process": process,
})
}
process.cancel()
}
func (process *Process) IsAlive() bool {
select {
case <-process.Context.Done():
return false
default:
return true
}
}
type LogFileResult struct {
Text string `json:"text"`
Counter int32 `json:"counter"` // TODO: bad name
}
func findById(id ProcessId) func(row Process) bool {
return func(row Process) bool {
return row.Id == id
}
}
func (cfg *ProcessConfig) fillEmptyValues() error {
if cfg.Id.Key == "" {
return fmt.Errorf("cannot create process without a key")
}
if cfg.Id.Category == "" {
return fmt.Errorf("cannot create process without a category")
}
parentEnv := os.Environ()
env := make(map[string]string, len(parentEnv)+len(cfg.Env))
// copy over parent env first
for _, envVar := range parentEnv {
parts := strings.SplitN(envVar, "=", 2)
env[parts[0]] = parts[1]
}
// then override with any custom env vars
for k, v := range cfg.Env {
env[k] = v
}
// and replace the config's env with the new one
cfg.Env = env
if cfg.WorkDir == "" {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
cfg.WorkDir = dir
}
if cfg.HealthCheck == nil {
cfg.HealthCheck = &health.ProcessHealthCheck{}
}
return nil
}
// This is essentially a global type, but it's set up as an instance for testing purposes.
// Use `process.Manager` to manage processes.
type ProcessManager struct {
processLogsFolderPath string
// Data persisted to disk about processes
db model.Store[Process]
registry *pubsub.Registry
// Context for long running operations, the parent
// of all process contexts
ctx context.Context
// Cancel function for the context
cancel func()
}
func NewProcessManager(registry *pubsub.Registry, logsPath string, dbPath string) (*ProcessManager, error) {
manager := &ProcessManager{}
var err error
manager.db, err = model.NewStore[Process](dbPath)
if err != nil {
return nil, fmt.Errorf("failed to create process database: %w", err)
}
manager.processLogsFolderPath = logsPath
manager.registry = registry
manager.ctx, manager.cancel = context.WithCancel(context.Background())
procIds := make([]pollPidContext, 0)
var topicCreationErr error
err = manager.db.ForEachWriting(func(proc *Process) {
proc.Context, proc.cancel = context.WithCancel(manager.ctx)
if !health.PidIsAlive(proc.Pid) {
proc.cancel()
return
}
procIds = append(procIds, pollPidContext{
pid: proc.Pid,
cancel: proc.cancel,
})
topic, err := manager.logTopicForProcId(proc.Id)
if err != nil {
topicCreationErr = err
return
}
proc.logsTopic = topic
go manager.pipeTailIntoTopic(topicTailInfo{
processId: proc.Id,
logsTopic: topic,
Context: proc.Context,
})
})
if topicCreationErr != nil {
return nil, topicCreationErr
}
if err != nil {
return nil, err
}
// Hand off procIds to the goroutine
go pollForExit(procIds)
return manager, nil
}
// This polls to see if the process is still alive; this is necessary
// because if the process is not our child, we can't use process.Wait()
// anymore. This can happen if robin restarts but the child is still alive.
type pollPidContext struct {
cancel func()
pid int
}
func pollForExit(processes []pollPidContext) {
for {
if len(processes) == 0 {
return
}
nextProcesses := make([]pollPidContext, 0, len(processes))
for _, proc := range processes {
if !health.PidIsAlive(proc.pid) {
proc.cancel()
} else {
nextProcesses = append(nextProcesses, proc)
}
}
processes = nextProcesses
time.Sleep(200 * time.Millisecond)
}
}
func (r *RHandle) FindById(id ProcessId) (Process, bool) {
procEntry, found := r.db.Find(findById(id))
if !found {
return Process{}, false
}
return procEntry, true
}
func (r *RHandle) IsAlive(id ProcessId) bool {
process, found := r.FindById(id)
if !found {
return false
}
return process.IsAlive()
}
func (r *RHandle) CheckHealth(id ProcessId) bool {
process, found := r.FindById(id)
if !found {
return false
}
return process.CheckHealth()
}
func (proc *Process) CheckHealth() bool {
return proc.HealthCheck.Check(health.RunningProcessInfo{Pid: proc.Pid, Port: proc.Port})
}
// This reads the path variable to find the right executable.
func (w *WHandle) SpawnFromPathVar(config ProcessConfig) (Process, error) {
var err error
config.Command, err = exec.LookPath(config.Command)
if err != nil {
return Process{}, fmt.Errorf("failed to find command %s in $PATH: %w", config.Command, err)
}
return w.Spawn(config)
}
// This spawns a process using the given arguments and executable path.
func (w *WHandle) Spawn(procConfig ProcessConfig) (Process, error) {
if err := procConfig.fillEmptyValues(); err != nil {
return Process{}, err
}
healthCheck, err := health.NewHealthCheck(procConfig.HealthCheck)
if err != nil {
return Process{}, err
}
prev, found := w.db.Find(findById(procConfig.Id))
if found {
if prev.IsAlive() {
logger.Debug("Found previous process", log.Ctx{
"processId": procConfig.Id,
"pid": prev.Pid,
})
return prev, processExists(procConfig.Id)
}
logger.Debug("Found previous dead process entry, deleting it", log.Ctx{
"processId": procConfig.Id,
})
if err := w.Remove(prev.Id); err != nil {
return Process{}, fmt.Errorf("failed to delete previous process: %w", err)
}
}
logger.Info("Spawning Process", log.Ctx{
"config": procConfig,
})
empty, err := os.Open(os.DevNull)
if err != nil {
return Process{}, fmt.Errorf("failed to open null device: %w", err)
}
defer empty.Close()
processLogsPath := w.Read.m.getLogFilePath(procConfig.Id)
processLogsFolderPath := filepath.Dir(processLogsPath)
if err := os.MkdirAll(processLogsFolderPath, 0755); err != nil {
return Process{}, fmt.Errorf("failed to create process folder: %w", err)
}
// Don't close the file, instead pass it on to the tail goroutine later on
output, err := os.Create(processLogsPath)
if err != nil {
return Process{}, err
}
defer output.Close()
var attr os.ProcAttr
attr.Env = make([]string, 0, len(procConfig.Env))
attr.Dir = procConfig.WorkDir
attr.Files = []*os.File{empty, output, output}
attr.Sys = getProcessSysAttrs()
for key, value := range procConfig.Env {
attr.Env = append(attr.Env, key+"="+value)
}
argStrings := append([]string{procConfig.Command}, procConfig.Args...)
proc, err := os.StartProcess(procConfig.Command, argStrings, &attr)
if err != nil {
return Process{}, err
}
defer proc.Release()
topic, err := w.Read.m.logTopicForProcId(procConfig.Id)
if err != nil {
_ = proc.Kill()
return Process{}, err
}
ctx, cancel := context.WithCancel(w.Read.m.ctx)
entry := Process{
Id: procConfig.Id,
WorkDir: procConfig.WorkDir,
StartedAt: time.Now(),
Command: procConfig.Command,
Args: procConfig.Args,
Pid: proc.Pid,
Env: procConfig.Env,
Port: procConfig.Port,
HealthCheck: healthCheck,
logsTopic: topic,
Context: ctx,
cancel: cancel,
}
// Write output to file
go w.Read.m.pipeTailIntoTopic(topicTailInfo{
processId: entry.Id,
logsTopic: entry.logsTopic,
Context: entry.Context,
})
// Reap zombies
go waitForExit(pollPidContext{
pid: entry.Pid,
cancel: entry.cancel,
})
logger.Debug("Process created", log.Ctx{
"id": entry.Id,
"pid": entry.Pid,
"logsPath": processLogsPath,
})
if err := w.db.Insert(entry); err != nil {
logger.Debug("Failed to insert process into database", log.Ctx{
"error": err.Error(),
})
// If we failed to insert the process into the database, kill it
// so we don't end up with an unmanaged process
if err := proc.Kill(); err != nil {
logger.Err("Failed to kill unmanaged process", log.Ctx{
"error": err,
"process": entry,
})
}
return Process{}, err
}
return entry, nil
}
// Remove will kill the process if it is alive, and then remove it from the database
func (w *WHandle) Remove(id ProcessId) error {
procEntry, found := w.db.Find(findById(id))
if !found {
return nil
}
if procEntry.IsAlive() {
if err := w.Kill(id); err != nil {
return fmt.Errorf("failed to kill process: %w", err)
}
}
if err := w.db.Delete(findById(id)); err != nil {
return fmt.Errorf("failed to delete process: %w", err)
}
return nil
}
// TODO:
// - Maybe this should take in a function and allow the user
// to change the data before its outputted
// - Also, since there's no GC right now, old processes
// that are dead will still have their entries in the DB
func (r *RHandle) CopyOutData() []Process {
data := r.db.ShallowCopyOutData()
for i := 0; i < len(data); i += 1 {
proc := &data[i]
env := proc.Env
proc.Env = make(map[string]string, len(env))
for k, v := range env {
proc.Env[k] = v
}
args := proc.Args
proc.Args = make([]string, 0, len(args))
proc.Args = append(proc.Args, args...)
}
return data
}