func getOldestPendingTaskHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") tasks, err := getAllPendingTasks() if err != nil { skutil.ReportError(w, r, err, fmt.Sprintf("Failed to get all pending tasks: %v", err)) return } var oldestTask task_common.Task for _, task := range tasks { if oldestTask == nil { oldestTask = task } else if oldestTask.GetCommonCols().TsAdded.Int64 < task.GetCommonCols().TsAdded.Int64 { oldestTask = task } } oldestTaskJsonRepr := map[string]task_common.Task{} if oldestTask != nil { oldestTaskJsonRepr[oldestTask.GetTaskName()] = oldestTask } if err := json.NewEncoder(w).Encode(oldestTaskJsonRepr); err != nil { skutil.ReportError(w, r, err, fmt.Sprintf("Failed to encode JSON: %v", err)) return } }
// GetOldestPendingTask returns the oldest pending task of any type. func GetOldestPendingTask() (task_common.Task, error) { var oldestTask task_common.Task for _, task := range task_types.Prototypes() { query := fmt.Sprintf("SELECT * FROM %s WHERE ts_completed IS NULL ORDER BY ts_added LIMIT 1;", task.TableName()) if err := db.DB.Get(task, query); err == sql.ErrNoRows { continue } else if err != nil { return nil, fmt.Errorf("Failed to query DB: %v", err) } if oldestTask == nil { oldestTask = task } else if oldestTask.GetCommonCols().TsAdded.Int64 > task.GetCommonCols().TsAdded.Int64 { oldestTask = task } } return oldestTask, nil }