예제 #1
0
func (dispatcher concreteActionDispatcher) Dispatch(req boshhandler.Request) (resp boshhandler.Response) {
	action, err := dispatcher.actionFactory.Create(req.Method)

	switch {
	case err != nil:
		resp = boshhandler.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 = boshhandler.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 = boshhandler.NewExceptionResponse(err.Error())
			dispatcher.logger.Error("Action Dispatcher", err.Error())
			return
		}
		resp = boshhandler.NewValueResponse(value)
	}
	return
}
func (dispatcher concreteActionDispatcher) dispatchAsynchronousAction(
	action boshaction.Action,
	req boshhandler.Request,
) boshhandler.Response {
	dispatcher.logger.Info(actionDispatcherLogTag, "Running async action %s", req.Method)

	var task boshtask.Task
	var err error

	runTask := func() (interface{}, error) {
		return dispatcher.actionRunner.Run(action, req.GetPayload())
	}

	cancelTask := func(_ boshtask.Task) error { return action.Cancel() }

	// Certain long-running tasks (e.g. configure_networks) must be resumed
	// after agent restart so that API consumers do not need to know
	// if agent is restarted midway through the task.
	if action.IsPersistent() {
		dispatcher.logger.Info(actionDispatcherLogTag, "Running persistent action %s", req.Method)
		task, err = dispatcher.taskService.CreateTask(runTask, cancelTask, dispatcher.removeTaskInfo)
		if err != nil {
			err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
			dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
			return boshhandler.NewExceptionResponse(err)
		}

		taskInfo := boshtask.TaskInfo{
			TaskID:  task.ID,
			Method:  req.Method,
			Payload: req.GetPayload(),
		}

		err = dispatcher.taskManager.AddTaskInfo(taskInfo)
		if err != nil {
			err = bosherr.WrapError(err, "Action Failed %s", req.Method)
			dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
			return boshhandler.NewExceptionResponse(err)
		}
	} else {
		task, err = dispatcher.taskService.CreateTask(runTask, cancelTask, nil)
		if err != nil {
			err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
			dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
			return boshhandler.NewExceptionResponse(err)
		}
	}

	dispatcher.taskService.StartTask(task)

	return boshhandler.NewValueResponse(boshtask.TaskStateValue{
		AgentTaskID: task.ID,
		State:       task.State,
	})
}
예제 #3
0
func (d *dummyNatsJobSupervisor) statusHandler(req boshhandler.Request) boshhandler.Response {
	switch req.Method {
	case "set_dummy_status":
		// Do not unmarshal message until determining its method
		var body map[string]string

		err := json.Unmarshal(req.GetPayload(), &body)
		if err != nil {
			return boshhandler.NewExceptionResponse(err.Error())
		}

		d.status = body["status"]

		if d.status == "failing" && d.jobFailureHandler != nil {
			d.jobFailureHandler(boshalert.MonitAlert{
				ID:          "fake-monit-alert",
				Service:     "fake-monit-service",
				Event:       "failing",
				Action:      "start",
				Date:        "Sun, 22 May 2011 20:07:41 +0500",
				Description: "fake-monit-description",
			})
		}

		return boshhandler.NewValueResponse("ok")
	default:
		return nil
	}
}
func (dispatcher concreteActionDispatcher) Dispatch(req boshhandler.Request) boshhandler.Response {
	action, err := dispatcher.actionFactory.Create(req.Method)
	if err != nil {
		dispatcher.logger.Error(actionDispatcherLogTag, "Unknown action %s", req.Method)
		return boshhandler.NewExceptionResponse(bosherr.New("unknown message %s", req.Method))
	}

	if action.IsAsynchronous() {
		return dispatcher.dispatchAsynchronousAction(action, req)
	}

	return dispatcher.dispatchSynchronousAction(action, req)
}
func (dispatcher concreteActionDispatcher) dispatchSynchronousAction(
	action boshaction.Action,
	req boshhandler.Request,
) boshhandler.Response {
	dispatcher.logger.Info(actionDispatcherLogTag, "Running sync action %s", req.Method)

	value, err := dispatcher.actionRunner.Run(action, req.GetPayload())
	if err != nil {
		err = bosherr.WrapError(err, "Action Failed %s", req.Method)
		dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
		return boshhandler.NewExceptionResponse(err)
	}

	return boshhandler.NewValueResponse(value)
}
예제 #6
0
func (dispatcher concreteActionDispatcher) Dispatch(req boshhandler.Request) boshhandler.Response {
	action, err := dispatcher.actionFactory.Create(req.Method)

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

	case action.IsAsynchronous():
		dispatcher.logger.Error("Action Dispatcher", "Running async action %s", req.Method)

		var task boshtask.Task

		runTask := func() (interface{}, error) {
			return dispatcher.actionRunner.Run(action, req.GetPayload())
		}

		// Certain long-running tasks (e.g. configure_networks) must be resumed
		// after agent restart so that API consumers do not need to know
		// if agent is restarted midway through the task.
		if action.IsPersistent() {
			dispatcher.logger.Error("Action Dispatcher", "Running persistent action %s", req.Method)
			task, err = dispatcher.taskService.CreateTask(runTask, dispatcher.removeTaskInfo)
			if err != nil {
				err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
				dispatcher.logger.Error("Action Dispatcher", err.Error())
				return boshhandler.NewExceptionResponse(err.Error())
			}

			taskInfo := boshtask.TaskInfo{
				TaskId:  task.Id,
				Method:  req.Method,
				Payload: req.GetPayload(),
			}

			err = dispatcher.taskManager.AddTaskInfo(taskInfo)
			if err != nil {
				err = bosherr.WrapError(err, "Action Failed %s", req.Method)
				dispatcher.logger.Error("Action Dispatcher", err.Error())
				return boshhandler.NewExceptionResponse(err.Error())
			}
		} else {
			task, err = dispatcher.taskService.CreateTask(runTask, nil)
			if err != nil {
				err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
				dispatcher.logger.Error("Action Dispatcher", err.Error())
				return boshhandler.NewExceptionResponse(err.Error())
			}
		}

		dispatcher.taskService.StartTask(task)

		return boshhandler.NewValueResponse(boshtask.TaskStateValue{
			AgentTaskId: task.Id,
			State:       task.State,
		})

	default:
		dispatcher.logger.Debug("Action Dispatcher", "Running sync action %s", req.Method)

		value, err := dispatcher.actionRunner.Run(action, req.GetPayload())
		if err != nil {
			err = bosherr.WrapError(err, "Action Failed %s", req.Method)
			dispatcher.logger.Error("Action Dispatcher", err.Error())
			return boshhandler.NewExceptionResponse(err.Error())
		}

		return boshhandler.NewValueResponse(value)
	}
}