You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudbrain.go 18 kB

3 years ago
5 years ago
5 years ago
5 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os"
  6. "strconv"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "code.gitea.io/gitea/modules/storage"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. //Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"`
  17. //CommandBenchmark = `echo "start benchmark";python /code/test.py;echo "end benchmark"`
  18. CommandBenchmark = `echo "start benchmark";cd /benchmark && bash run_bk.sh;echo "end benchmark"`
  19. CodeMountPath = "/code"
  20. DataSetMountPath = "/dataset"
  21. ModelMountPath = "/model"
  22. LogFile = "log.txt"
  23. BenchMarkMountPath = "/benchmark"
  24. BenchMarkResourceID = 1
  25. Snn4imagenetMountPath = "/snn4imagenet"
  26. BrainScoreMountPath = "/brainscore"
  27. TaskInfoName = "/taskInfo"
  28. Snn4imagenetCommand = `/opt/conda/bin/python /snn4imagenet/testSNN_script.py --modelname '%s' --modelpath '/dataset' --modeldescription '%s'`
  29. BrainScoreCommand = `bash /brainscore/brainscore_test_par4shSrcipt.sh -b '%s' -n '%s' -p '/dataset' -d '%s'`
  30. SubTaskName = "task1"
  31. Success = "S000"
  32. DefaultBranchName = "master"
  33. ResultPath = "/result"
  34. )
  35. var (
  36. ResourceSpecs *models.ResourceSpecs
  37. TrainResourceSpecs *models.ResourceSpecs
  38. InferenceResourceSpecs *models.ResourceSpecs
  39. SpecialPools *models.SpecialPools
  40. )
  41. type GenerateCloudBrainTaskReq struct {
  42. Ctx *context.Context
  43. DisplayJobName string
  44. JobName string
  45. Image string
  46. Command string
  47. CodePath string
  48. ModelPath string
  49. BenchmarkPath string
  50. Snn4ImageNetPath string
  51. BrainScorePath string
  52. JobType string
  53. GpuQueue string
  54. Description string
  55. BranchName string
  56. BootFile string
  57. Params string
  58. CommitID string
  59. Uuids string
  60. DatasetNames string
  61. DatasetInfos map[string]models.DatasetInfo
  62. BenchmarkTypeID int
  63. BenchmarkChildTypeID int
  64. ResourceSpecId int
  65. ResultPath string
  66. TrainUrl string
  67. ModelName string
  68. ModelVersion string
  69. CkptName string
  70. LabelName string
  71. Spec *models.Specification
  72. }
  73. func GetCloudbrainDebugCommand() string {
  74. var command = `pip3 install jupyterlab==3 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --ServerApp.shutdown_no_activity_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_inactive_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_idle_timeout=` + setting.CullIdleTimeout + ` --MappingKernelManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_connected=True --MappingKernelManager.cull_busy=True --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --ServerApp.token="" --ServerApp.allow_origin="self https://cloudbrain.pcl.ac.cn" `
  75. return command
  76. }
  77. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  78. if !ctx.IsSigned {
  79. return false
  80. }
  81. if err != nil {
  82. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  83. } else {
  84. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  85. }
  86. }
  87. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  88. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  89. }
  90. func CanCreateOrDebugJob(ctx *context.Context) bool {
  91. if !ctx.IsSigned {
  92. return false
  93. }
  94. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  95. }
  96. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  97. return isAdminOrJobCreater(ctx, job, nil)
  98. }
  99. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  100. if !ctx.IsSigned {
  101. return false
  102. }
  103. if err != nil {
  104. return ctx.IsUserSiteAdmin()
  105. } else {
  106. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  107. }
  108. }
  109. func isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  110. if !ctx.IsSigned {
  111. return false
  112. }
  113. if err != nil {
  114. return ctx.IsUserSiteAdmin()
  115. } else {
  116. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  117. }
  118. }
  119. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  120. var ID = ctx.Params(":id")
  121. job, err := models.GetCloudbrainByID(ID)
  122. if err != nil {
  123. log.Error("GetCloudbrainByID failed:%v", err.Error())
  124. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  125. }
  126. ctx.Cloudbrain = job
  127. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  128. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  129. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  130. }
  131. }
  132. func AdminOrJobCreaterRight(ctx *context.Context) {
  133. var ID = ctx.Params(":id")
  134. job, err := models.GetCloudbrainByID(ID)
  135. if err != nil {
  136. log.Error("GetCloudbrainByID failed:%v", err.Error())
  137. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  138. }
  139. ctx.Cloudbrain = job
  140. if !isAdminOrJobCreater(ctx, job, err) {
  141. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  142. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  143. }
  144. }
  145. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  146. var jobID = ctx.Params(":jobid")
  147. job, err := models.GetCloudbrainByJobID(jobID)
  148. if err != nil {
  149. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  150. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  151. }
  152. ctx.Cloudbrain = job
  153. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  154. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  155. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  156. }
  157. }
  158. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  159. var jobID = ctx.Params(":jobid")
  160. job, err := models.GetCloudbrainByJobID(jobID)
  161. if err != nil {
  162. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  163. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  164. }
  165. ctx.Cloudbrain = job
  166. if !isAdminOrJobCreater(ctx, job, err) {
  167. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  168. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  169. }
  170. }
  171. func AdminOrImageCreaterRight(ctx *context.Context) {
  172. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  173. var image *models.Image
  174. if err != nil {
  175. log.Error("Get Image by ID failed:%v", err.Error())
  176. } else {
  177. image, err = models.GetImageByID(id)
  178. if err != nil {
  179. log.Error("Get Image by ID failed:%v", err.Error())
  180. return
  181. }
  182. }
  183. if !isAdminOrImageCreater(ctx, image, err) {
  184. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  185. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  186. }
  187. }
  188. func GenerateTask(req GenerateCloudBrainTaskReq) error {
  189. var versionCount int
  190. if req.JobType == string(models.JobTypeTrain) {
  191. versionCount = 1
  192. }
  193. volumes := []models.Volume{
  194. {
  195. HostPath: models.StHostPath{
  196. Path: req.CodePath,
  197. MountPath: CodeMountPath,
  198. ReadOnly: false,
  199. },
  200. },
  201. {
  202. HostPath: models.StHostPath{
  203. Path: req.ModelPath,
  204. MountPath: ModelMountPath,
  205. ReadOnly: false,
  206. },
  207. },
  208. {
  209. HostPath: models.StHostPath{
  210. Path: req.BenchmarkPath,
  211. MountPath: BenchMarkMountPath,
  212. ReadOnly: true,
  213. },
  214. },
  215. {
  216. HostPath: models.StHostPath{
  217. Path: req.Snn4ImageNetPath,
  218. MountPath: Snn4imagenetMountPath,
  219. ReadOnly: true,
  220. },
  221. },
  222. {
  223. HostPath: models.StHostPath{
  224. Path: req.BrainScorePath,
  225. MountPath: BrainScoreMountPath,
  226. ReadOnly: true,
  227. },
  228. },
  229. {
  230. HostPath: models.StHostPath{
  231. Path: req.ResultPath,
  232. MountPath: ResultPath,
  233. ReadOnly: false,
  234. },
  235. },
  236. }
  237. if len(req.DatasetInfos) == 1 {
  238. volumes = append(volumes, models.Volume{
  239. HostPath: models.StHostPath{
  240. Path: req.DatasetInfos[req.Uuids].DataLocalPath,
  241. MountPath: DataSetMountPath,
  242. ReadOnly: true,
  243. },
  244. })
  245. } else if len(req.DatasetInfos) > 1 {
  246. for _, dataset := range req.DatasetInfos {
  247. volumes = append(volumes, models.Volume{
  248. HostPath: models.StHostPath{
  249. Path: dataset.DataLocalPath,
  250. MountPath: DataSetMountPath + "/" + dataset.Name,
  251. ReadOnly: true,
  252. },
  253. })
  254. }
  255. }
  256. createTime := timeutil.TimeStampNow()
  257. jobResult, err := CreateJob(req.JobName, models.CreateJobParams{
  258. JobName: req.JobName,
  259. RetryCount: 1,
  260. GpuType: req.Spec.QueueCode,
  261. Image: req.Image,
  262. TaskRoles: []models.TaskRole{
  263. {
  264. Name: SubTaskName,
  265. TaskNumber: 1,
  266. MinSucceededTaskCount: 1,
  267. MinFailedTaskCount: 1,
  268. CPUNumber: req.Spec.CpuCores,
  269. GPUNumber: req.Spec.AccCardsNum,
  270. MemoryMB: int(req.Spec.MemGiB * 1024),
  271. ShmMB: int(req.Spec.ShareMemGiB * 1024),
  272. Command: req.Command,
  273. NeedIBDevice: false,
  274. IsMainRole: false,
  275. UseNNI: false,
  276. },
  277. },
  278. Volumes: volumes,
  279. })
  280. if err != nil {
  281. log.Error("CreateJob failed:", err.Error(), req.Ctx.Data["MsgID"])
  282. return err
  283. }
  284. if jobResult.Code != Success {
  285. log.Error("CreateJob(%s) failed:%s", req.JobName, jobResult.Msg, req.Ctx.Data["MsgID"])
  286. return errors.New(jobResult.Msg)
  287. }
  288. var jobID = jobResult.Payload["jobId"].(string)
  289. err = models.CreateCloudbrain(&models.Cloudbrain{
  290. Status: string(models.JobWaiting),
  291. UserID: req.Ctx.User.ID,
  292. RepoID: req.Ctx.Repo.Repository.ID,
  293. JobID: jobID,
  294. JobName: req.JobName,
  295. DisplayJobName: req.DisplayJobName,
  296. SubTaskName: SubTaskName,
  297. JobType: req.JobType,
  298. Type: models.TypeCloudBrainOne,
  299. Uuid: req.Uuids,
  300. Image: req.Image,
  301. GpuQueue: req.GpuQueue,
  302. ResourceSpecId: req.ResourceSpecId,
  303. ComputeResource: models.GPUResource,
  304. BenchmarkTypeID: req.BenchmarkTypeID,
  305. BenchmarkChildTypeID: req.BenchmarkChildTypeID,
  306. Description: req.Description,
  307. IsLatestVersion: "1",
  308. VersionCount: versionCount,
  309. BranchName: req.BranchName,
  310. BootFile: req.BootFile,
  311. DatasetName: req.DatasetNames,
  312. Parameters: req.Params,
  313. TrainUrl: req.TrainUrl,
  314. ModelName: req.ModelName,
  315. ModelVersion: req.ModelVersion,
  316. CkptName: req.CkptName,
  317. ResultUrl: req.ResultPath,
  318. LabelName: req.LabelName,
  319. CreatedUnix: createTime,
  320. UpdatedUnix: createTime,
  321. CommitID: req.CommitID,
  322. Spec: req.Spec,
  323. })
  324. if err != nil {
  325. return err
  326. }
  327. task, err := models.GetCloudbrainByJobID(jobID)
  328. if err != nil {
  329. log.Error("GetCloudbrainByJobID failed: %v", err.Error())
  330. return err
  331. }
  332. stringId := strconv.FormatInt(task.ID, 10)
  333. if IsBenchmarkJob(req.JobType) {
  334. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateBenchMarkTask)
  335. } else if string(models.JobTypeTrain) == req.JobType {
  336. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateGPUTrainTask)
  337. } else if string(models.JobTypeInference) == req.JobType {
  338. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  339. } else {
  340. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateDebugGPUTask)
  341. }
  342. return nil
  343. }
  344. func IsBenchmarkJob(jobType string) bool {
  345. return string(models.JobTypeBenchmark) == jobType || string(models.JobTypeBrainScore) == jobType || string(models.JobTypeSnn4imagenet) == jobType
  346. }
  347. func GetWaitingCloudbrainCount(cloudbrainType int, computeResource string, jobTypes ...models.JobType) int64 {
  348. num, err := models.GetWaitingCloudbrainCount(cloudbrainType, computeResource, jobTypes...)
  349. if err != nil {
  350. log.Warn("Get waiting count err", err)
  351. num = 0
  352. }
  353. return num
  354. }
  355. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  356. jobName := task.JobName
  357. spec := task.Spec
  358. var datasetInfos map[string]models.DatasetInfo
  359. if task.Uuid != "" {
  360. var err error
  361. datasetInfos, _, err = models.GetDatasetInfo(task.Uuid)
  362. if err != nil {
  363. log.Error("GetDatasetInfo failed:%v", err, ctx.Data["MsgID"])
  364. return err
  365. }
  366. }
  367. volumes := []models.Volume{
  368. {
  369. HostPath: models.StHostPath{
  370. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  371. MountPath: CodeMountPath,
  372. ReadOnly: false,
  373. },
  374. },
  375. {
  376. HostPath: models.StHostPath{
  377. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  378. MountPath: ModelMountPath,
  379. ReadOnly: false,
  380. },
  381. },
  382. {
  383. HostPath: models.StHostPath{
  384. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  385. MountPath: BenchMarkMountPath,
  386. ReadOnly: true,
  387. },
  388. },
  389. {
  390. HostPath: models.StHostPath{
  391. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  392. MountPath: Snn4imagenetMountPath,
  393. ReadOnly: true,
  394. },
  395. },
  396. {
  397. HostPath: models.StHostPath{
  398. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  399. MountPath: BrainScoreMountPath,
  400. ReadOnly: true,
  401. },
  402. },
  403. }
  404. if datasetInfos != nil {
  405. if len(datasetInfos) == 1 {
  406. volumes = append(volumes, models.Volume{
  407. HostPath: models.StHostPath{
  408. Path: datasetInfos[task.Uuid].DataLocalPath,
  409. MountPath: DataSetMountPath,
  410. ReadOnly: true,
  411. },
  412. })
  413. } else {
  414. for _, dataset := range datasetInfos {
  415. volumes = append(volumes, models.Volume{
  416. HostPath: models.StHostPath{
  417. Path: dataset.DataLocalPath,
  418. MountPath: DataSetMountPath + "/" + dataset.Name,
  419. ReadOnly: true,
  420. },
  421. })
  422. }
  423. }
  424. }
  425. createTime := timeutil.TimeStampNow()
  426. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  427. JobName: jobName,
  428. RetryCount: 1,
  429. GpuType: task.GpuQueue,
  430. Image: task.Image,
  431. TaskRoles: []models.TaskRole{
  432. {
  433. Name: SubTaskName,
  434. TaskNumber: 1,
  435. MinSucceededTaskCount: 1,
  436. MinFailedTaskCount: 1,
  437. CPUNumber: spec.CpuCores,
  438. GPUNumber: spec.AccCardsNum,
  439. MemoryMB: int(spec.MemGiB * 1024),
  440. ShmMB: int(spec.ShareMemGiB * 1024),
  441. Command: GetCloudbrainDebugCommand(), //Command,
  442. NeedIBDevice: false,
  443. IsMainRole: false,
  444. UseNNI: false,
  445. },
  446. },
  447. Volumes: volumes,
  448. })
  449. if err != nil {
  450. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  451. return err
  452. }
  453. if jobResult.Code != Success {
  454. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  455. return errors.New(jobResult.Msg)
  456. }
  457. var jobID = jobResult.Payload["jobId"].(string)
  458. newTask := &models.Cloudbrain{
  459. Status: string(models.JobWaiting),
  460. UserID: task.UserID,
  461. RepoID: task.RepoID,
  462. JobID: jobID,
  463. JobName: task.JobName,
  464. DisplayJobName: task.DisplayJobName,
  465. SubTaskName: task.SubTaskName,
  466. JobType: task.JobType,
  467. Type: task.Type,
  468. Uuid: task.Uuid,
  469. DatasetName: task.DatasetName,
  470. Image: task.Image,
  471. GpuQueue: task.GpuQueue,
  472. ResourceSpecId: task.ResourceSpecId,
  473. ComputeResource: task.ComputeResource,
  474. CreatedUnix: createTime,
  475. UpdatedUnix: createTime,
  476. BranchName: task.BranchName,
  477. Spec: spec,
  478. }
  479. err = models.RestartCloudbrain(task, newTask)
  480. if err != nil {
  481. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  482. return err
  483. }
  484. stringId := strconv.FormatInt(newTask.ID, 10)
  485. *newID = stringId
  486. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  487. return nil
  488. }
  489. func geMatchResourceSpec(jobType string, gpuQueue string, resourceSpecId int) *models.ResourceSpec {
  490. for _, specialPool := range SpecialPools.Pools {
  491. if specialPool.ResourceSpec != nil {
  492. if IsElementExist(specialPool.JobType, jobType) && IsQueueInSpecialtPool(specialPool.Pool, gpuQueue) {
  493. for _, spec := range specialPool.ResourceSpec {
  494. if resourceSpecId == spec.Id {
  495. return spec
  496. }
  497. }
  498. }
  499. }
  500. }
  501. return nil
  502. }
  503. func DelCloudBrainJob(jobId string) string {
  504. task, err := models.GetCloudbrainByJobID(jobId)
  505. if err != nil {
  506. log.Error("get cloud brain err:", err)
  507. return "cloudbrain.Delete_failed"
  508. }
  509. if task.Status != string(models.JobStopped) && task.Status != string(models.JobFailed) && task.Status != string(models.JobSucceeded) {
  510. log.Error("the job(%s) has not been stopped", task.JobName)
  511. return "cloudbrain.Not_Stopped"
  512. }
  513. err = models.DeleteJob(task)
  514. if err != nil {
  515. log.Error("DeleteJob failed:", err)
  516. return "cloudbrain.Delete_failed"
  517. }
  518. deleteJobStorage(task.JobName)
  519. return ""
  520. }
  521. func deleteJobStorage(jobName string) error {
  522. //delete local
  523. localJobPath := setting.JobPath + jobName
  524. err := os.RemoveAll(localJobPath)
  525. if err != nil {
  526. log.Error("RemoveAll(%s) failed:%v", localJobPath, err)
  527. }
  528. dirPath := setting.CBCodePathPrefix + jobName + "/"
  529. err = storage.Attachments.DeleteDir(dirPath)
  530. if err != nil {
  531. log.Error("DeleteDir(%s) failed:%v", localJobPath, err)
  532. }
  533. return nil
  534. }
  535. func InitSpecialPool() {
  536. if SpecialPools == nil && setting.SpecialPools != "" {
  537. json.Unmarshal([]byte(setting.SpecialPools), &SpecialPools)
  538. }
  539. }
  540. func IsResourceSpecInSpecialPool(resourceSpecs []*models.ResourceSpec, resourceSpecId int) bool {
  541. if resourceSpecs == nil || len(resourceSpecs) == 0 {
  542. return true
  543. }
  544. for _, v := range resourceSpecs {
  545. if v.Id == resourceSpecId {
  546. return true
  547. }
  548. }
  549. return false
  550. }
  551. func IsQueueInSpecialtPool(pool []*models.GpuInfo, queue string) bool {
  552. for _, v := range pool {
  553. if v.Queue == queue {
  554. return true
  555. }
  556. }
  557. return false
  558. }
  559. func IsElementExist(s []string, str string) bool {
  560. for _, v := range s {
  561. if v == str {
  562. return true
  563. }
  564. }
  565. return false
  566. }