Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: refactor timing task check (#259)

Luke Zhou8a54de580d8e
diff --git a/server/task.go b/server/task.go
index f198bbdbc9..550ff72e20 100644
--- a/server/task.go
+++ b/server/task.go
@@ -175,7 +175,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
}
}
- // create an activity for earliest allowed time update
+ // create an activity and trigger task check for earliest allowed time update
if updatedTask.EarliestAllowedTs != task.EarliestAllowedTs {
// create an activity
issueFind := &api.IssueFind{
@@ -209,6 +209,29 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create activity after updating task earliest allowed time: %v", updatedTask.Name)).SetInternal(err)
}
+
+ // trigger task check
+ payload, err = json.Marshal(api.TaskCheckEarliestAllowedTimePayload{
+ EarliestAllowedTs: *taskPatch.EarliestAllowedTs,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err))
+ }
+ _, err = s.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ CreatorID: api.SystemBotID,
+ TaskID: task.ID,
+ Type: api.TaskCheckGeneralEarliestAllowedTime,
+ Payload: string(payload),
+ SkipIfAlreadyTerminated: false,
+ })
+ if err != nil {
+ // It's OK if we failed to trigger a check, just emit an error log
+ s.l.Error("Failed to trigger timing check after changing task earliest allowed time",
+ zap.Int("task_id", task.ID),
+ zap.String("task_name", task.Name),
+ zap.Error(err),
+ )
+ }
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 198138eaac..7335274b1f 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -177,12 +177,10 @@ func (s *TaskCheckScheduler) Register(taskType string, executor TaskCheckExecuto
s.executors[taskType] = executor
}
-// Why this comment is such long and tedious? It a long story...
-// TL;DR
-// 1. We do not want anything happen if user does not set this field at the frontend.
-// 2. If user has specified this field once (even if she sets it to default latter), we will schedule one ever since.
-// 3. We only want to schedule one if the payload field is different from the last run (unless user forces one)
-// 4. Once the specified time has passed, schedule one immediately to unblock the task.
+// Returns true if we meet either of the following conditions:
+// 1. Task has a non-default value and no task check has run before (so we are about to kick of the check the first time)
+// 2. The specified EarliestAllowedTs has elapsed, so we need to rerun the check to unblock the task.
+// On the other hand, we would also rerun the check if user has modified EarliestAllowedTs. This is handled separately in the task patch handler.
func (s *TaskCheckScheduler) shouldScheduleTimingTaskCheck(ctx context.Context, task *api.Task, forceSchedule bool) (bool, error) {
statusList := []api.TaskCheckRunStatus{api.TaskCheckRunDone, api.TaskCheckRunFailed, api.TaskCheckRunRunning}
taskCheckType := api.TaskCheckGeneralEarliestAllowedTime
@@ -197,29 +195,15 @@ func (s *TaskCheckScheduler) shouldScheduleTimingTaskCheck(ctx context.Context,
return false, err
}
- // If there is not any taskcheck scheduled before, we should only schedule one if user has specified a non-default value.
+ // If there is not any task check scheduled before, we should only schedule one if user has specified a non-default value.
if len(taskCheckRunList) == 0 {
return task.EarliestAllowedTs != 0, nil
}
- // Do not schedule one if there is already one running.
- if taskCheckRunList[0].Status == api.TaskCheckRunRunning {
- return false, nil
- }
-
if forceSchedule {
return true, nil
}
- taskCheckPayload := &api.TaskCheckEarliestAllowedTimePayload{}
- if err := json.Unmarshal([]byte(taskCheckRunList[0].Payload), taskCheckPayload); err != nil {
- return false, err
- }
-
- if taskCheckPayload.EarliestAllowedTs != task.EarliestAllowedTs {
- return true, nil
- }
-
if time.Now().After(time.Unix(task.EarliestAllowedTs, 0)) {
checkResult := &api.TaskCheckRunResultPayload{}
if err := json.Unmarshal([]byte(taskCheckRunList[0].Result), checkResult); err != nil {