Пример #1
0
func (dispatcher concreteActionDispatcher) Dispatch(req boshmbus.Request) (resp boshmbus.Response) {
	action, err := dispatcher.actionFactory.Create(req.Method)

	switch {
	case err != nil:
		resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
		dispatcher.logger.Error("Action Dispatcher", "Unknown action %s", req.Method)

	case action.IsAsynchronous():
		task := dispatcher.taskService.StartTask(func() (value interface{}, err error) {
			value, err = dispatcher.actionRunner.Run(action, req.GetPayload())
			return
		})

		resp = boshmbus.NewValueResponse(boshtask.TaskStateValue{
			AgentTaskId: task.Id,
			State:       task.State,
		})

	default:
		value, err := dispatcher.actionRunner.Run(action, req.GetPayload())

		if err != nil {
			err = bosherr.WrapError(err, "Action Failed %s", req.Method)
			resp = boshmbus.NewExceptionResponse(err.Error())
			dispatcher.logger.Error("Action Dispatcher", err.Error())
			return
		}
		resp = boshmbus.NewValueResponse(value)
	}
	return
}
Пример #2
0
func (a agent) runMbusHandler(errChan chan error) {
	handlerFunc := func(req boshmbus.Request) (resp boshmbus.Response) {
		switch req.Method {
		case "apply":
			task := a.taskService.StartTask(func() (err error) {
				action := a.actionFactory.Create(req.Method)
				_, err = action.Run(req.GetPayload())
				return
			})

			resp = boshmbus.NewValueResponse(TaskValue{
				AgentTaskId: task.Id,
				State:       string(task.State),
			})
		case "get_task", "ping", "get_state":
			action := a.actionFactory.Create(req.Method)
			value, err := action.Run(req.GetPayload())
			if err != nil {
				resp = boshmbus.NewExceptionResponse(err.Error())
				return
			}
			resp = boshmbus.NewValueResponse(value)
		default:
			resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
		}

		return
	}
	errChan <- a.mbusHandler.Run(handlerFunc)
}
Пример #3
0
func (a agent) runMbusHandler(errChan chan error) {
	handlerFunc := func(req boshmbus.Request) (resp boshmbus.Response) {
		switch req.Method {
		case "get_task", "ping", "get_state", "ssh", "start":
			action := a.actionFactory.Create(req.Method)
			value, err := action.Run(req.GetPayload())

			if err != nil {
				err = bosherr.WrapError(err, "Action Failed %s", req.Method)
				resp = boshmbus.NewExceptionResponse(err.Error())
				boshlog.Error("Agent", err.Error())
				return
			}
			resp = boshmbus.NewValueResponse(value)
		case "apply", "fetch_logs", "stop", "drain":
			task := a.taskService.StartTask(func() (value interface{}, err error) {
				action := a.actionFactory.Create(req.Method)
				value, err = action.Run(req.GetPayload())
				return
			})

			resp = boshmbus.NewValueResponse(TaskValue{
				AgentTaskId: task.Id,
				State:       string(task.State),
			})
		default:
			resp = boshmbus.NewExceptionResponse("unknown message %s", req.Method)
			boshlog.Error("Agent", "Unknown action %s", req.Method)
		}

		return
	}
	errChan <- a.mbusHandler.Run(handlerFunc)
}