Beispiel #1
0
func (fh *ForwardHandler) HandleStatusEvent(w http.ResponseWriter, body []byte) {
	task, err := tasks.ParseTask(body)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintln(w, err.Error())
		log.Printf("[ERROR] body generated error: %s", err.Error())
		return
	}

	switch task.TaskStatus {
	case "TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST":
		err = fh.consul.DeleteTask(task)
	case "TASK_STAGING", "TASK_STARTING", "TASK_RUNNING":
		err = fh.consul.UpdateTask(task)
	default:
		err = errors.New("unknown task status")
	}

	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintln(w, err.Error())
	} else {
		w.WriteHeader(200)
		fmt.Fprintln(w, "OK")
	}
}
Beispiel #2
0
func (fh *ForwardHandler) HandleStatusEvent(w http.ResponseWriter, body []byte) {
	// for every other use of Tasks, Marathon uses the "id" field for the task ID.
	// Here, it uses "taskId", with most of the other fields being equal. We'll
	// just swap "taskId" for "id" in the body so that we can successfully parse
	// incoming events.
	body = bytes.Replace(body, []byte("taskId"), []byte("id"), -1)

	task, err := tasks.ParseTask(body)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintln(w, err.Error())
		log.Printf("[ERROR] body generated error: %s", err.Error())
		return
	}

	switch task.TaskStatus {
	case "TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST":
		err = fh.consul.DeleteTask(task)
	case "TASK_STAGING", "TASK_STARTING", "TASK_RUNNING":
		err = fh.consul.UpdateTask(task)
	default:
		err = errors.New("unknown task status")
	}

	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintln(w, err.Error())
	} else {
		w.WriteHeader(200)
		fmt.Fprintln(w, "OK")
	}
}
Beispiel #3
0
func TestForwardHandlerHandleStatusEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	// deletes
	for _, status := range []string{"TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST"} {
		tempBody := tempTaskBody(status)

		err := consul.UpdateTask(testTask)
		assert.Nil(t, err)

		// test
		recorder := httptest.NewRecorder()
		handler.HandleStatusEvent(recorder, tempBody)
		assert.Equal(t, 200, recorder.Code)
		assert.Equal(t, "OK\n", recorder.Body.String())

		// assert
		result, _, err := kv.Get(testTask.Key())
		assert.Nil(t, err)
		assert.Nil(t, result)
	}

	// puts
	for _, status := range []string{"TASK_STAGING", "TASK_STARTING", "TASK_RUNNING"} {
		tempBody := tempTaskBody(status)
		tempTask, _ := tasks.ParseTask(tempBody)

		// test
		recorder := httptest.NewRecorder()
		handler.HandleStatusEvent(recorder, tempBody)
		assert.Equal(t, 200, recorder.Code)
		assert.Equal(t, "OK\n", recorder.Body.String())

		// assert
		result, _, err := kv.Get(tempTask.Key())
		assert.Nil(t, err)
		assert.Equal(t, result, tempTask.KV())

		// cleanup
		_, err = kv.Delete(testTask.Key())
		assert.Nil(t, err)
	}

	// bad status
	tempBody := tempTaskBody("TASK_BATMAN")
	recorder := httptest.NewRecorder()
	handler.HandleStatusEvent(recorder, tempBody)
	assert.Equal(t, 500, recorder.Code)
	assert.Equal(t, "unknown task status\n", recorder.Body.String())
}
Beispiel #4
0
func (fh *ForwardHandler) HandleStatusEvent(body []byte) error {
	// for every other use of Tasks, Marathon uses the "id" field for the task ID.
	// Here, it uses "taskId", with most of the other fields being equal. We'll
	// just swap "taskId" for "id" in the body so that we can successfully parse
	// incoming events.
	body = bytes.Replace(body, []byte("taskId"), []byte("id"), -1)

	task, err := tasks.ParseTask(body)

	if err != nil {
		return err
	}

	switch task.TaskStatus {
	case "TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST":
		err = fh.consul.DeleteTask(task)
	case "TASK_STAGING", "TASK_STARTING", "TASK_RUNNING":
		err = fh.consul.UpdateTask(task)
	default:
		err = errors.New("unknown task status")
	}
	return err
}