func (h *TaskHandler) GetByGuid(w http.ResponseWriter, req *http.Request) {
	guid := req.FormValue(":task_guid")
	logger := h.logger.Session("get-by-guid", lager.Data{
		"TaskGuid": guid,
	})

	if guid == "" {
		err := errors.New("task_guid missing from request")
		logger.Error("missing-task-guid", err)
		writeBadRequestResponse(w, receptor.InvalidRequest, err)
		return
	}

	task, err := h.bbs.TaskByGuid(logger, guid)
	if err == bbserrors.ErrStoreResourceNotFound {
		h.logger.Error("failed-to-fetch-task", err)
		writeTaskNotFoundResponse(w, guid)
		return
	}

	if err != nil {
		if err == bbserrors.ErrStoreResourceNotFound {
			h.logger.Error("failed-to-fetch-task", err)
			writeTaskNotFoundResponse(w, guid)
			return
		}

		h.logger.Error("failed-to-fetch-task", err)
		writeUnknownErrorResponse(w, err)
		return
	}

	writeJSONResponse(w, http.StatusOK, serialization.TaskToResponse(task))
}
func (t *taskWorker) handleCompletedTask(task *models.Task) {
	logger := t.logger.WithData(lager.Data{"task-guid": task.TaskGuid})

	if task.CompletionCallbackUrl != "" {
		var err error

		logger.Info("resolving-task")
		err = t.taskdb.ResolvingTask(task.TaskGuid)
		if err != nil {
			logger.Error("marking-task-as-resolving-failed", err)
			return
		}

		logger = logger.WithData(lager.Data{"callback_url": task.CompletionCallbackUrl})

		json, err := json.Marshal(serialization.TaskToResponse(task))
		if err != nil {
			logger.Error("marshalling-task-failed", err)
			return
		}

		var statusCode int

		for i := 0; i < MAX_RETRIES; i++ {
			request, err := http.NewRequest("POST", task.CompletionCallbackUrl, bytes.NewReader(json))
			if err != nil {
				logger.Error("building-request-failed", err)
				return
			}

			request.Header.Set("Content-Type", "application/json")

			response, err := t.httpClient.Do(request)
			if err != nil {
				matched, _ := regexp.MatchString("use of closed network connection", err.Error())
				if matched {
					continue
				}
				logger.Error("doing-request-failed", err)
				return
			}

			statusCode = response.StatusCode
			if shouldResolve(statusCode) {
				err = t.taskdb.ResolveTask(task.TaskGuid)
				if err != nil {
					logger.Error("resolving-task-failed", err)
					return
				}

				logger.Info("resolved-task", lager.Data{"status_code": statusCode})
				return
			}
		}

		logger.Info("callback-failed", lager.Data{"status_code": statusCode})
	}
}
func writeTaskResponse(w http.ResponseWriter, logger lager.Logger, tasks []models.Task, err error) {
	if err != nil {
		logger.Error("failed-to-fetch-tasks", err)
		writeUnknownErrorResponse(w, err)
		return
	}

	taskResponses := make([]receptor.TaskResponse, 0, len(tasks))
	for _, task := range tasks {
		taskResponses = append(taskResponses, serialization.TaskToResponse(task))
	}

	writeJSONResponse(w, http.StatusOK, taskResponses)
}
Beispiel #4
0
				},
			}
		})

		It("serializes the state", func() {
			EXPECTED_STATE_MAP := map[models.TaskState]string{
				models.TaskStateInvalid:   "INVALID",
				models.TaskStatePending:   "PENDING",
				models.TaskStateRunning:   "RUNNING",
				models.TaskStateCompleted: "COMPLETED",
				models.TaskStateResolving: "RESOLVING",
			}

			for modelState, jsonState := range EXPECTED_STATE_MAP {
				task.State = modelState
				Expect(serialization.TaskToResponse(task).State).To(Equal(jsonState))
			}
		})

		It("serializes the task's fields", func() {
			actualResponse := serialization.TaskToResponse(task)

			expectedResponse := receptor.TaskResponse{
				TaskGuid: "the-task-guid",
				Domain:   "the-domain",
				RootFS:   "the-rootfs",
				CellID:   "the-cell-id",
				Action: &models.UploadAction{
					From: "from",
					To:   "to",
				},
					request, err := http.NewRequest("", "http://example.com?domain=domain-1", nil)
					Expect(err).NotTo(HaveOccurred())

					handler.GetAll(responseRecorder, request)
					Expect(responseRecorder.Code).To(Equal(http.StatusOK))

					var tasks []receptor.TaskResponse
					err = json.Unmarshal(responseRecorder.Body.Bytes(), &tasks)
					Expect(err).NotTo(HaveOccurred())

					Expect(fakeClient.TasksByDomainCallCount()).To(Equal(1))
					actualDomain := fakeClient.TasksByDomainArgsForCall(0)
					Expect(actualDomain).To(Equal("domain-1"))

					expectedTasks := []receptor.TaskResponse{
						serialization.TaskToResponse(domain1Task),
					}
					Expect(tasks).To(ConsistOf(expectedTasks))
				})
			})

			Context("when a domain query param is not provided", func() {
				It("gets all tasks", func() {
					handler.GetAll(responseRecorder, newTestRequest(""))
					Expect(responseRecorder.Code).To(Equal(http.StatusOK))

					var tasks []receptor.TaskResponse
					err := json.Unmarshal(responseRecorder.Body.Bytes(), &tasks)
					Expect(err).NotTo(HaveOccurred())

					expectedTasks := []receptor.TaskResponse{