-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
process_management.go
540 lines (451 loc) · 14.8 KB
/
process_management.go
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package system
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"alda.io/client/color"
"alda.io/client/generated"
"alda.io/client/help"
log "alda.io/client/logging"
"alda.io/client/util"
"github.com/daveyarwood/go-osc/osc"
)
const reasonableTimeout = 20 * time.Second
func DeletePlayerStateFile(playerID string) error {
path := CachePath(
"state", "players", generated.ClientVersion, playerID+".json",
)
log.Debug().
Str("path", path).
Msg("Deleting player state file.")
err := os.Remove(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
func cleanUpStaleStateFiles(stateDir string) error {
if err := filepath.WalkDir(
stateDir,
func(path string, info os.DirEntry, err error) error {
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileInfo, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return nil
} else if err != nil {
return err
}
timeSinceModified := time.Since(fileInfo.ModTime())
if timeSinceModified > time.Minute*2 {
log.Debug().
Float64("seconds-since-modified", timeSinceModified.Seconds()).
Str("path", path).
Msg("Deleting stale file.")
err := os.Remove(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
return nil
},
); err != nil {
return err
}
// NOTE: I initially also walked the directory a second time and attempted to
// delete empty directories, but I found that it was too easy to run into a
// race condition where the directory, so we delete it, but right before we
// delete it, a player starts up and puts a file in the directory, and then
// the directory gets deleted even though it isn't actually empty at that
// point.
//
// Player processes also delete empty directories for old versions, and it
// seems to behave well, so we don't need to include the deletion of empty
// directories part here in the client.
return nil
}
// Each `alda-player` process creates a state file where it tracks its own
// state. The `alda` client uses this information to list player processes
// (`alda ps`) or to find an available player process to play a score.
//
// Each player process deletes its own state file when it exits, but this
// doesn't happen in exceptional scenarios like an out of memory error or the
// process being forcibly terminated (e.g. `kill -9`).
//
// Because it will always be possible for player processes to die before they
// can clean up their own state files, both `alda-player` and `alda` routinely
// check for and clean up stale player state files.
func CleanUpStaleStateFiles() error {
for _, stateDir := range []string{
CachePath("state", "players"),
CachePath("state", "repl-servers"),
} {
if err := cleanUpStaleStateFiles(stateDir); err != nil {
return err
}
}
return nil
}
// PlayerState describes the current state of a player process. These states are
// continously written to files by each player process. (See: StateManager.kt.)
type PlayerState struct {
State string
Port int
Expiry int64
ID string
ReadError error
}
// REPLServerState describes the current state of an Alda REPL server process.
// These states are continously written to files by each Alda REPL process.
// (See: repl/server.go.)
type REPLServerState struct {
Port int `json:"port"`
ID string `json:"id"`
ReadError error `json:"-"`
}
func processFiles(
directory string,
process func(filename string, contents []byte, readError error),
) error {
if err := os.MkdirAll(directory, os.ModePerm); err != nil {
return err
}
files, err := os.ReadDir(directory)
if err != nil {
return err
}
for _, file := range files {
filename := file.Name()
filepath := filepath.Join(directory, filename)
contents, readError := os.ReadFile(filepath)
process(filename, contents, readError)
}
return nil
}
// ReadPlayerStates reads all of the player state files in the Alda cache
// directory and returns a list of player state structs describing the current
// state of each player process.
//
// Returns an error if something goes wrong.
func ReadPlayerStates() ([]PlayerState, error) {
if err := CleanUpStaleStateFiles(); err != nil {
log.Warn().Err(err).Msg("Failed to clean up stale state files.")
}
states := []PlayerState{}
if err := processFiles(
CachePath("state", "players", generated.ClientVersion),
func(filename string, contents []byte, readError error) {
var state PlayerState
if readError == nil {
err := json.Unmarshal(contents, &state)
if err != nil {
readError = err
}
}
state.ID = strings.Replace(filename, ".json", "", 1)
state.ReadError = readError
states = append(states, state)
},
); err != nil {
return nil, err
}
return states, nil
}
// ReadREPLServerStates reads all of the REPL server state files in the Alda
// cache directory and returns a list of REPLServerState structs describing the
// current state of each player process.
//
// Returns an error if something goes wrong.
func ReadREPLServerStates() ([]REPLServerState, error) {
states := []REPLServerState{}
processFiles(
CachePath("state", "repl-servers"),
func(filename string, contents []byte, readError error) {
var state REPLServerState
if readError == nil {
err := json.Unmarshal(contents, &state)
if err != nil {
readError = err
}
}
state.ID = strings.Replace(filename, ".json", "", 1)
state.ReadError = readError
states = append(states, state)
},
)
return states, nil
}
// ErrAldaPlayerNotFoundOnPath is the error message that is returned when
// `alda-player` is not found on the PATH.
var ErrAldaPlayerNotFoundOnPath error
// ErrNoPlayersAvailable is the error message that is returned when no player
// process is in an available state.
var ErrNoPlayersAvailable error
func init() {
ErrAldaPlayerNotFoundOnPath = help.UserFacingErrorf(
`%s does not appear to be installed.
The %s command-line client needs to spawn %s processes in order to play audio
in the background.
To install %s, run %s and answer %s when prompted.`,
color.Aurora.Bold("alda-player"),
color.Aurora.Bold("alda"),
color.Aurora.Bold("alda-player"),
color.Aurora.Bold("alda-player"),
color.Aurora.BrightYellow("alda doctor"),
color.Aurora.Bold("y"),
)
playerLogFile := CachePath("logs", "alda-player.log")
ErrNoPlayersAvailable = help.UserFacingErrorf(
`It looks like Alda is having trouble starting player processes in the
background. This could happen for a number of reasons.
To troubleshoot:
• Run %s and see if any of the health checks fail.
• Run %s to see the state of any currently running player
processes.
• Look for error messages in:
%s
• Try running a player process in the foreground:
%s
• Try to make it play something:
%s`,
color.Aurora.BrightYellow("alda doctor"),
color.Aurora.BrightYellow("alda ps"),
color.Aurora.BrightYellow(playerLogFile),
color.Aurora.BrightYellow("alda-player -v run -p 27278"),
color.Aurora.BrightYellow("alda -v2 play -p 27278 -c \"piano: c12 e g > c4\""),
)
}
// PingPlayer sends a ping message to the specified port number, where a player
// process is expected to be listening.
//
// If the first ping doesn't go through, it is tried repeatedly for the
// `reasonableTimeout` duration, so as to avoid concluding too hastily that a
// player process is not reachable (e.g. it might be in the process of coming up
// and will be available shortly).
//
// If the player is reachable, the OSC client that we created in order to ping
// the player process is returned, so that it can be reused if desired.
//
// Returns an error if the player isn't reachable after the timeout duration.
func PingPlayer(port int) (*osc.Client, error) {
log.Debug().
Int("port", port).
Msg("Waiting for player to respond to ping.")
client := osc.NewClient("localhost", port, osc.ClientProtocol(osc.TCP))
err := util.Await(
func() error {
return client.Send(osc.NewMessage("/ping"))
},
reasonableTimeout,
)
return client, err
}
// FindAvailablePlayer finds a player that is in an available state, confirms
// that it can be reached by sending a ping, and returns current information
// about the player's state.
//
// In the case where a player appears to exist in the available state, but is
// not reachable (e.g. if the player suddenly died and wasn't able to clean up
// its own state file), FindAvailablePlayer will delete that player's state file
// and try again with other players that appear to be in the available state,
// and will also fill the player pool to help ensure that we don't run out of
// players.
//
// Returns `ErrNoPlayersAvailable` if no player is currently in an available
// state.
func FindAvailablePlayer() (PlayerState, error) {
players, err := ReadPlayerStates()
if err != nil {
return PlayerState{}, err
}
for _, player := range players {
if player.State != "ready" {
continue
}
if _, err := PingPlayer(player.Port); err != nil {
log.Warn().
Interface("player", player).
Err(err).
Msg("Failed to reach player process. Will try another one.")
if err := DeletePlayerStateFile(player.ID); err != nil {
return PlayerState{}, err
}
if err := FillPlayerPool(); err != nil {
return PlayerState{}, err
}
return FindAvailablePlayer()
}
return player, nil
}
return PlayerState{}, ErrNoPlayersAvailable
}
// FindPlayerByID returns the current state of the player process with the
// provided ID.
//
// Returns an error if no player is found with that ID, or if something goes
// wrong in the process of reading the player states.
func FindPlayerByID(id string) (PlayerState, error) {
players, err := ReadPlayerStates()
if err != nil {
return PlayerState{}, err
}
for _, player := range players {
if player.ID == id {
return player, nil
}
}
// FIXME: repl/player_management.go does a check whether the error message
// returned by FindPlayerByID starts with "No player was found". If we ever
// change the verbiage here, we'll need to adjust that other code accordingly.
//
// TODO: Consider adding an optional error code to UserFacingErrors that we
// could check for instead.
return PlayerState{}, help.UserFacingErrorf(
`No player was found with the ID %s.
To list the current player processes, you can run %s.
You can also omit the %s / %s option, and Alda will find a player process
for you automatically.`,
color.Aurora.BrightYellow(id),
color.Aurora.BrightYellow("alda ps"),
color.Aurora.BrightYellow("-i"),
color.Aurora.BrightYellow("--player-id"),
)
}
// AldaPlayerPath returns the absolute path to the `alda-player` executable and
// whether the player is the same version as the client.
//
// To determine whether the player is the same version of the client, we run
// `alda-player info` and parse the version number from the output. If the
// versions don't match, a warning is logged that includes instructions about
// how to fix this situation.
//
// Returns `ErrAldaPlayerNotFoundOnPath` if `alda-player` is not found on the
// PATH.
//
// Returns an unspecified error if something else goes wrong.
func AldaPlayerPath() (playerPath string, sameVersion bool, err error) {
aldaPlayer, err := exec.LookPath("alda-player")
if err != nil {
return "", false, ErrAldaPlayerNotFoundOnPath
}
infoCmd := exec.Command(aldaPlayer, "info")
infoCmd.Stdout = nil
infoCmd.Stderr = nil
outputBytes, err := infoCmd.Output()
if err != nil {
return "", false, err
}
output := string(outputBytes)
re := regexp.MustCompile(`alda-player ([^\r\n]+)`)
captured := re.FindStringSubmatch(output)
if len(captured) < 2 {
return "", false, fmt.Errorf(
"unable to parse player version from output: %s", output,
)
}
// captured[0] is "alda-player X.X.X", captured[1] is "X.X.X"
playerVersion := captured[1]
if playerVersion != generated.ClientVersion {
log.Warn().
Str("clientVersion", generated.ClientVersion).
Str("playerVersion", playerVersion).
Msg("`alda` and `alda-player` are different versions. " +
"Run `alda doctor` to install the correct version of `alda-player`.")
}
return aldaPlayer, playerVersion == generated.ClientVersion, nil
}
// spawnPlayer spawns an Alda player process, using the provided absolute path
// to `alda-player`.
//
// Note that this path can be obtained by calling `AldaPlayerPath()`, which also
// checks that the client and player versions are the same and logs a warning if
// they aren't.
func spawnPlayer(playerPath string) error {
runCmd := exec.Command(playerPath, "run")
if err := runCmd.Start(); err != nil {
return err
}
log.Info().Msg("Spawned player process.")
return nil
}
// FillPlayerPool ensures that a minimum desired number of player processes is
// available. Spawns as many player processes as it takes to make that happen.
//
// Returns an error if something goes wrong.
func FillPlayerPool() error {
// If ALDA_DISABLE_SPAWNING is set to true, we do nothing.
//
// This is useful for CI/CD purposes. (See .circleci/config.yml.)
if os.Getenv("ALDA_DISABLE_SPAWNING") == "yes" {
log.Info().
Str("ALDA_DISABLE_SPAWNING", "yes").
Msg("Skipping filling the player pool.")
return nil
}
playerPath, _, err := AldaPlayerPath()
if err != nil {
return err
}
players, err := ReadPlayerStates()
if err != nil {
return err
}
availablePlayers := 0
for _, player := range players {
if player.State == "ready" || player.State == "starting" {
availablePlayers++
}
}
desiredAvailablePlayers := 3
playersToStart := desiredAvailablePlayers - availablePlayers
log.Debug().
Int("availablePlayers", availablePlayers).
Int("desiredAvailablePlayers", desiredAvailablePlayers).
Int("playersToStart", playersToStart).
Msg("Spawning players.")
results := []<-chan error{}
for i := 0; i < playersToStart; i++ {
result := make(chan error)
results = append(results, result)
go func() { result <- spawnPlayer(playerPath) }()
}
for _, result := range results {
err := <-result
if err != nil {
return err
}
}
return nil
}
// Alda starts player processes in the background as needed when running
// (almost) any command. Most of the time, this is totally transparent to the
// user, as when we get to this point, there is already a player process
// available, so playback is immediate.
//
// However, on the very first run (or the first run after a period of
// inactivity), it's easy for there not to be any Alda player processes
// available yet, so there is a brief (but noticeable) pause before playback
// starts.
//
// To avoid making it look like Alda is "hanging" here while we wait for player
// processes come up, we print a message to make it clear what we're waiting
// for.
func StartingPlayerProcesses() {
if _, err := FindAvailablePlayer(); err == ErrNoPlayersAvailable {
fmt.Fprintln(os.Stderr, "Starting player processes...")
}
}