Skip to content

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

fix: correct the earliest allowed time logic to make it as optional (#243)

Luke Zhouef35f09c4f94
diff --git a/frontend/src/components/IssueSidebar.vue b/frontend/src/components/IssueSidebar.vue
index 36d3ec056c..735809bf35 100644
--- a/frontend/src/components/IssueSidebar.vue
+++ b/frontend/src/components/IssueSidebar.vue
@@ -36,7 +36,7 @@
</div>
<h2 class="textlabel flex items-center col-span-1 col-start-1">
- {{ $t("common.when") }}<span v-if="create" class="text-red-600">*</span>
+ {{ $t("common.when") }}
<div class="tooltip-wrapper">
<span class="tooltip w-60">{{
$t("task.earliest-allowed-time-hint")
@@ -46,11 +46,11 @@
</div>
</h2>
<div class="col-span-2">
- <!-- TODO: should added i18n support for naive-i18n & should fit in with the current theme -->
<n-date-picker
v-if="allowEditEarliestAllowedTime"
v-model:value="state.earliestAllowedTs"
- :is-date-disabled="isDatePassed"
+ :is-date-disabled="isDayPassed"
+ :placeholder="$t('task.earliest-allowed-time-unset')"
class="w-full"
type="datetime"
clearable
@@ -244,6 +244,7 @@ import {
} from "../types";
import { allTaskList, databaseSlug, isDBAOrOwner } from "../utils";
import { useRouter } from "vue-router";
+import moment from "moment";
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface LocalState {}
@@ -305,7 +306,7 @@ export default defineComponent({
const store = useStore();
const router = useRouter();
- const now = Date.now();
+ const now = new Date();
const state = reactive<LocalState>({
earliestAllowedTs: props.task.earliestAllowedTs
? props.task.earliestAllowedTs * 1000
@@ -451,7 +452,7 @@ export default defineComponent({
}
};
- const isDatePassed = (ts: number) => ts < now;
+ const isDayPassed = (ts: number) => !moment(ts).isSameOrAfter(now, "day");
return {
EMPTY_ID,
@@ -469,7 +470,7 @@ export default defineComponent({
isDatabaseCreated,
showDatabaseCreationLabel,
clickDatabase,
- isDatePassed,
+ isDayPassed,
};
},
});
diff --git a/frontend/src/components/IssueStatusTransitionButtonGroup.vue b/frontend/src/components/IssueStatusTransitionButtonGroup.vue
index 3726f66ede..11b45fb102 100644
--- a/frontend/src/components/IssueStatusTransitionButtonGroup.vue
+++ b/frontend/src/components/IssueStatusTransitionButtonGroup.vue
@@ -403,14 +403,6 @@ export default defineComponent({
return false;
}
- for (const stage of newIssue.pipeline.stageList) {
- for (const task of stage.taskList) {
- if (!task.earliestAllowedTs) {
- return false;
- }
- }
- }
-
if (
newIssue.type == "bb.issue.database.create" ||
newIssue.type == "bb.issue.database.schema.update"
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index ea85e0ee5d..fa2b4f2587 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -178,7 +178,10 @@ task:
connection: Connection
migration-schema: Migration schema
earliest-allowed-time: Earliest allowed time
- earliest-allowed-time-hint: '''@:{''common.when''}'' specifies the expected execution timing for this task'
+ earliest-allowed-time-hint: >-
+ '@:{'common.when'}' specifies the expected execution timing for this task.
+ If this field is not specified, the task will be executed once it has passed all other gating criteria.
+ earliest-allowed-time-unset: Unset
comment: Comment
invoker: Invoker
started: Started
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index c25d5c9e2f..e976e0f703 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -178,13 +178,14 @@ task:
connection: 连接
migration-schema: 迁移 schema
earliest-allowed-time: 最早执行时间
- earliest-allowed-time-hint: '''@:{''common.when''}'' 指定了该任务最早允许执行的时间'
+ earliest-allowed-time-hint: '''@:{''common.when''}'' 指定了该任务最早允许执行的时间。如果该字段没有被指定,则任务会在满足其他条件后立即执行。'
comment: 评论
invoker: 执行者
started: 开始于
ended: 结束于
view-migration: 查看迁移
view-migration-history: 查看迁移历史
+ earliest-allowed-time-unset: 未设置
banner:
demo-intro: 这是 Bytebase 的演示版本。
demo-intro-readonly: 这是 Bytebase 的只读演示版本
diff --git a/frontend/src/types/pipeline/task.ts b/frontend/src/types/pipeline/task.ts
index fdf5cfc7a0..9070d3e9c1 100644
--- a/frontend/src/types/pipeline/task.ts
+++ b/frontend/src/types/pipeline/task.ts
@@ -32,6 +32,10 @@ export type TaskGeneralPayload = {
statement: string;
};
+export type TaskEarliestAllowedTimePayload = {
+ earliestAllowedTs: number;
+};
+
export type TaskDatabaseCreatePayload = {
projectId: ProjectId;
statement: string;
@@ -56,7 +60,8 @@ export type TaskPayload =
| TaskGeneralPayload
| TaskDatabaseCreatePayload
| TaskDatabaseSchemaUpdatePayload
- | TaskDatabaseRestorePayload;
+ | TaskDatabaseRestorePayload
+ | TaskEarliestAllowedTimePayload;
export type Task = {
id: TaskId;
diff --git a/server/task_check_executor_timing.go b/server/task_check_executor_timing.go
index 0d05cbc6cc..4c3e7db6d8 100644
--- a/server/task_check_executor_timing.go
+++ b/server/task_check_executor_timing.go
@@ -43,6 +43,17 @@ func (exec *TaskCheckTimingExecutor) Run(ctx context.Context, server *Server, ta
}, nil
}
+ if payload.EarliestAllowedTs == 0 {
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusSuccess,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "Earliest allowed time unset",
+ },
+ }, nil
+ }
+
return []api.TaskCheckResult{
{
Status: api.TaskCheckStatusSuccess,
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 75c9adba8f..198138eaac 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -177,17 +177,13 @@ func (s *TaskCheckScheduler) Register(taskType string, executor TaskCheckExecuto
s.executors[taskType] = executor
}
-// shouldScheduleTimingTaskCheck will return whether should we schedule a timing task check for current task
-// the logic goes like the following:
-// 1. there is no running timing task check
-// 2(a). the earliestAllowedTs state has been altered since last check (either via new creation or patch).
-// 2(b). the earliestAllowedTs has NOT been altered since last check, however the last check had failed && earliestAllowedTs has passed now.
-// ONLY when {1 && (2.a || 2.b)} listed above or EXPLICITLY set the forceSchedule to TRUE would this function return true
+// 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.
func (s *TaskCheckScheduler) shouldScheduleTimingTaskCheck(ctx context.Context, task *api.Task, forceSchedule bool) (bool, error) {
- if forceSchedule {
- return true, nil
- }
-
statusList := []api.TaskCheckRunStatus{api.TaskCheckRunDone, api.TaskCheckRunFailed, api.TaskCheckRunRunning}
taskCheckType := api.TaskCheckGeneralEarliestAllowedTime
taskCheckRunFind := &api.TaskCheckRunFind{
@@ -201,14 +197,20 @@ 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 len(taskCheckRunList) == 0 {
- return true, nil
+ 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