Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/494 Reviewed-by: zouap <zouap@pcl.ac.cn>pull/499/head
@@ -620,6 +620,18 @@ func GetCloudbrainByJobID(jobID string) (*Cloudbrain, error) { | |||||
return getRepoCloudBrain(cb) | return getRepoCloudBrain(cb) | ||||
} | } | ||||
func GetCloudbrainsNeededStopByUserID(userID int64) ([]*Cloudbrain, error) { | |||||
cloudBrains := make([]*Cloudbrain, 0) | |||||
err := x.Cols("job_id", "status", "type").Where("user_id=? AND (status =? OR status=?)", userID, string(JobRunning), string(JobWaiting)).Find(&cloudBrains) | |||||
return cloudBrains, err | |||||
} | |||||
func GetCloudbrainsNeededStopByRepoID(repoID int64) ([]*Cloudbrain, error) { | |||||
cloudBrains := make([]*Cloudbrain, 0) | |||||
err := x.Cols("job_id", "status", "type").Where("repo_id=? AND (status =? OR status=?)", repoID, string(JobRunning), string(JobWaiting)).Find(&cloudBrains) | |||||
return cloudBrains, err | |||||
} | |||||
func SetCloudbrainStatusByJobID(jobID string, status CloudbrainStatus) (err error) { | func SetCloudbrainStatusByJobID(jobID string, status CloudbrainStatus) (err error) { | ||||
cb := &Cloudbrain{JobID: jobID, Status: string(status)} | cb := &Cloudbrain{JobID: jobID, Status: string(status)} | ||||
_, err = x.Cols("status").Where("cloudbrain.job_id=?", jobID).Update(cb) | _, err = x.Cols("status").Where("cloudbrain.job_id=?", jobID).Update(cb) | ||||
@@ -13,6 +13,8 @@ import ( | |||||
"strings" | "strings" | ||||
"time" | "time" | ||||
"code.gitea.io/gitea/modules/modelarts" | |||||
"code.gitea.io/gitea/modules/git" | "code.gitea.io/gitea/modules/git" | ||||
"code.gitea.io/gitea/modules/storage" | "code.gitea.io/gitea/modules/storage" | ||||
@@ -361,6 +363,58 @@ func CloudBrainStop(ctx *context.Context) { | |||||
ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") | ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") | ||||
} | } | ||||
func StopJobsByUserID(userID int64) { | |||||
cloudBrains, err := models.GetCloudbrainsNeededStopByUserID(userID) | |||||
if err != nil { | |||||
log.Warn("Failed to get cloudBrain info", err) | |||||
return | |||||
} | |||||
StopJobs(cloudBrains) | |||||
} | |||||
func StopJobsByRepoID(repoID int64) { | |||||
cloudBrains, err := models.GetCloudbrainsNeededStopByRepoID(repoID) | |||||
if err != nil { | |||||
log.Warn("Failed to get cloudBrain info", err) | |||||
return | |||||
} | |||||
StopJobs(cloudBrains) | |||||
} | |||||
/** | |||||
*/ | |||||
func StopJobs(cloudBrains []*models.Cloudbrain) { | |||||
for _, taskInfo := range cloudBrains { | |||||
if taskInfo.Type == models.TypeCloudBrainOne { | |||||
err := cloudbrain.StopJob(taskInfo.JobID) | |||||
logErrorAndUpdateJobStatus(err, taskInfo) | |||||
} else { | |||||
param := models.NotebookAction{ | |||||
Action: models.ActionStop, | |||||
} | |||||
_, err := modelarts.StopJob(taskInfo.JobID, param) | |||||
logErrorAndUpdateJobStatus(err, taskInfo) | |||||
} | |||||
} | |||||
} | |||||
func logErrorAndUpdateJobStatus(err error, taskInfo *models.Cloudbrain) { | |||||
if err != nil { | |||||
log.Warn("Failed to stop cloudBrain job:"+taskInfo.JobID, err) | |||||
} else { | |||||
taskInfo.Status = string(models.JobStopped) | |||||
err = models.UpdateJob(taskInfo) | |||||
if err != nil { | |||||
log.Warn("UpdateJob failed", err) | |||||
} | |||||
} | |||||
} | |||||
func CloudBrainDel(ctx *context.Context) { | func CloudBrainDel(ctx *context.Context) { | ||||
var jobID = ctx.Params(":jobid") | var jobID = ctx.Params(":jobid") | ||||
task, err := models.GetCloudbrainByJobID(jobID) | task, err := models.GetCloudbrainByJobID(jobID) | ||||
@@ -440,6 +440,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { | |||||
return | return | ||||
} | } | ||||
log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name) | 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.Flash.Success(ctx.Tr("repo.settings.deletion_success")) | ||||
ctx.Redirect(ctx.Repo.Owner.DashboardLink()) | ctx.Redirect(ctx.Repo.Owner.DashboardLink()) | ||||
@@ -11,6 +11,8 @@ import ( | |||||
"net/http" | "net/http" | ||||
"strings" | "strings" | ||||
"code.gitea.io/gitea/routers/repo" | |||||
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
"code.gitea.io/gitea/modules/auth/oauth2" | "code.gitea.io/gitea/modules/auth/oauth2" | ||||
@@ -1056,6 +1058,7 @@ func SignOut(ctx *context.Context) { | |||||
}) | }) | ||||
} | } | ||||
HandleSignOut(ctx) | HandleSignOut(ctx) | ||||
go repo.StopJobsByUserID(ctx.User.ID) | |||||
ctx.Redirect(setting.AppSubURL + "/") | ctx.Redirect(setting.AppSubURL + "/") | ||||
} | } | ||||