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 19 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
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
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
5 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
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 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
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. }
  71. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  72. if !ctx.IsSigned {
  73. return false
  74. }
  75. if err != nil {
  76. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  77. } else {
  78. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  79. }
  80. }
  81. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  82. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  83. }
  84. func CanCreateOrDebugJob(ctx *context.Context) bool {
  85. if !ctx.IsSigned {
  86. return false
  87. }
  88. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  89. }
  90. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  91. return isAdminOrJobCreater(ctx, job, nil)
  92. }
  93. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  94. if !ctx.IsSigned {
  95. return false
  96. }
  97. if err != nil {
  98. return ctx.IsUserSiteAdmin()
  99. } else {
  100. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  101. }
  102. }
  103. func isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  104. if !ctx.IsSigned {
  105. return false
  106. }
  107. if err != nil {
  108. return ctx.IsUserSiteAdmin()
  109. } else {
  110. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  111. }
  112. }
  113. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  114. var ID = ctx.Params(":id")
  115. job, err := models.GetCloudbrainByID(ID)
  116. if err != nil {
  117. log.Error("GetCloudbrainByID failed:%v", err.Error())
  118. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  119. }
  120. ctx.Cloudbrain = job
  121. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  122. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  123. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  124. }
  125. }
  126. func AdminOrJobCreaterRight(ctx *context.Context) {
  127. var ID = ctx.Params(":id")
  128. job, err := models.GetCloudbrainByID(ID)
  129. if err != nil {
  130. log.Error("GetCloudbrainByID failed:%v", err.Error())
  131. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  132. }
  133. ctx.Cloudbrain = job
  134. if !isAdminOrJobCreater(ctx, job, err) {
  135. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  136. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  137. }
  138. }
  139. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  140. var jobID = ctx.Params(":jobid")
  141. job, err := models.GetCloudbrainByJobID(jobID)
  142. if err != nil {
  143. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  144. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  145. }
  146. ctx.Cloudbrain = job
  147. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  148. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  149. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  150. }
  151. }
  152. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  153. var jobID = ctx.Params(":jobid")
  154. job, err := models.GetCloudbrainByJobID(jobID)
  155. if err != nil {
  156. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  157. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  158. }
  159. ctx.Cloudbrain = job
  160. if !isAdminOrJobCreater(ctx, job, err) {
  161. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  162. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  163. }
  164. }
  165. func AdminOrImageCreaterRight(ctx *context.Context) {
  166. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  167. var image *models.Image
  168. if err != nil {
  169. log.Error("Get Image by ID failed:%v", err.Error())
  170. } else {
  171. image, err = models.GetImageByID(id)
  172. if err != nil {
  173. log.Error("Get Image by ID failed:%v", err.Error())
  174. return
  175. }
  176. }
  177. if !isAdminOrImageCreater(ctx, image, err) {
  178. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  179. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  180. }
  181. }
  182. func GenerateTask(req GenerateCloudBrainTaskReq) error {
  183. var resourceSpec *models.ResourceSpec
  184. var versionCount int
  185. if req.JobType == string(models.JobTypeTrain) {
  186. versionCount = 1
  187. if TrainResourceSpecs == nil {
  188. json.Unmarshal([]byte(setting.TrainResourceSpecs), &TrainResourceSpecs)
  189. }
  190. for _, spec := range TrainResourceSpecs.ResourceSpec {
  191. if req.ResourceSpecId == spec.Id {
  192. resourceSpec = spec
  193. break
  194. }
  195. }
  196. } else if req.JobType == string(models.JobTypeInference) {
  197. if InferenceResourceSpecs == nil {
  198. json.Unmarshal([]byte(setting.InferenceResourceSpecs), &InferenceResourceSpecs)
  199. }
  200. for _, spec := range InferenceResourceSpecs.ResourceSpec {
  201. if req.ResourceSpecId == spec.Id {
  202. resourceSpec = spec
  203. break
  204. }
  205. }
  206. } else {
  207. if ResourceSpecs == nil {
  208. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  209. }
  210. for _, spec := range ResourceSpecs.ResourceSpec {
  211. if req.ResourceSpecId == spec.Id {
  212. resourceSpec = spec
  213. break
  214. }
  215. }
  216. }
  217. //如果没有匹配到spec信息,尝试从专属资源池获取
  218. if resourceSpec == nil && SpecialPools != nil {
  219. for _, specialPool := range SpecialPools.Pools {
  220. if resourceSpec != nil {
  221. break
  222. }
  223. if specialPool.ResourceSpec != nil {
  224. if IsElementExist(specialPool.JobType, req.JobType) && IsQueueInSpecialtPool(specialPool.Pool, req.GpuQueue) {
  225. for _, spec := range specialPool.ResourceSpec {
  226. if req.ResourceSpecId == spec.Id {
  227. resourceSpec = spec
  228. break
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. if resourceSpec == nil {
  236. log.Error("no such resourceSpecId(%d)", req.ResourceSpecId, req.Ctx.Data["MsgID"])
  237. return errors.New("no such resourceSpec")
  238. }
  239. volumes := []models.Volume{
  240. {
  241. HostPath: models.StHostPath{
  242. Path: req.CodePath,
  243. MountPath: CodeMountPath,
  244. ReadOnly: false,
  245. },
  246. },
  247. {
  248. HostPath: models.StHostPath{
  249. Path: req.ModelPath,
  250. MountPath: ModelMountPath,
  251. ReadOnly: false,
  252. },
  253. },
  254. {
  255. HostPath: models.StHostPath{
  256. Path: req.BenchmarkPath,
  257. MountPath: BenchMarkMountPath,
  258. ReadOnly: true,
  259. },
  260. },
  261. {
  262. HostPath: models.StHostPath{
  263. Path: req.Snn4ImageNetPath,
  264. MountPath: Snn4imagenetMountPath,
  265. ReadOnly: true,
  266. },
  267. },
  268. {
  269. HostPath: models.StHostPath{
  270. Path: req.BrainScorePath,
  271. MountPath: BrainScoreMountPath,
  272. ReadOnly: true,
  273. },
  274. },
  275. {
  276. HostPath: models.StHostPath{
  277. Path: req.ResultPath,
  278. MountPath: ResultPath,
  279. ReadOnly: false,
  280. },
  281. },
  282. }
  283. if len(req.DatasetInfos) == 1 {
  284. volumes = append(volumes, models.Volume{
  285. HostPath: models.StHostPath{
  286. Path: req.DatasetInfos[req.Uuids].DataLocalPath,
  287. MountPath: DataSetMountPath,
  288. ReadOnly: true,
  289. },
  290. })
  291. } else {
  292. for _, dataset := range req.DatasetInfos {
  293. volumes = append(volumes, models.Volume{
  294. HostPath: models.StHostPath{
  295. Path: dataset.DataLocalPath,
  296. MountPath: DataSetMountPath + "/" + dataset.Name,
  297. ReadOnly: true,
  298. },
  299. })
  300. }
  301. }
  302. createTime := timeutil.TimeStampNow()
  303. jobResult, err := CreateJob(req.JobName, models.CreateJobParams{
  304. JobName: req.JobName,
  305. RetryCount: 1,
  306. GpuType: req.GpuQueue,
  307. Image: req.Image,
  308. TaskRoles: []models.TaskRole{
  309. {
  310. Name: SubTaskName,
  311. TaskNumber: 1,
  312. MinSucceededTaskCount: 1,
  313. MinFailedTaskCount: 1,
  314. CPUNumber: resourceSpec.CpuNum,
  315. GPUNumber: resourceSpec.GpuNum,
  316. MemoryMB: resourceSpec.MemMiB,
  317. ShmMB: resourceSpec.ShareMemMiB,
  318. Command: req.Command,
  319. NeedIBDevice: false,
  320. IsMainRole: false,
  321. UseNNI: false,
  322. },
  323. },
  324. Volumes: volumes,
  325. })
  326. if err != nil {
  327. log.Error("CreateJob failed:", err.Error(), req.Ctx.Data["MsgID"])
  328. return err
  329. }
  330. if jobResult.Code != Success {
  331. log.Error("CreateJob(%s) failed:%s", req.JobName, jobResult.Msg, req.Ctx.Data["MsgID"])
  332. return errors.New(jobResult.Msg)
  333. }
  334. var jobID = jobResult.Payload["jobId"].(string)
  335. err = models.CreateCloudbrain(&models.Cloudbrain{
  336. Status: string(models.JobWaiting),
  337. UserID: req.Ctx.User.ID,
  338. RepoID: req.Ctx.Repo.Repository.ID,
  339. JobID: jobID,
  340. JobName: req.JobName,
  341. DisplayJobName: req.DisplayJobName,
  342. SubTaskName: SubTaskName,
  343. JobType: req.JobType,
  344. Type: models.TypeCloudBrainOne,
  345. Uuid: req.Uuids,
  346. Image: req.Image,
  347. GpuQueue: req.GpuQueue,
  348. ResourceSpecId: req.ResourceSpecId,
  349. ComputeResource: models.GPUResource,
  350. BenchmarkTypeID: req.BenchmarkTypeID,
  351. BenchmarkChildTypeID: req.BenchmarkChildTypeID,
  352. Description: req.Description,
  353. IsLatestVersion: "1",
  354. VersionCount: versionCount,
  355. BranchName: req.BranchName,
  356. BootFile: req.BootFile,
  357. DatasetName: req.DatasetNames,
  358. Parameters: req.Params,
  359. TrainUrl: req.TrainUrl,
  360. ModelName: req.ModelName,
  361. ModelVersion: req.ModelVersion,
  362. CkptName: req.CkptName,
  363. ResultUrl: req.ResultPath,
  364. CreatedUnix: createTime,
  365. UpdatedUnix: createTime,
  366. CommitID: req.CommitID,
  367. })
  368. if err != nil {
  369. return err
  370. }
  371. task, err := models.GetCloudbrainByJobID(jobID)
  372. if err != nil {
  373. log.Error("GetCloudbrainByJobID failed: %v", err.Error())
  374. return err
  375. }
  376. stringId := strconv.FormatInt(task.ID, 10)
  377. if IsBenchmarkJob(req.JobType) {
  378. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateBenchMarkTask)
  379. } else if string(models.JobTypeTrain) == req.JobType {
  380. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateGPUTrainTask)
  381. } else if string(models.JobTypeInference) == req.JobType {
  382. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  383. } else {
  384. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateDebugGPUTask)
  385. }
  386. return nil
  387. }
  388. func IsBenchmarkJob(jobType string) bool {
  389. return string(models.JobTypeBenchmark) == jobType || string(models.JobTypeBrainScore) == jobType || string(models.JobTypeSnn4imagenet) == jobType
  390. }
  391. func GetWaitingCloudbrainCount(cloudbrainType int, computeResource string, jobTypes ...models.JobType) int64 {
  392. num, err := models.GetWaitingCloudbrainCount(cloudbrainType, computeResource, jobTypes...)
  393. if err != nil {
  394. log.Warn("Get waiting count err", err)
  395. num = 0
  396. }
  397. return num
  398. }
  399. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  400. jobName := task.JobName
  401. var resourceSpec *models.ResourceSpec
  402. if ResourceSpecs == nil {
  403. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  404. }
  405. for _, spec := range ResourceSpecs.ResourceSpec {
  406. if task.ResourceSpecId == spec.Id {
  407. resourceSpec = spec
  408. }
  409. }
  410. if resourceSpec == nil {
  411. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  412. return errors.New("no such resourceSpec")
  413. }
  414. datasetInfos, _, err := models.GetDatasetInfo(task.Uuid)
  415. if err != nil {
  416. log.Error("GetDatasetInfo failed:%v", err, ctx.Data["MsgID"])
  417. return err
  418. }
  419. volumes := []models.Volume{
  420. {
  421. HostPath: models.StHostPath{
  422. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  423. MountPath: CodeMountPath,
  424. ReadOnly: false,
  425. },
  426. },
  427. {
  428. HostPath: models.StHostPath{
  429. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  430. MountPath: ModelMountPath,
  431. ReadOnly: false,
  432. },
  433. },
  434. {
  435. HostPath: models.StHostPath{
  436. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  437. MountPath: BenchMarkMountPath,
  438. ReadOnly: true,
  439. },
  440. },
  441. {
  442. HostPath: models.StHostPath{
  443. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  444. MountPath: Snn4imagenetMountPath,
  445. ReadOnly: true,
  446. },
  447. },
  448. {
  449. HostPath: models.StHostPath{
  450. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  451. MountPath: BrainScoreMountPath,
  452. ReadOnly: true,
  453. },
  454. },
  455. }
  456. if len(datasetInfos) == 1 {
  457. volumes = append(volumes, models.Volume{
  458. HostPath: models.StHostPath{
  459. Path: datasetInfos[task.Uuid].DataLocalPath,
  460. MountPath: DataSetMountPath,
  461. ReadOnly: true,
  462. },
  463. })
  464. } else {
  465. for _, dataset := range datasetInfos {
  466. volumes = append(volumes, models.Volume{
  467. HostPath: models.StHostPath{
  468. Path: dataset.DataLocalPath,
  469. MountPath: DataSetMountPath + "/" + dataset.Name,
  470. ReadOnly: true,
  471. },
  472. })
  473. }
  474. }
  475. createTime := timeutil.TimeStampNow()
  476. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  477. JobName: jobName,
  478. RetryCount: 1,
  479. GpuType: task.GpuQueue,
  480. Image: task.Image,
  481. TaskRoles: []models.TaskRole{
  482. {
  483. Name: SubTaskName,
  484. TaskNumber: 1,
  485. MinSucceededTaskCount: 1,
  486. MinFailedTaskCount: 1,
  487. CPUNumber: resourceSpec.CpuNum,
  488. GPUNumber: resourceSpec.GpuNum,
  489. MemoryMB: resourceSpec.MemMiB,
  490. ShmMB: resourceSpec.ShareMemMiB,
  491. Command: Command,
  492. NeedIBDevice: false,
  493. IsMainRole: false,
  494. UseNNI: false,
  495. },
  496. },
  497. Volumes: volumes,
  498. })
  499. if err != nil {
  500. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  501. return err
  502. }
  503. if jobResult.Code != Success {
  504. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  505. return errors.New(jobResult.Msg)
  506. }
  507. var jobID = jobResult.Payload["jobId"].(string)
  508. newTask := &models.Cloudbrain{
  509. Status: string(models.JobWaiting),
  510. UserID: task.UserID,
  511. RepoID: task.RepoID,
  512. JobID: jobID,
  513. JobName: task.JobName,
  514. DisplayJobName: task.DisplayJobName,
  515. SubTaskName: task.SubTaskName,
  516. JobType: task.JobType,
  517. Type: task.Type,
  518. Uuid: task.Uuid,
  519. DatasetName: task.DatasetName,
  520. Image: task.Image,
  521. GpuQueue: task.GpuQueue,
  522. ResourceSpecId: task.ResourceSpecId,
  523. ComputeResource: task.ComputeResource,
  524. CreatedUnix: createTime,
  525. UpdatedUnix: createTime,
  526. BranchName: task.BranchName,
  527. }
  528. err = models.RestartCloudbrain(task, newTask)
  529. if err != nil {
  530. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  531. return err
  532. }
  533. stringId := strconv.FormatInt(newTask.ID, 10)
  534. *newID = stringId
  535. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  536. return nil
  537. }
  538. func DelCloudBrainJob(jobId string) string {
  539. task, err := models.GetCloudbrainByJobID(jobId)
  540. if err != nil {
  541. log.Error("get cloud brain err:", err)
  542. return "cloudbrain.Delete_failed"
  543. }
  544. if task.Status != string(models.JobStopped) && task.Status != string(models.JobFailed) && task.Status != string(models.JobSucceeded) {
  545. log.Error("the job(%s) has not been stopped", task.JobName)
  546. return "cloudbrain.Not_Stopped"
  547. }
  548. err = models.DeleteJob(task)
  549. if err != nil {
  550. log.Error("DeleteJob failed:", err)
  551. return "cloudbrain.Delete_failed"
  552. }
  553. deleteJobStorage(task.JobName)
  554. return ""
  555. }
  556. func deleteJobStorage(jobName string) error {
  557. //delete local
  558. localJobPath := setting.JobPath + jobName
  559. err := os.RemoveAll(localJobPath)
  560. if err != nil {
  561. log.Error("RemoveAll(%s) failed:%v", localJobPath, err)
  562. }
  563. dirPath := setting.CBCodePathPrefix + jobName + "/"
  564. err = storage.Attachments.DeleteDir(dirPath)
  565. if err != nil {
  566. log.Error("DeleteDir(%s) failed:%v", localJobPath, err)
  567. }
  568. return nil
  569. }
  570. func InitSpecialPool() {
  571. if SpecialPools == nil && setting.SpecialPools != "" {
  572. json.Unmarshal([]byte(setting.SpecialPools), &SpecialPools)
  573. }
  574. }
  575. func IsResourceSpecInSpecialPool(resourceSpecs []*models.ResourceSpec, resourceSpecId int) bool {
  576. if resourceSpecs == nil || len(resourceSpecs) == 0 {
  577. return true
  578. }
  579. for _, v := range resourceSpecs {
  580. if v.Id == resourceSpecId {
  581. return true
  582. }
  583. }
  584. return false
  585. }
  586. func IsQueueInSpecialtPool(pool []*models.GpuInfo, queue string) bool {
  587. for _, v := range pool {
  588. if v.Queue == queue {
  589. return true
  590. }
  591. }
  592. return false
  593. }
  594. func IsElementExist(s []string, str string) bool {
  595. for _, v := range s {
  596. if v == str {
  597. return true
  598. }
  599. }
  600. return false
  601. }