package org.popkit.leap.monitor;

import org.popkit.core.logger.LeapLogger;
import org.popkit.leap.elpa.entity.ActorStatus;
import org.popkit.leap.elpa.services.PkgFetchService;
import org.popkit.leap.monitor.entity.FetcherStatus;

import java.util.concurrent.*;

/**
 * Created by Aborn Jiang
 * Mail aborn.jiang@gmail.com
 * 2016-05-14:22:21
 */
public class FetcherTask extends Thread {

    private String pkgName;
    private PkgFetchService fetchService;

    public FetcherTask(String pkgName, PkgFetchService fetchService) {
        this.pkgName = pkgName;
        this.fetchService = fetchService;
    }

    public void run() {
        LeapLogger.info("pkgName=[" + pkgName + "]正在进行fetch...");
        FutureTask<FetcherStatus> futureTask = new FutureTask<FetcherStatus>(
                new Callable<FetcherStatus>() {
                    public FetcherStatus call() throws Exception {
                        try {
                            fetchService.downloadPackage(pkgName);
                        } catch (Throwable throwable) {
                            LeapLogger.warn("downloadPackage [" + pkgName + "] throwable", throwable);
                        }
                        return new FetcherStatus(true, "pkgName=[" + pkgName + "]fetch完成!");
                    }
                }
        );
        futureTask.run();

        try {
            FetcherStatus status = futureTask.get(10, TimeUnit.MINUTES);
            if (status == null) {
                LeapLogger.info("pkgName=[" + pkgName + "] fetch future timeout!");
            } else {
                LeapLogger.info("pkgName=[" + pkgName + "] fetch future finished!" + status.getInfo());
            }
        } catch (InterruptedException e) {
            LeapLogger.info("pkgName=[" + pkgName + "]fetch timeout InterruptedException!");
        } catch (ExecutionException e) {
            LeapLogger.info("pkgName=[" + pkgName + "]fetch timeout ExecutionException!");
        } catch (TimeoutException e) {
            LeapLogger.info("pkgName=[" + pkgName + "]fetch timeout TimeoutException!");
        } finally {
            RoundStatusMonitor.updateFetcherStatus(pkgName, ActorStatus.FINISHED);
        }

        /**
        // TODO 如果失败,会出问题!!!,会使得没有更新
        if (fetchService.downloadPackage(pkgName)) {
            RoundStatusMonitor.updateFetcherStatus(pkgName, ActorStatus.FINISHED);
        }
         LeapLogger.info("pkgName=[" + pkgName + "]fetch完成!");
        **/
    }

    public String getPkgName() {
        return pkgName;
    }
}

@@ -599,32 +599,46 @@
 Service;%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -657,84 +657,16 @@
     
-LeapLogger.info(%22pkgName=%5B%22 + pkgName + %22%5D%E6%AD%A3%E5%9C%A8%E8%BF%9B%E8%A1%8Cfetch...%22);%0A        FutureTask
+Callable
 %3CFet
@@ -681,67 +681,22 @@
 us%3E 
-future
+download
 Task =
- new FutureTask%3CFetcherStatus%3E(%0A               
  new
@@ -720,32 +720,24 @@
 Status%3E() %7B%0A
-        
             
@@ -799,24 +799,69 @@
         
+long beginTime = System.currentTimeMillis();%0A
         
 try %7B%0A  
@@ -848,38 +848,32 @@
 ();%0A        
-try %7B%0A
         
             
@@ -852,32 +852,38 @@
                 
+try %7B%0A
                 
@@ -941,24 +941,16 @@
         
-        
 %7D catch 
@@ -954,17 +954,17 @@
 ch (
-Throwable
+Exception
  thr
@@ -969,32 +969,24 @@
 hrowable) %7B%0A
-        
             
@@ -1080,32 +1080,34 @@
             
+%7D%0A
         
 %7D%0A          
@@ -1090,34 +1090,32 @@
   %7D%0A        
-%7D%0A
         
             
@@ -1094,32 +1094,99 @@
                 
+long timeCost = (System.currentTimeMillis() - beginTime) / (1000);%0A
                 
@@ -1251,26 +1251,47 @@
 etch%E5%AE%8C%E6%88%90!%22
-);%0A       
+ + %22, %E8%80%97%E6%97%B6:%22 + timeCost + %22s.%22);%0A
         
@@ -1286,33 +1286,32 @@
 %22);%0A            
- 
 %7D%0A              
@@ -1300,32 +1300,36 @@
   %7D%0A        
+%7D;%0A%0A
         
 %7D%0A        );
@@ -1320,48 +1320,140 @@
     
-%7D%0A        );%0A        futureTask.run(
+ExecutorService service = Executors.newSingleThreadExecutor();%0A        Future%3CFetcherStatus%3E future = service.submit(downloadTask
 );%0A
-%0A
     
@@ -1503,20 +1503,16 @@
 = future
-Task
 .get(10,
@@ -1885,30 +1885,23 @@
 ame + %22%5D
+ 
 fetch 
-timeout 
 Interrup
@@ -2012,30 +2012,23 @@
 ame + %22%5D
+ 
 fetch 
-timeout 
 Executio
@@ -2073,32 +2073,65 @@
 tException e) %7B%0A
+            future.cancel(true);%0A
             Leap
@@ -2164,24 +2164,25 @@
 pkgName + %22%5D
+ 
 fetch timeou

设置10分钟超时