yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/job.go (about)

     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  
    15  package hcs
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  )
    28  
    29  const (
    30  	TASK_SUCCESS = "SUCCESS"
    31  	TASK_FAIL    = "FAIL"
    32  )
    33  
    34  type SJob struct {
    35  	Status   string `json:"status"`
    36  	Entities struct {
    37  		SubJobsJotal int
    38  		ImageId      string
    39  		ServerId     string
    40  		SubJobs      []SJob
    41  	} `json:"entities"`
    42  	JobId      string `json:"job_id"`
    43  	JobType    string `json:"job_type"`
    44  	BeginTime  string `json:"begin_time"`
    45  	EndTime    string `json:"end_time"`
    46  	ErrorCode  string `json:"error_code"`
    47  	FailReason string `json:"fail_reason"`
    48  }
    49  
    50  func (self *SJob) GetIds() []string {
    51  	ret := []string{}
    52  	if len(self.Entities.ImageId) > 0 {
    53  		ret = append(ret, self.Entities.ImageId)
    54  	}
    55  	if len(self.Entities.ServerId) > 0 {
    56  		ret = append(ret, self.Entities.ServerId)
    57  	}
    58  	for _, sub := range self.Entities.SubJobs {
    59  		if len(sub.Entities.ServerId) > 0 {
    60  			ret = append(ret, sub.Entities.ServerId)
    61  		}
    62  	}
    63  	return ret
    64  }
    65  
    66  func (self *SHcsClient) waitJobSuccess(serviceType, regionId string, jobId string, interval time.Duration, timeout time.Duration) (*SJob, error) {
    67  	start := time.Now()
    68  	var job *SJob
    69  	var err error
    70  	for time.Now().Sub(start) < timeout {
    71  		job, err = self.GetJob(serviceType, regionId, jobId)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  		log.Infof("wait %s job %s(%s) status: %s", serviceType, job.JobId, job.JobType, job.Status)
    76  		if job.Status == TASK_SUCCESS {
    77  			return job, nil
    78  		}
    79  		reason := []string{job.FailReason}
    80  		for _, subJob := range job.Entities.SubJobs {
    81  			if len(subJob.FailReason) > 0 {
    82  				reason = append(reason, subJob.FailReason)
    83  			}
    84  		}
    85  		if job.Status == TASK_FAIL {
    86  			return nil, fmt.Errorf("job %s failed reason %s", jobId, strings.Join(reason, ";"))
    87  		}
    88  		time.Sleep(interval)
    89  	}
    90  	return nil, errors.Wrapf(cloudprovider.ErrTimeout, "wait job %s", jsonutils.Marshal(job))
    91  }
    92  
    93  func (self *SHcsClient) GetJob(service, regionId string, jobId string) (*SJob, error) {
    94  	resp, err := self._getJob(service, regionId, jobId)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	ret := &SJob{}
    99  	return ret, resp.Unmarshal(ret)
   100  
   101  }
   102  
   103  func (self *SRegion) GetJob(service string, jobId string) (*SJob, error) {
   104  	return self.client.GetJob(service, self.Id, jobId)
   105  }