Exemple #1
0
// GET: /job
// To do:
// - Iterate job queries
func (cr *JobController) ReadMany(cx *goweb.Context) {
	LogRequest(cx.Request)

	// Gather query params
	query := &Query{Li: cx.Request.URL.Query()}

	// Setup query and jobs objects
	q := bson.M{}
	jobs := core.Jobs{}

	// Gather params to make db query. Do not include the
	// following list.
	skip := map[string]int{"limit": 1, "skip": 1, "query": 1, "recent": 1}
	if query.Has("query") {
		for key, val := range query.All() {
			_, s := skip[key]
			if !s {
				queryvalues := strings.Split(val[0], ",")
				q[key] = bson.M{"$in": queryvalues}
			}
		}
	} else if query.Has("active") {
		q["state"] = core.JOB_STAT_INPROGRESS
	} else if query.Has("suspend") {
		q["state"] = core.JOB_STAT_SUSPEND
	}

	// Limit and skip. Set default if both are not specified
	if query.Has("limit") || query.Has("skip") || query.Has("recent") {
		var lim, off, recent int
		if query.Has("limit") {
			lim, _ = strconv.Atoi(query.Value("limit"))
		} else {
			lim = 100
		}
		if query.Has("skip") {
			off, _ = strconv.Atoi(query.Value("skip"))
		} else {
			off = 0
		}
		if query.Has("recent") {
			recent, _ = strconv.Atoi(query.Value("recent"))
		} else {
			recent = 0
		}
		var err error
		if recent > 0 {
			_, err = jobs.GetAllRecent(q, recent)
		} else {
			err = jobs.GetAllLimitOffset(q, lim, off)
		}
		if err != nil {
			logger.Error("err " + err.Error())
			cx.RespondWithError(http.StatusBadRequest)
			return
		}
	} else {
		// Get jobs from db
		err := jobs.GetAll(q)
		if err != nil {
			logger.Error("err " + err.Error())
			cx.RespondWithError(http.StatusBadRequest)
			return
		}
	}

	//getting real active (in-progress) job (some jobs are in "submitted" states but not in the queue,
	//because they may have failed and not recovered from the mongodb).
	if query.Has("active") {
		filtered_jobs := []core.Job{}
		act_jobs := core.QMgr.GetActiveJobs()
		length := jobs.Length()
		for i := 0; i < length; i++ {
			job := jobs.GetJobAt(i)
			if _, ok := act_jobs[job.Id]; ok {
				filtered_jobs = append(filtered_jobs, job)
			}
		}
		cx.RespondWithData(filtered_jobs)
		return
	}

	//geting suspended job in the current queue (excluding jobs in db but not in qmgr)
	if query.Has("suspend") {
		filtered_jobs := []core.Job{}
		suspend_jobs := core.QMgr.GetSuspendJobs()
		length := jobs.Length()
		for i := 0; i < length; i++ {
			job := jobs.GetJobAt(i)
			if _, ok := suspend_jobs[job.Id]; ok {
				filtered_jobs = append(filtered_jobs, job)
			}
		}
		cx.RespondWithData(filtered_jobs)
		return
	}
	cx.RespondWithData(jobs)
	return
}