Ver Fonte

add:添加排队中任务状态处理

sunjh há 5 anos atrás
pai
commit
99a10d5d6e

+ 18 - 11
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/ThirdPartToolAppServiceImpl.java

@@ -16,7 +16,6 @@ import cn.iselab.mooctest.site.service.ThirdPartToolAppService;
 import com.google.gson.Gson;
 import net.sf.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -42,7 +41,7 @@ public class ThirdPartToolAppServiceImpl implements ThirdPartToolAppService {
     private String generateReportHost = "106.14.81.62";
 
     //@Value("${thirdPartTool.reportService.port}")
-    private String generateReportPort= "8080";
+    private String generateReportPort = "8080";
 
     @Autowired
     private ThirdPartToolJobDao thirdPartToolJobDao;
@@ -160,7 +159,7 @@ public class ThirdPartToolAppServiceImpl implements ThirdPartToolAppService {
         ThirdPartToolAppStatusResponse appDataJobStatus;
         try {
             appDataJobStatus = rt.getForObject(url, ThirdPartToolAppStatusResponse.class);
-        }catch (Exception e){
+        } catch (Exception e) {
             throw new Exception("[jobs status:running]fail to get job status:" + jobId);
         }
         if (null == appDataJobStatus) {
@@ -181,12 +180,16 @@ public class ThirdPartToolAppServiceImpl implements ThirdPartToolAppService {
         Gson gson = new Gson();
         ThirdPartToolJob thirdPartToolJob = thirdPartToolJobDao.findOne(jobId);
         if (null == thirdPartToolJob) {
-            throw new Exception("this job is not exist:" + jobId);
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId);
         }
         if (null == thirdPartToolJob.getConfig()) {
-            throw new Exception("this job's config is empty:" + jobId);
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId);
+        }
+        String config = thirdPartToolJob.getConfig();
+        if (null == config) {
+            throw new Exception("[jobs status:pending]fail to get this job's config :" + jobId);
         }
-        ThirdPartToolAppStartConfig thirdPartToolAppStartConfig = gson.fromJson(thirdPartToolJob.getConfig(), ThirdPartToolAppStartConfig.class);
+        ThirdPartToolAppStartConfig thirdPartToolAppStartConfig = gson.fromJson(config, ThirdPartToolAppStartConfig.class);
 
         ThirdPartToolAppStartCommand thirdPartToolAppStartCommand = new ThirdPartToolAppStartCommand();
         //TODO set command
@@ -197,18 +200,21 @@ public class ThirdPartToolAppServiceImpl implements ThirdPartToolAppService {
         RestTemplate rt = new RestTemplate();
         HttpEntity<ThirdPartToolAppStartCommand> httpEntity = new HttpEntity<>(thirdPartToolAppStartCommand, getHeader());
         String url = getAppAutoTestStartUrl();
-        ThirdPartToolAppStartResponse thirdPartToolAppStartResponse = rt.postForObject(url, httpEntity, ThirdPartToolAppStartResponse.class);
-
+        ThirdPartToolAppStartResponse thirdPartToolAppStartResponse;
+        try {
+            thirdPartToolAppStartResponse = rt.postForObject(url, httpEntity, ThirdPartToolAppStartResponse.class);
+        } catch (Exception e) {
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId + ";" + e.getMessage());
+        }
         if (null == thirdPartToolAppStartResponse) {
-            throw new Exception("start job fail:" + jobId);
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId);
         }
 
         if (thirdPartToolAppStartResponse.getStatusCode().equals(ThirdPartToolAppStartResponseCode.CREATE)) {
             thirdPartToolJob.setTraceId(thirdPartToolAppStartResponse.getTraceId());
-            thirdPartToolJob.setStatus(ThirdPartToolJobStatusConstants.RUNNING);
             thirdPartToolJobDao.save(thirdPartToolJob);
         } else {
-            throw new Exception("start job fail:" + jobId);
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId +";jobstatus:" + thirdPartToolAppStartResponse.getStatusCode());
         }
     }
 
@@ -278,6 +284,7 @@ public class ThirdPartToolAppServiceImpl implements ThirdPartToolAppService {
         headers.setContentType(MediaType.APPLICATION_JSON);
         return headers;
     }
+
     private String getGenerateReportUrl(Long orderId, Long ownerId, String reportType) {
         String api = "/api/report/generate/order/{orderId}/owner/{ownerId}/reportType/{reportType}"
                 .replace("{orderId}", "" + orderId)

+ 17 - 4
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/ThirdPartToolWebServiceImpl.java

@@ -222,7 +222,16 @@ public class ThirdPartToolWebServiceImpl implements ThirdPartToolWebService {
     public void runJob(Long jobId) throws Exception {
         Gson gson = new Gson();
         ThirdPartToolJob thirdPartToolJob = thirdPartToolJobDao.findOne(jobId);
-        ThirdPartToolWebStartConfig thirdPartToolWebStartConfig = gson.fromJson(thirdPartToolJob.getConfig(), ThirdPartToolWebStartConfig.class);
+        String config = thirdPartToolJob.getConfig();
+        if (null == config){
+            throw new Exception("[jobs status:pending]fail to get config:" + jobId);
+        }
+        ThirdPartToolWebStartConfig thirdPartToolWebStartConfig;
+        try {
+            thirdPartToolWebStartConfig = gson.fromJson(config, ThirdPartToolWebStartConfig.class);
+        }catch (Exception e){
+            throw new Exception("[jobs status:pending]fail to get config:" + jobId);
+        }
 
         ThirdPartToolWebStartCommand thirdPartToolWebStartCommand = new ThirdPartToolWebStartCommand();
         thirdPartToolWebStartCommand.setMaximumStates(thirdPartToolWebStartConfig.getMaximumStates());
@@ -239,13 +248,17 @@ public class ThirdPartToolWebServiceImpl implements ThirdPartToolWebService {
         RestTemplate rt = new RestTemplate();
         HttpEntity<ThirdPartToolWebStartCommand> httpEntity = new HttpEntity<>(thirdPartToolWebStartCommand, getHeader());
         String url = getWebAutoTestStartUrl();
-        ThirdPartToolWebStartResponse thirdPartToolWebStartResponse = rt.postForObject(url, httpEntity, ThirdPartToolWebStartResponse.class);
+        ThirdPartToolWebStartResponse thirdPartToolWebStartResponse;
+        try {
+            thirdPartToolWebStartResponse = rt.postForObject(url, httpEntity, ThirdPartToolWebStartResponse.class);
+        }catch (Exception e){
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId);
+        }
 
         if (null == thirdPartToolWebStartResponse || !thirdPartToolWebStartResponse.getCode().equals(ThirdPartToolWebStartResponseCode.SUCCESS)) {
-            throw new Exception("start job fail:" + jobId);
+            throw new Exception("[jobs status:pending]fail to run job :" + jobId);
         } else {
             thirdPartToolJob.setTraceId(thirdPartToolWebStartResponse.getData().getSessionId());
-            thirdPartToolJob.setStatus(ThirdPartToolJobStatusConstants.RUNNING);
             thirdPartToolJobDao.save(thirdPartToolJob);
         }
     }