package bakeoff import ( "os/exec" "strings" ) // AsleepFromLogs reads the platform's own events for host in `carlos logs` // output: the instance is asleep when its latest event is "stopping" or // "exited", and awake after "woke". (The health column in `carlos // instances list` lags by minutes, so it can't be trusted for this.) func AsleepFromLogs(logs, host string) bool { asleep := false for _, line := range strings.Split(logs, "\n") { if !strings.Contains(line, "[platform] "+host+" ") { continue } switch { case strings.Contains(line, "instance woke"): asleep = false case strings.Contains(line, "instance stopping"), strings.Contains(line, "instance exited"): asleep = true } } return asleep } // IsAsleep asks the platform's log whether the instance at host is asleep. func IsAsleep(app, host string) (bool, error) { out, err := exec.Command("carlos", "logs", "--account", "bfj", "--app", app).CombinedOutput() if err != nil { return false, err } return AsleepFromLogs(string(out), host), nil } // MeasureWake times the first request to a sleeping instance. func MeasureWake(s *Session, path string) (float64, error) { return s.timeToFirstByte(path) }