package mingzuozhibi.spider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class AutoRunSpider {

    @Autowired
    private SakuraSpeedSpider sakuraSpeedSpider;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void fetchSakuraSpeedData() {
        fetchSakuraSpeedData(3);
    }

    private void fetchSakuraSpeedData(int retry) {
        Logger logger = LoggerFactory.getLogger(AutoRunSpider.class);
        if (retry == 0) {
            if (logger.isWarnEnabled()) {
                logger.warn("fetching sakura speed data failed");
            }
        } else {
            if (logger.isInfoEnabled()) {
                logger.info("fetching sakura speed data ({})", retry);
                try {
                    sakuraSpeedSpider.fetch();
                } catch (IOException e) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("fetching sakura speed data throw an error", e);
                    }
                    fetchSakuraSpeedData(retry - 1);
                }
            }
        }
    }

}

@@ -903,24 +903,34 @@
 retry);%0A    
+        %7D%0A
             
@@ -935,20 +935,16 @@
   try %7B%0A
-    
         
@@ -978,20 +978,16 @@
 etch();%0A
-    
         
@@ -1024,36 +1024,32 @@
                 
-    
 if (logger.isDeb
@@ -1055,36 +1055,32 @@
 bugEnabled()) %7B%0A
-    
                 
@@ -1157,34 +1157,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1216,37 +1216,145 @@
 1);%0A            
+%7D%0A
     
-%7D
+        if (logger.isInfoEnabled()) %7B%0A                logger.info(%22fetched sakura speed data (%7B%7D)%22, retry);
 %0A            %7D%0A 

move spider fetch invoke