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.

job.go 3.1 kB

7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package hpc
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
  7. "gitlink.org.cn/cloudream/common/utils/http2"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. )
  10. type CreateHPCJobReq struct {
  11. Name string `json:"name"`
  12. Description string `json:"description"`
  13. ClusterID schsdk.ClusterID `json:"clusterId"`
  14. Backend string `json:"backend"`
  15. App string `json:"app"`
  16. OperateType string `json:"operateType"`
  17. ScriptContent string `json:"scriptContent"`
  18. //Parameters HPCParameter `json:"parameters"`
  19. Parameters map[string]string `json:"parameters"`
  20. }
  21. //type HPCParameter struct {
  22. // JobName string `json:"jobName"`
  23. // Partition string `json:"partition"`
  24. // Ntasks string `json:"ntasks"`
  25. // Nodes string `json:"nodes"`
  26. // BamFile string `json:"bamFile"`
  27. // InputFile string `json:"inputFile"`
  28. //}
  29. type CreateJobResp struct {
  30. Backend string `json:"backend"`
  31. JobInfo HPCJobInfo `json:"jobInfo"`
  32. }
  33. type HPCJobInfo struct {
  34. JobDir string `json:"jobDir"`
  35. JobID string `json:"jobId"`
  36. TaskID string `json:"taskId"`
  37. }
  38. func (c *Client) CreateJob(req CreateHPCJobReq, token string) (*CreateJobResp, error) {
  39. targetUrl, err := url.JoinPath(c.baseURL, "/hpc/commitHpcTask")
  40. if err != nil {
  41. return nil, err
  42. }
  43. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  44. Body: req,
  45. Header: map[string]string{
  46. "Authorization": token,
  47. },
  48. })
  49. if err != nil {
  50. return nil, err
  51. }
  52. // 打印resp.Body内容
  53. //body, err := io.ReadAll(resp.Body)
  54. //if err != nil {
  55. // println("Error reading response body:", err)
  56. //}
  57. //println("Response Body:", string(body))
  58. contType := resp.Header.Get("Content-Type")
  59. if strings.Contains(contType, http2.ContentTypeJSON) {
  60. var codeResp respons2[CreateJobResp]
  61. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  62. return nil, fmt.Errorf("parsing response: %w", err)
  63. }
  64. if codeResp.Code == ResponseCodeOK {
  65. return &codeResp.Data, nil
  66. }
  67. return nil, fmt.Errorf("error: %s", codeResp.Message)
  68. }
  69. return nil, fmt.Errorf("unknow response content type: %s", contType)
  70. }
  71. func (c *Client) GetHPCAppClusters(app string, token string) (*AppClusterResp, error) {
  72. targetUrl, err := url.JoinPath(c.baseURL, "/hpc/getHpcAppCluster")
  73. if err != nil {
  74. return nil, err
  75. }
  76. resp, err := http2.GetForm(targetUrl, http2.RequestParam{
  77. Query: map[string]string{
  78. "app": app,
  79. },
  80. Header: map[string]string{
  81. "Authorization": token,
  82. },
  83. })
  84. if err != nil {
  85. return nil, err
  86. }
  87. contType := resp.Header.Get("Content-Type")
  88. if strings.Contains(contType, http2.ContentTypeJSON) {
  89. var codeResp respons2[AppClusterResp]
  90. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  91. return nil, fmt.Errorf("parsing response: %w", err)
  92. }
  93. if codeResp.Code == ResponseCodeOK {
  94. return &codeResp.Data, nil
  95. }
  96. return nil, fmt.Errorf("error: %s", codeResp.Message)
  97. }
  98. return nil, fmt.Errorf("unknow response content type: %s", contType)
  99. }