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.removeInfo) if err != nil { err = bosherr.WrapErrorf(err, "Create Task Failed %s", req.Method) dispatcher.logger.Error(actionDispatcherLogTag, err.Error()) return boshhandler.NewExceptionResponse(err) } taskInfo := boshtask.Info{ TaskID: task.ID, Method: req.Method, Payload: req.GetPayload(), } err = dispatcher.taskManager.AddInfo(taskInfo) if err != nil { err = bosherr.WrapErrorf(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.WrapErrorf(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.StateValue{ AgentTaskID: task.ID, State: task.State, }) }
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) } 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.Errorf("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.WrapErrorf(err, "Action Failed %s", req.Method) dispatcher.logger.Error(actionDispatcherLogTag, err.Error()) return boshhandler.NewExceptionResponse(err) } return boshhandler.NewValueResponse(value) }
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.Errorf("unknown message %s", req.Method)) } dispatcher.logger.Info(actionDispatcherLogTag, "Received request with action %s", req.Method) if action.IsLoggable() { dispatcher.logger.DebugWithDetails(actionDispatcherLogTag, "Payload", req.Payload) } if action.IsAsynchronous() { return dispatcher.dispatchAsynchronousAction(action, req) } return dispatcher.dispatchSynchronousAction(action, req) }