github.com/matrixorigin/matrixone@v1.2.0/pkg/predefine/predefine.go (about) 1 // Copyright 2024 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package predefine 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/catalog" 23 "github.com/matrixorigin/matrixone/pkg/pb/task" 24 "github.com/matrixorigin/matrixone/pkg/util/export" 25 "github.com/matrixorigin/matrixone/pkg/util/metric/mometric" 26 "github.com/robfig/cron/v3" 27 ) 28 29 // genInitCronTaskSQL Generate `insert` statement for creating system cron tasks, which works on the `mo_task`.`sys_cron_task` table. 30 func GenInitCronTaskSQL() (string, error) { 31 cronParser := cron.NewParser( 32 cron.Second | 33 cron.Minute | 34 cron.Hour | 35 cron.Dom | 36 cron.Month | 37 cron.Dow | 38 cron.Descriptor) 39 40 createCronTask := func(value task.TaskMetadata, cronExpr string) (*task.CronTask, error) { 41 sche, err := cronParser.Parse(cronExpr) 42 if err != nil { 43 return nil, err 44 } 45 46 now := time.Now().UnixMilli() 47 next := sche.Next(time.UnixMilli(now)) 48 49 return &task.CronTask{ 50 Metadata: value, 51 CronExpr: cronExpr, 52 NextTime: next.UnixMilli(), 53 TriggerTimes: 0, 54 CreateAt: now, 55 UpdateAt: now, 56 }, nil 57 } 58 59 cronTasks := make([]*task.CronTask, 0, 2) 60 task1, err := createCronTask(export.MergeTaskMetadata(task.TaskCode_MetricLogMerge), export.MergeTaskCronExprEvery05Min) 61 if err != nil { 62 return "", err 63 } 64 cronTasks = append(cronTasks, task1) 65 66 task2, err := createCronTask(mometric.TaskMetadata(mometric.StorageUsageCronTask, task.TaskCode_MetricStorageUsage), mometric.StorageUsageTaskCronExpr) 67 if err != nil { 68 return "", err 69 } 70 cronTasks = append(cronTasks, task2) 71 72 sql := fmt.Sprintf(`insert into %s.sys_cron_task ( 73 task_metadata_id, 74 task_metadata_executor, 75 task_metadata_context, 76 task_metadata_option, 77 cron_expr, 78 next_time, 79 trigger_times, 80 create_at, 81 update_at 82 ) values `, catalog.MOTaskDB) 83 84 for i, t := range cronTasks { 85 j, err := json.Marshal(t.Metadata.Options) 86 if err != nil { 87 return "", err 88 } 89 if i == 0 { 90 sql += fmt.Sprintf("('%s' ,%d ,'%s' ,'%s' ,'%s' ,%d ,%d ,%d ,%d)", 91 t.Metadata.ID, 92 t.Metadata.Executor, 93 t.Metadata.Context, 94 string(j), 95 t.CronExpr, 96 t.NextTime, 97 t.TriggerTimes, 98 t.CreateAt, 99 t.UpdateAt) 100 } else { 101 sql += fmt.Sprintf(",('%s' ,%d ,'%s' ,'%s' ,'%s' ,%d ,%d ,%d ,%d)", 102 t.Metadata.ID, 103 t.Metadata.Executor, 104 t.Metadata.Context, 105 string(j), 106 t.CronExpr, 107 t.NextTime, 108 t.TriggerTimes, 109 t.CreateAt, 110 t.UpdateAt) 111 } 112 } 113 return sql, nil 114 }