task.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package hcso
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/log"
  19. )
  20. func (self *SRegion) waitTaskStatus(serviceType string, taskId string, targetStatus string, interval time.Duration, timeout time.Duration) error {
  21. start := time.Now()
  22. for time.Now().Sub(start) < timeout {
  23. status, err := self.GetTaskStatus(serviceType, taskId)
  24. if err != nil {
  25. return err
  26. }
  27. if status == targetStatus {
  28. break
  29. } else if status == TASK_FAIL {
  30. return fmt.Errorf("task %s failed", taskId)
  31. } else {
  32. time.Sleep(interval)
  33. }
  34. }
  35. return nil
  36. }
  37. func (self *SRegion) GetTaskStatus(serviceType string, taskId string) (string, error) {
  38. querys := map[string]string{"service_type": serviceType}
  39. task, err := self.ecsClient.Jobs.Get(taskId, querys)
  40. if err != nil {
  41. return "", err
  42. }
  43. status, err := task.GetString("status")
  44. if status == TASK_FAIL {
  45. log.Debugf("task %s failed: %s", taskId, task.String())
  46. }
  47. return status, err
  48. }
  49. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0022225398.html
  50. // 数据结构 entities -> []job
  51. func (self *SRegion) GetAllSubTaskEntityIDs(serviceType string, taskId string, entityKeyName string) ([]string, error) {
  52. err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
  53. if err != nil {
  54. return nil, err
  55. }
  56. querys := map[string]string{"service_type": serviceType}
  57. ret, err := self.ecsClient.Jobs.Get(taskId, querys)
  58. if err != nil {
  59. return nil, err
  60. }
  61. entities, err := ret.GetArray("entities", "sub_jobs")
  62. if err != nil {
  63. return nil, err
  64. }
  65. ids := make([]string, 0)
  66. for i := range entities {
  67. entity := entities[i]
  68. rid, err := entity.GetString("entities", entityKeyName)
  69. if err != nil {
  70. return nil, err
  71. }
  72. ids = append(ids, rid)
  73. }
  74. return ids, nil
  75. }
  76. // 数据结构 entities -> job
  77. func (self *SRegion) GetTaskEntityID(serviceType string, taskId string, entityKeyName string) (string, error) {
  78. err := self.waitTaskStatus(serviceType, taskId, TASK_SUCCESS, 10*time.Second, 600*time.Second)
  79. if err != nil {
  80. return "", err
  81. }
  82. querys := map[string]string{"service_type": serviceType}
  83. ret, err := self.ecsClient.Jobs.Get(taskId, querys)
  84. if err != nil {
  85. return "", err
  86. }
  87. return ret.GetString("entities", entityKeyName)
  88. }