Example #1
0
func handleHeartBeat(w http.ResponseWriter, r *http.Request) {
	token, err := ParseToken(r)
	if err != nil {
		WriteJsonErr(w, 401, err)
		return
	}

	matches := heartPat.FindStringSubmatch(r.URL.Path)
	if len(matches) != 2 {
		WriteJsonErr(w, 400, errors.New("Missing job id or failured id."))
		return
	}
	id := string(matches[1])
	if len(id) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}

	job := store.Job{Id: id, QueueId: token}
	err = job.HeartBeat()
	if err != nil {
		WriteJsonErr(w, 500, errors.New("Unable to commit heartbeat."))
		return
	}
	WriteJson(w, 201, map[string]string{"message": "OK"})
}
Example #2
0
func handleDeleteJob(w http.ResponseWriter, r *http.Request) {
	token, err := ParseToken(r)
	if err != nil {
		WriteJsonErr(w, 401, err)
		return
	}

	matches := jobPat.FindStringSubmatch(r.URL.Path)
	if len(matches) < 2 || len(matches[1]) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}
	// Trim the forward slash that came from the url path.
	id := string(matches[1][1:])
	if len(id) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}
	job := store.Job{Id: id, QueueId: token}
	if err := job.Delete(); err != nil {
		WriteJsonErr(w, 400, err)
		return
	}
	WriteJson(w, 200, job)
}
Example #3
0
func handleFailedJob(w http.ResponseWriter, r *http.Request) {
	token, err := ParseToken(r)
	if err != nil {
		WriteJsonErr(w, 401, err)
		return
	}

	matches := failPat.FindStringSubmatch(r.URL.Path)
	if len(matches) < 3 {
		WriteJsonErr(w, 400, errors.New("Missing job id or failured id."))
		return
	}
	jid := string(matches[1])
	fid := string(matches[2])
	if len(jid) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}
	if len(fid) == 0 {
		WriteJsonErr(w, 400, errors.New("Failure id not found."))
		return
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		WriteJsonErr(w, 500, err)
		return
	}

	f := store.FailedJob{Id: fid, JobId: jid, QueueId: token}

	if ok := f.Get(); ok {
		WriteJson(w, 200, f)
		return
	}

	err = json.Unmarshal(b, &f.Payload)
	if err != nil {
		WriteJsonErr(w, 400, err)
		return
	}

	j := store.Job{Id: jid}
	if ok := j.Get(); ok {
		f.JobPayload = j.Payload
	}

	err = f.Insert()
	if err != nil {
		WriteJsonErr(w, 500, err)
		return
	}
	WriteJson(w, 201, f)
}
Example #4
0
func handleNewJob(w http.ResponseWriter, r *http.Request) {
	token, err := ParseToken(r)
	if err != nil {
		WriteJsonErr(w, 401, err)
		return
	}

	matches := jobPat.FindStringSubmatch(r.URL.Path)
	if len(matches) < 2 || len(matches[1]) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}
	// Trim the forward slash that came from the url path.
	id := string(matches[1][1:])
	if len(id) == 0 {
		WriteJsonErr(w, 400, errors.New("Job id not found."))
		return
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		WriteJsonErr(w, 500, err)
		return
	}

	j := store.Job{Id: id, QueueId: token}
	err = json.Unmarshal(b, &j.Payload)
	if err != nil {
		WriteJsonErr(w, 400, err)
		return
	}

	if ok := j.Get(); ok {
		WriteJson(w, 200, j)
		return
	}

	err = j.Insert()
	if err != nil {
		WriteJsonErr(w, 500, err)
		return
	}
	WriteJson(w, 201, j)
}