Beispiel #1
0
// repeatedTasksScheduler looks for all tasks that contain repeat_after_days
// set to > 0 and schedules them when the specified time comes.
// The function does the following:
// 1. Look for tasks that need to be scheduled in the next 5 minutes.
// 2. Loop over these tasks.
//   2.1 Schedule the task again and set repeat_after_days to what it
//       originally was.
//   2.2 Update the original task and set repeat_after_days to 0 since the
//       newly created task will now replace it.
func repeatedTasksScheduler() {

	for _ = range time.Tick(*tasksSchedulerWaitTime) {
		// Loop over all tasks to find tasks which need to be scheduled.
		for _, prototype := range task_types.Prototypes() {

			query, args := task_common.DBTaskQuery(prototype,
				task_common.QueryParams{
					FutureRunsOnly: true,
					Offset:         0,
					Size:           task_common.MAX_PAGE_SIZE,
				})
			glog.Infof("Running %s", query)
			data, err := prototype.Select(query, args...)
			if err != nil {
				glog.Errorf("Failed to query %s tasks: %v", prototype.GetTaskName(), err)
				continue
			}

			tasks := task_common.AsTaskSlice(data)
			for _, task := range tasks {
				addedTime := ctutil.GetTimeFromTs(strconv.FormatInt(task.GetCommonCols().TsAdded.Int64, 10))
				scheduledTime := addedTime.Add(time.Duration(task.GetCommonCols().RepeatAfterDays) * time.Hour * 24)

				cutOffTime := time.Now().UTC().Add(*tasksSchedulerWaitTime)
				if scheduledTime.Before(cutOffTime) {
					addTaskVars := task.GetPopulatedAddTaskVars()
					if _, err := task_common.AddTask(addTaskVars); err != nil {
						glog.Errorf("Failed to add task %v: %v", task, err)
						continue
					}

					taskVars := task.GetUpdateTaskVars()
					taskVars.GetUpdateTaskCommonVars().Id = task.GetCommonCols().Id
					taskVars.GetUpdateTaskCommonVars().ClearRepeatAfterDays()
					if err := task_common.UpdateTask(taskVars, task.TableName()); err != nil {
						glog.Errorf("Failed to update task %v: %v", task, err)
						continue
					}
				}
			}
		}
	}
}
func addTrybotTaskHandler(w http.ResponseWriter, r *http.Request) {
	data, err := webhook.AuthenticateRequest(r)
	if err != nil {
		if data == nil {
			skutil.ReportError(w, r, err, "Failed to read add request")
			return
		}
		if !ctfeutil.UserHasAdminRights(r) {
			skutil.ReportError(w, r, err, "Failed authentication")
			return
		}
	}

	trybotTask := TrybotTask{}
	if err := json.Unmarshal(data, &trybotTask); err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to add %v trybot task", trybotTask))
		return
	}

	task := &trybotTask.TaskVars
	// Add patch data to the task.
	detail, err := getCLDetail(trybotTask.Issue)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	patchsetID, err := strconv.Atoi(trybotTask.PatchsetID)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	patch, err := getCLPatch(detail, patchsetID)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	clData, err := gatherCLData(detail, patch)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}

	task.Description = fmt.Sprintf("Trybot run for http://codereview.chromium.org/%s#ps%s", clData["cl"], clData["patchset"])
	if val, ok := clData["chromium_patch"]; ok {
		task.ChromiumPatch = val
	}
	if val, ok := clData["skia_patch"]; ok {
		task.SkiaPatch = val
	}

	task.GetAddTaskCommonVars().TsAdded = ctutil.GetCurrentTs()

	taskID, err := task_common.AddTask(task)
	if err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to insert %T task", task))
		return
	}

	w.Header().Set("Content-Type", "application/json")
	jsonResponse := map[string]interface{}{
		"taskID": taskID,
	}
	if err := json.NewEncoder(w).Encode(jsonResponse); err != nil {
		skutil.ReportError(w, r, err, "Failed to encode JSON")
		return
	}
}