Beispiel #1
0
func killProcessHF(w http.ResponseWriter, r *http.Request, p rest.Params) error {
	pid, err := parsePid(p.Get("pid"))
	if err != nil {
		return rest.BadRequest(err)
	}
	if err := Kill(pid); err != nil {
		return asHttpError(err)
	}
	return nil
}
Beispiel #2
0
func getProcessLogsHF(w http.ResponseWriter, r *http.Request, p rest.Params) error {
	pid, err := parsePid(p.Get("pid"))
	if err != nil {
		return rest.BadRequest(err)
	}

	// Parse 'from', if 'from' is not specified then read all the logs from the start
	// if 'from' format is different from the DATE_TIME_FORMAT then return 400
	from, err := parseTime(r.URL.Query().Get("from"), time.Time{})
	if err != nil {
		return rest.BadRequest(errors.New("Bad format of 'from', " + err.Error()))
	}

	// Parse 'till', if 'till' is not specified then 'now' is used for it
	// if 'till' format is different from the DATE_TIME_FORMAT then return 400
	till, err := parseTime(r.URL.Query().Get("till"), time.Now())
	if err != nil {
		return rest.BadRequest(errors.New("Bad format of 'till', " + err.Error()))
	}

	logs, err := ReadLogs(pid, from, till)
	if err != nil {
		return asHttpError(err)
	}

	// limit logs from the latest to the earliest
	// limit - how many the latest logs will be present
	// skip - how many log lines should be skipped from the end
	limit := restutil.IntQueryParam(r, "limit", DefaultLogsPerPageLimit)
	skip := restutil.IntQueryParam(r, "skip", 0)
	if limit < 1 {
		return rest.BadRequest(errors.New("Required 'limit' to be > 0"))
	}
	if skip < 0 {
		return rest.BadRequest(errors.New("Required 'skip' to be >= 0"))
	}
	len := len(logs)
	fromIdx := int(math.Max(float64(len-limit-skip), 0))
	toIdx := len - int(math.Min(float64(skip), float64(len)))

	// Respond with an appropriate logs format, default json
	format := r.URL.Query().Get("format")
	switch strings.ToLower(format) {
	case "text":
		for _, item := range logs[fromIdx:toIdx] {
			line := fmt.Sprintf("[%s] %s \t %s\n", item.Kind, item.Time.Format(DateTimeFormat), item.Text)
			io.WriteString(w, line)
		}
	default:
		return restutil.WriteJson(w, logs[fromIdx:toIdx])
	}
	return nil
}
Beispiel #3
0
func getProcessHF(w http.ResponseWriter, r *http.Request, p rest.Params) error {
	pid, err := parsePid(p.Get("pid"))
	if err != nil {
		return rest.BadRequest(err)
	}

	process, err := Get(pid)
	if err != nil {
		return asHttpError(err)
	}
	return restutil.WriteJson(w, process)
}