From de5819675f3795a0649e4458ec9f6f89735bb2ab Mon Sep 17 00:00:00 2001 From: liuzx Date: Mon, 30 May 2022 18:24:15 +0800 Subject: [PATCH 1/5] fix-1941 --- models/cloudbrain.go | 5 +++++ routers/org/setting.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index e28ba3ea5..b9f0d0b86 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -1566,6 +1566,11 @@ func GetCloudbrainCountByUserID(userID int64, jobType string) (int, error) { return int(count), err } +func GetCloudbrainRunCountByUserID(userID int64) (int, error) { + count, err := x.In("status", JobWaiting, JobRunning).And("user_id = ?", userID).Count(new(Cloudbrain)) + return int(count), err +} + func GetBenchmarkCountByUserID(userID int64) (int, error) { count, err := x.In("status", JobWaiting, JobRunning).And("(job_type = ? or job_type = ? or job_type = ?) and user_id = ? and type = ?", string(JobTypeBenchmark), string(JobTypeBrainScore), string(JobTypeSnn4imagenet), userID, TypeCloudBrainOne).Count(new(Cloudbrain)) return int(count), err diff --git a/routers/org/setting.go b/routers/org/setting.go index 348d8cc8d..3213bf03d 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -73,6 +73,18 @@ func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) { ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name log.Trace("Organization name changed: %s -> %s", org.Name, form.Name) } + + count, err := models.GetCloudbrainRunCountByUserID(ctx.User.ID) + if err != nil { + ctx.ServerError("GetCloudbrainCountByUserID failed", err) + return + } else { + if count >= 1 { + log.Error("the user already has running or waiting task", ctx.Data["MsgID"]) + ctx.RenderWithErr(ctx.Tr("The cloudbrain tasks needs to be stopped before you delete the project"), tplSettingsOptions, &form) + return + } + } // In case it's just a case change. org.Name = form.Name org.LowerName = strings.ToLower(form.Name) From 406dabb36bed1ed622f45e271a273414558764f6 Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 31 May 2022 11:22:00 +0800 Subject: [PATCH 2/5] fix-1941 --- models/cloudbrain.go | 4 ++-- options/locale/locale_en-US.ini | 1 + options/locale/locale_zh-CN.ini | 1 + routers/org/setting.go | 12 ------------ routers/repo/setting.go | 28 +++++++++++++++++++--------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index b9f0d0b86..c1f346c89 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -1566,8 +1566,8 @@ func GetCloudbrainCountByUserID(userID int64, jobType string) (int, error) { return int(count), err } -func GetCloudbrainRunCountByUserID(userID int64) (int, error) { - count, err := x.In("status", JobWaiting, JobRunning).And("user_id = ?", userID).Count(new(Cloudbrain)) +func GetCloudbrainRunCountByRepoID(repoID int64) (int, error) { + count, err := x.In("status", JobWaiting, JobRunning).And("repo_id = ?", repoID).Count(new(Cloudbrain)) return int(count), err } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 265e7ed36..ff493c26a 100755 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -814,6 +814,7 @@ settings.delete_notices_1= - This operation CANNOT be undone. settings.delete_notices_2= - This operation will permanently delete the %s dataset. settings.delete_notices_fork_1= - Forks of this dataset will become independent after deletion. settings.deletion_success= The dataset has been deleted. +settings.deletion_notice_cloudbrain = you need to stop the cloudbrain task under the project before remove the project! task.machine_translation= machine translation task.question_answering_system= question answering system task.information_retrieval= information retrieval diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 1b26a930c..4b6305b55 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -1928,6 +1928,7 @@ settings.delete_notices_1=- 此操作 不可以 被回滚。 settings.delete_notices_2=- 此操作将永久删除项目 %s,包括 Git 数据、 任务、评论、百科和协作者的操作权限。 settings.delete_notices_fork_1=- 在此项目删除后,它的派生项目将变成独立项目。 settings.deletion_success=项目已被删除。 +settings.deletion_notice_cloudbrain=删除项目前需要先停止该项目下的云脑任务! settings.update_settings_success=项目设置已更新。 settings.transfer_owner=新拥有者 settings.make_transfer=开始转移 diff --git a/routers/org/setting.go b/routers/org/setting.go index 3213bf03d..348d8cc8d 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -73,18 +73,6 @@ func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) { ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name log.Trace("Organization name changed: %s -> %s", org.Name, form.Name) } - - count, err := models.GetCloudbrainRunCountByUserID(ctx.User.ID) - if err != nil { - ctx.ServerError("GetCloudbrainCountByUserID failed", err) - return - } else { - if count >= 1 { - log.Error("the user already has running or waiting task", ctx.Data["MsgID"]) - ctx.RenderWithErr(ctx.Tr("The cloudbrain tasks needs to be stopped before you delete the project"), tplSettingsOptions, &form) - return - } - } // In case it's just a case change. org.Name = form.Name org.LowerName = strings.ToLower(form.Name) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index af28f3290..a72af3858 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -6,7 +6,6 @@ package repo import ( - "code.gitea.io/gitea/modules/notification" "errors" "fmt" "io/ioutil" @@ -15,6 +14,8 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" @@ -477,16 +478,25 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil) return } - - if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { - ctx.ServerError("DeleteRepository", err) + count, err := models.GetCloudbrainRunCountByRepoID(repo.ID) + if err != nil { + ctx.ServerError("GetCloudbrainCountByRepoID failed", err) return - } - log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name) - go StopJobsByRepoID(repo.ID) + } else { + if count >= 1 { + ctx.RenderWithErr(ctx.Tr("repo.settings.deletion_notice_cloudbrain"), tplSettingsOptions, nil) + return + } + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { + ctx.ServerError("DeleteRepository", err) + return + } + log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name) + go StopJobsByRepoID(repo.ID) - ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) - ctx.Redirect(ctx.Repo.Owner.DashboardLink()) + ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) + ctx.Redirect(ctx.Repo.Owner.DashboardLink()) + } case "delete-wiki": if !ctx.Repo.IsOwner() { From a624735f82ba68b31e25f054e8f673c670f2825f Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Tue, 31 May 2022 12:00:57 +0800 Subject: [PATCH 3/5] update redis lock --- modules/auth/wechat/access_token.go | 10 +++++----- modules/redis/redis_lock/lock.go | 17 +++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/modules/auth/wechat/access_token.go b/modules/auth/wechat/access_token.go index 0a63bc2de..f9516e3e1 100644 --- a/modules/auth/wechat/access_token.go +++ b/modules/auth/wechat/access_token.go @@ -9,7 +9,7 @@ import ( const EMPTY_REDIS_VAL = "Nil" -var accessTokenLock = redis_lock.NewDistributeLock() +var accessTokenLock = redis_lock.NewDistributeLock(redis_key.AccessTokenLockKey()) func GetWechatAccessToken() string { token, _ := redis_client.Get(redis_key.WechatAccessTokenKey()) @@ -28,15 +28,15 @@ func GetWechatAccessToken() string { } func refreshAccessToken() { - if ok := accessTokenLock.Lock(redis_key.AccessTokenLockKey(), 3*time.Second); ok { - defer accessTokenLock.UnLock(redis_key.AccessTokenLockKey()) + if ok := accessTokenLock.Lock(3 * time.Second); ok { + defer accessTokenLock.UnLock() callAccessTokenAndUpdateCache() } } func refreshAndGetAccessToken() string { - if ok := accessTokenLock.LockWithWait(redis_key.AccessTokenLockKey(), 3*time.Second, 3*time.Second); ok { - defer accessTokenLock.UnLock(redis_key.AccessTokenLockKey()) + if ok := accessTokenLock.LockWithWait(3*time.Second, 3*time.Second); ok { + defer accessTokenLock.UnLock() token, _ := redis_client.Get(redis_key.WechatAccessTokenKey()) if token != "" { if token == EMPTY_REDIS_VAL { diff --git a/modules/redis/redis_lock/lock.go b/modules/redis/redis_lock/lock.go index 0faed3237..b8cd837f1 100644 --- a/modules/redis/redis_lock/lock.go +++ b/modules/redis/redis_lock/lock.go @@ -6,22 +6,23 @@ import ( ) type DistributeLock struct { + lockKey string } -func NewDistributeLock() *DistributeLock { - return &DistributeLock{} +func NewDistributeLock(lockKey string) *DistributeLock { + return &DistributeLock{lockKey: lockKey} } -func (lock *DistributeLock) Lock(lockKey string, expireTime time.Duration) bool { - isOk, _ := redis_client.Setnx(lockKey, "", expireTime) +func (lock *DistributeLock) Lock(expireTime time.Duration) bool { + isOk, _ := redis_client.Setnx(lock.lockKey, "", expireTime) return isOk } -func (lock *DistributeLock) LockWithWait(lockKey string, expireTime time.Duration, waitTime time.Duration) bool { +func (lock *DistributeLock) LockWithWait(expireTime time.Duration, waitTime time.Duration) bool { start := time.Now().Unix() * 1000 duration := waitTime.Milliseconds() for { - isOk, _ := redis_client.Setnx(lockKey, "", expireTime) + isOk, _ := redis_client.Setnx(lock.lockKey, "", expireTime) if isOk { return true } @@ -34,7 +35,7 @@ func (lock *DistributeLock) LockWithWait(lockKey string, expireTime time.Duratio return false } -func (lock *DistributeLock) UnLock(lockKey string) error { - _, err := redis_client.Del(lockKey) +func (lock *DistributeLock) UnLock() error { + _, err := redis_client.Del(lock.lockKey) return err } From 3df7cddfbf36e5d6d897d1510349190ccb95d8e4 Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 31 May 2022 14:46:07 +0800 Subject: [PATCH 4/5] fix-1941 --- models/cloudbrain.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index c1f346c89..810e68d30 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -1567,7 +1567,10 @@ func GetCloudbrainCountByUserID(userID int64, jobType string) (int, error) { } func GetCloudbrainRunCountByRepoID(repoID int64) (int, error) { - count, err := x.In("status", JobWaiting, JobRunning).And("repo_id = ?", repoID).Count(new(Cloudbrain)) + count, err := x.In("status", JobWaiting, JobRunning, ModelArtsCreateQueue, ModelArtsCreating, ModelArtsStarting, + ModelArtsReadyToStart, ModelArtsResizing, ModelArtsStartQueuing, ModelArtsRunning, ModelArtsRestarting, ModelArtsTrainJobInit, + ModelArtsTrainJobImageCreating, ModelArtsTrainJobSubmitTrying, ModelArtsTrainJobWaiting, ModelArtsTrainJobRunning, + ModelArtsTrainJobScaling, ModelArtsTrainJobCheckInit, ModelArtsTrainJobCheckRunning, ModelArtsTrainJobCheckRunningCompleted).And("repo_id = ?", repoID).Count(new(Cloudbrain)) return int(count), err } From 5e9b76979cf4a816584738bce2be6273e00cad04 Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 31 May 2022 16:21:27 +0800 Subject: [PATCH 5/5] fix-1941 --- options/locale/locale_zh-CN.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 4b6305b55..6640cb372 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -1928,7 +1928,7 @@ settings.delete_notices_1=- 此操作 不可以 被回滚。 settings.delete_notices_2=- 此操作将永久删除项目 %s,包括 Git 数据、 任务、评论、百科和协作者的操作权限。 settings.delete_notices_fork_1=- 在此项目删除后,它的派生项目将变成独立项目。 settings.deletion_success=项目已被删除。 -settings.deletion_notice_cloudbrain=删除项目前需要先停止该项目下的云脑任务! +settings.deletion_notice_cloudbrain=请先停止项目内正在运行的云脑任务,然后再删除项目。 settings.update_settings_success=项目设置已更新。 settings.transfer_owner=新拥有者 settings.make_transfer=开始转移