| 1 | package bakeoff |
| 2 | |
| 3 | import ( |
| 4 | "os/exec" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // AsleepFromLogs reads the platform's own events for host in `carlos logs` |
| 9 | // output: the instance is asleep when its latest event is "stopping" or |
| 10 | // "exited", and awake after "woke". (The health column in `carlos |
| 11 | // instances list` lags by minutes, so it can't be trusted for this.) |
| 12 | func AsleepFromLogs(logs, host string) bool { |
| 13 | asleep := false |
| 14 | for _, line := range strings.Split(logs, "\n") { |
| 15 | if !strings.Contains(line, "[platform] "+host+" ") { |
| 16 | continue |
| 17 | } |
| 18 | switch { |
| 19 | case strings.Contains(line, "instance woke"): |
| 20 | asleep = false |
| 21 | case strings.Contains(line, "instance stopping"), strings.Contains(line, "instance exited"): |
| 22 | asleep = true |
| 23 | } |
| 24 | } |
| 25 | return asleep |
| 26 | } |
| 27 | |
| 28 | // IsAsleep asks the platform's log whether the instance at host is asleep. |
| 29 | func IsAsleep(app, host string) (bool, error) { |
| 30 | out, err := exec.Command("carlos", "logs", "--account", "bfj", "--app", app).CombinedOutput() |
| 31 | if err != nil { |
| 32 | return false, err |
| 33 | } |
| 34 | return AsleepFromLogs(string(out), host), nil |
| 35 | } |
| 36 | |
| 37 | // MeasureWake times the first request to a sleeping instance. |
| 38 | func MeasureWake(s *Session, path string) (float64, error) { |
| 39 | return s.timeToFirstByte(path) |
| 40 | } |
| 41 | |