| @@ -2050,9 +2050,9 @@ func GetCloudBrainUnStoppedJob() ([]*Cloudbrain, error) { | |||||
| Find(&cloudbrains) | Find(&cloudbrains) | ||||
| } | } | ||||
| func GetCloudBrainOneStoppedJobDaysAgo(days int64, limit int) ([]*Cloudbrain, error) { | |||||
| func GetCloudBrainOneStoppedJobDaysAgo(days int, limit int) ([]*Cloudbrain, error) { | |||||
| cloudbrains := make([]*Cloudbrain, 0, 10) | cloudbrains := make([]*Cloudbrain, 0, 10) | ||||
| endTimeBefore := time.Now().Unix() - days*24*3600 | |||||
| endTimeBefore := time.Now().Unix() - int64(days)*24*3600 | |||||
| missEndTimeBefore := endTimeBefore - 24*3600 | missEndTimeBefore := endTimeBefore - 24*3600 | ||||
| return cloudbrains, x.Cols("id,job_name,job_id"). | return cloudbrains, x.Cols("id,job_name,job_id"). | ||||
| In("status", | In("status", | ||||
| @@ -5,6 +5,7 @@ | |||||
| package cron | package cron | ||||
| import ( | import ( | ||||
| "code.gitea.io/gitea/modules/setting" | |||||
| "context" | "context" | ||||
| "time" | "time" | ||||
| @@ -196,7 +197,7 @@ func registerHandleClearCloudbrainResult() { | |||||
| RegisterTaskFatal("handle_cloudbrain_one_result_clear", &BaseConfig{ | RegisterTaskFatal("handle_cloudbrain_one_result_clear", &BaseConfig{ | ||||
| Enabled: true, | Enabled: true, | ||||
| RunAtStart: false, | RunAtStart: false, | ||||
| Schedule: "* 0,30 2-8 * * ? *", | |||||
| Schedule: setting.ClearStrategy.Cron, | |||||
| }, func(ctx context.Context, _ *models.User, _ Config) error { | }, func(ctx context.Context, _ *models.User, _ Config) error { | ||||
| cloudbrainService.ClearCloudbrainResultSpace() | cloudbrainService.ClearCloudbrainResultSpace() | ||||
| return nil | return nil | ||||
| @@ -319,6 +320,7 @@ func initBasicTasks() { | |||||
| registerHandleRepoAndUserStatistic() | registerHandleRepoAndUserStatistic() | ||||
| registerHandleSummaryStatistic() | registerHandleSummaryStatistic() | ||||
| registerHandleClearCloudbrainResult() | |||||
| registerSyncCloudbrainStatus() | registerSyncCloudbrainStatus() | ||||
| registerHandleOrgStatistic() | registerHandleOrgStatistic() | ||||
| @@ -518,8 +518,7 @@ var ( | |||||
| MaxDatasetNum int | MaxDatasetNum int | ||||
| CullIdleTimeout string | CullIdleTimeout string | ||||
| CullInterval string | CullInterval string | ||||
| ResultSaveDays int64 | |||||
| ResultBatchSize int | |||||
| //benchmark config | //benchmark config | ||||
| IsBenchmarkEnabled bool | IsBenchmarkEnabled bool | ||||
| @@ -615,6 +614,15 @@ var ( | |||||
| UsageRateBeginTime string | UsageRateBeginTime string | ||||
| }{} | }{} | ||||
| ClearStrategy= struct { | |||||
| Enabled bool | |||||
| ResultSaveDays int | |||||
| ResultBatchSize int | |||||
| BatchSize int | |||||
| TrashSaveDays int | |||||
| Cron string | |||||
| }{} | |||||
| C2NetInfos *C2NetSqInfos | C2NetInfos *C2NetSqInfos | ||||
| CenterInfos *AiCenterInfos | CenterInfos *AiCenterInfos | ||||
| C2NetMapInfo map[string]*C2NetSequenceInfo | C2NetMapInfo map[string]*C2NetSequenceInfo | ||||
| @@ -1482,9 +1490,6 @@ func NewContext() { | |||||
| CullIdleTimeout = sec.Key("CULL_IDLE_TIMEOUT").MustString("900") | CullIdleTimeout = sec.Key("CULL_IDLE_TIMEOUT").MustString("900") | ||||
| CullInterval = sec.Key("CULL_INTERVAL").MustString("60") | CullInterval = sec.Key("CULL_INTERVAL").MustString("60") | ||||
| ResultSaveDays = sec.Key("RESULT_SAVE_DAYS").MustInt64(30) | |||||
| ResultBatchSize = sec.Key("BATCH_SIZE").MustInt(500) | |||||
| sec = Cfg.Section("benchmark") | sec = Cfg.Section("benchmark") | ||||
| IsBenchmarkEnabled = sec.Key("ENABLED").MustBool(false) | IsBenchmarkEnabled = sec.Key("ENABLED").MustBool(false) | ||||
| BenchmarkOwner = sec.Key("OWNER").MustString("") | BenchmarkOwner = sec.Key("OWNER").MustString("") | ||||
| @@ -1684,6 +1689,16 @@ func getModelartsCDConfig() { | |||||
| getNotebookFlavorInfos() | getNotebookFlavorInfos() | ||||
| } | } | ||||
| func getClearStrategy(){ | |||||
| sec := Cfg.Section("clear_strategy") | |||||
| ClearStrategy.Enabled=sec.Key("ENABLED").MustBool(false) | |||||
| ClearStrategy.ResultSaveDays=sec.Key("RESULT_SAVE_DAYS").MustInt(30) | |||||
| ClearStrategy.BatchSize=sec.Key("BATCH_SIZE").MustInt(500) | |||||
| ClearStrategy.TrashSaveDays=sec.Key("TRASH_SAVE_DAYS").MustInt(90) | |||||
| ClearStrategy.Cron=sec.Key("CRON").MustString("* 0,30 2-8 * * ? *") | |||||
| } | |||||
| func getGrampusConfig() { | func getGrampusConfig() { | ||||
| sec := Cfg.Section("grampus") | sec := Cfg.Section("grampus") | ||||
| @@ -14,7 +14,7 @@ import ( | |||||
| func ClearCloudbrainResultSpace() { | func ClearCloudbrainResultSpace() { | ||||
| tasks, err := models.GetCloudBrainOneStoppedJobDaysAgo(setting.ResultSaveDays, setting.ResultBatchSize) | |||||
| tasks, err := models.GetCloudBrainOneStoppedJobDaysAgo(setting.ClearStrategy.ResultSaveDays, setting.ClearStrategy.ResultBatchSize) | |||||
| if err != nil { | if err != nil { | ||||
| log.Warn("Failed to get cloudbrain, clear result failed.", err) | log.Warn("Failed to get cloudbrain, clear result failed.", err) | ||||
| @@ -32,17 +32,20 @@ func ClearCloudbrainResultSpace() { | |||||
| if err != nil { | if err != nil { | ||||
| log.Warn("Failed to set cloudbrain cleared status", err) | log.Warn("Failed to set cloudbrain cleared status", err) | ||||
| } | } | ||||
| if len(tasks) < setting.ResultBatchSize { //云脑表处理完了,处理历史垃圾数据,如果存在 | |||||
| if len(tasks) < setting.ClearStrategy.ResultBatchSize { //如果云脑表处理完了,通过遍历minio对象处理历史垃圾数据,如果存在 | |||||
| files, err := ioutil.ReadDir(setting.JobPath) | files, err := ioutil.ReadDir(setting.JobPath) | ||||
| processCount:=0 | |||||
| if err != nil { | if err != nil { | ||||
| log.Warn("Can not browser local job path.") | log.Warn("Can not browser local job path.") | ||||
| } else { | } else { | ||||
| SortModTimeAscend(files) | SortModTimeAscend(files) | ||||
| for _, file := range files { | for _, file := range files { | ||||
| //清理4个月前的任务 | |||||
| if file.ModTime().Before(time.Now().AddDate(0, -4, 0)) { | |||||
| //os.RemoveAll(setting.JobPath + file.Name()) | |||||
| } else { | |||||
| //清理n天前的历史垃圾数据,清理job目录 | |||||
| if file.ModTime().Before(time.Now().AddDate(0, 0, -setting.ClearStrategy.TrashSaveDays)) { | |||||
| os.RemoveAll(setting.JobPath + file.Name()) | |||||
| processCount++ | |||||
| } | |||||
| if processCount==setting.ClearStrategy.ResultBatchSize{ | |||||
| break | break | ||||
| } | } | ||||
| } | } | ||||
| @@ -50,20 +53,22 @@ func ClearCloudbrainResultSpace() { | |||||
| } | } | ||||
| minioFiles, err := storage.GetAllObjectByBucketAndPrefixMinio(setting.Attachment.Minio.Bucket, setting.CBCodePathPrefix) | minioFiles, err := storage.GetAllObjectByBucketAndPrefixMinio(setting.Attachment.Minio.Bucket, setting.CBCodePathPrefix) | ||||
| processCount=0 | |||||
| if err != nil { | if err != nil { | ||||
| log.Warn("Can not browser minio job path.") | log.Warn("Can not browser minio job path.") | ||||
| } else { | } else { | ||||
| SortModTimeAscendForMinio(minioFiles) | SortModTimeAscendForMinio(minioFiles) | ||||
| for _, file := range minioFiles { | for _, file := range minioFiles { | ||||
| //清理4个月前的任务 | |||||
| timeConvert, err := time.Parse("2006-01-02 15:04:05", file.ModTime) | timeConvert, err := time.Parse("2006-01-02 15:04:05", file.ModTime) | ||||
| if err == nil && timeConvert.Before(time.Now().AddDate(0, -4, 0)) { | |||||
| if err == nil && timeConvert.Before(time.Now().AddDate(0, 0, -setting.ClearStrategy.TrashSaveDays)) { | |||||
| dirPath := setting.CBCodePathPrefix + file.FileName + "/" | dirPath := setting.CBCodePathPrefix + file.FileName + "/" | ||||
| log.Info(dirPath) | log.Info(dirPath) | ||||
| //storage.Attachments.DeleteDir(dirPath) | |||||
| } else { | |||||
| break | |||||
| storage.Attachments.DeleteDir(dirPath) | |||||
| processCount++ | |||||
| if processCount==setting.ClearStrategy.ResultBatchSize{ | |||||
| break | |||||
| } | |||||
| } | } | ||||
| } | } | ||||