示例#1
0
文件: queue.go 项目: ningjh/tsuru
// ensureAppIsStarted make sure that the app and all units present in the given
// message are started.
func ensureAppIsStarted(msg *queue.Message) (App, error) {
	app, err := GetByName(msg.Args[0])
	if err != nil {
		return App{}, fmt.Errorf("Error handling %q: app %q does not exist.", msg.Action, msg.Args[0])
	}
	units := getUnits(app, msg.Args[1:])
	if len(msg.Args) > 1 && len(units) == 0 {
		format := "Error handling %q for the app %q: unknown units in the message. Deleting it..."
		return *app, fmt.Errorf(format, msg.Action, app.Name)
	}
	if !app.Available() || !units.Started() {
		format := "Error handling %q for the app %q:"
		uState := units.State()
		if uState == "error" || uState == "down" {
			format += fmt.Sprintf(" units are in %q state.", uState)
		} else {
			msg.Fail()
			format += " all units must be started."
		}
		return *app, fmt.Errorf(format, msg.Action, app.Name)
	}
	return *app, nil
}