resolve job issues
[vfc/nfvo/lcm.git] / lcm / jobs / job_get.py
1 # Copyright 2016 ZTE Corporation.
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 import logging
15
16 from lcm.pub.utils.jobutil import JobUtil
17 from lcm.pub.utils.values import remove_none_key
18 from lcm.jobs.api_model import JobQueryResp, JobDescriptor, JobHistory
19
20
21 logger = logging.getLogger(__name__)
22
23
24 class GetJobInfoService(object):
25     def __init__(self, job_id, response_id=0):
26         self.job_id = job_id
27         self.response_id = response_id if response_id else 0
28
29     def do_biz(self):
30         logger.debug("GetJobInfoService, job_id=%s, response_id=%s", self.job_id, self.response_id)
31         jobs = JobUtil.query_job_status(self.job_id, self.response_id)
32         if not jobs:
33             job_query_resp = JobQueryResp(self.job_id)
34             return remove_none_key(job_query_resp.to_dict())
35         job_query_resp = JobQueryResp(
36             self.job_id,
37             JobDescriptor(
38                 jobs[0].status,
39                 jobs[0].progress,
40                 jobs[0].descp,
41                 jobs[0].errcode,
42                 jobs[0].indexid,
43                 [
44                     JobHistory(
45                         job.status,
46                         job.progress,
47                         job.descp,
48                         job.errcode,
49                         job.indexid
50                     ) for job in jobs[1:]
51                 ]
52             )
53         )
54         return remove_none_key(job_query_resp.to_dict())