Refactor task TaskWorkerImpl and change log level from error to warn

This commit is contained in:
vwxc王兴朝 2016-03-08 13:37:16 +08:00
parent 6e7d5016e1
commit aa2934ad51
2 changed files with 32 additions and 36 deletions

View file

@ -1,65 +1,60 @@
package com.ctrip.zeus.executor.impl; package com.ctrip.zeus.executor.impl;
import com.ctrip.zeus.executor.TaskExecutor;
import com.ctrip.zeus.executor.TaskWorker; import com.ctrip.zeus.executor.TaskWorker;
import com.ctrip.zeus.model.entity.Slb;
import com.ctrip.zeus.service.model.EntityFactory; import com.ctrip.zeus.service.model.EntityFactory;
import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.model.SelectionMode;
import com.ctrip.zeus.service.model.SlbRepository;
import com.ctrip.zeus.executor.TaskExecutor;
import com.ctrip.zeus.service.query.SlbCriteriaQuery; import com.ctrip.zeus.service.query.SlbCriteriaQuery;
import com.ctrip.zeus.util.S; import com.ctrip.zeus.util.S;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
* Created by fanqq on 2015/7/31. * Created by fanqq on 2015/7/31.
*/ */
@Component("taskWorker") @Component("taskWorker")
public class TaskWorkerImpl implements TaskWorker { public class TaskWorkerImpl implements TaskWorker {
private static Long workerSlbId = null;
@Resource @Resource
SlbCriteriaQuery slbCriteriaQuery; private SlbCriteriaQuery slbCriteriaQuery;
@Resource @Resource
TaskExecutor taskExecutor; private TaskExecutor taskExecutor;
@Resource @Resource
EntityFactory entityFactory; private EntityFactory entityFactory;
private Logger logger = LoggerFactory.getLogger(this.getClass());
Logger logger = LoggerFactory.getLogger(this.getClass());
private static int initFailCount = 0;
@Override @Override
public void execute() { public void execute() {
String selfIp = S.getIp();
Long workerSlbId = null;
try { try {
//1. init //1. getWorkerSlbId
init(); workerSlbId = getWorkerSlbId();
}catch (Exception e ){
logger.error("Task Worker Init Fail."+e.getMessage(),e); //2. check slbId
} if (workerSlbId == null) {
//2. execute logger.warn("Can Not Found Slb by Local Ip. NginxStatusFetcher ignore task! Local Ip : " + selfIp);
if (workerSlbId!=null){ return;
}
//3. Do work
taskExecutor.execute(workerSlbId); taskExecutor.execute(workerSlbId);
}else { } catch (Exception e) {
logger.error("Task Worker Start Fail. WorkerSlbId is null!"); logger.error("Task Worker Execute Fail." + e.getMessage(), e);
} }
} }
private void init()throws Exception{ private Long getWorkerSlbId() throws Exception {
Long[] slbIds = entityFactory.getSlbIdsByIp(S.getIp(), SelectionMode.ONLINE_FIRST); Long workerSlbId = null;
if (slbIds == null || slbIds.length == 0){ String selfIp = S.getIp();
logger.error("Can Not Found Slb by Local Ip. TaskExecutor is not working!Local Ip : "+S.getIp()); Long[] slbIds = entityFactory.getSlbIdsByIp(selfIp, SelectionMode.ONLINE_FIRST);
return; if (slbIds != null && slbIds.length > 0) {
} workerSlbId = slbIds[0];
Long slbId = slbIds[0];
if (slbId != null && slbId > 0){
workerSlbId = slbId;
initFailCount = 0;
}else{
if (++initFailCount > 3){
workerSlbId = null;
}
logger.error("Can Not Found Slb by Local Ip. TaskExecutor is not working!Local Ip : "+S.getIp());
} }
return workerSlbId;
} }
} }

View file

@ -39,9 +39,10 @@ public class NginxStatusFetcher extends AbstractTask {
@Override @Override
public void run() throws Exception { public void run() throws Exception {
Long[] slbIds = entityFactory.getSlbIdsByIp(S.getIp(), SelectionMode.ONLINE_FIRST); String selfIp = S.getIp();
Long[] slbIds = entityFactory.getSlbIdsByIp(selfIp, SelectionMode.ONLINE_FIRST);
if (slbIds == null || slbIds.length == 0){ if (slbIds == null || slbIds.length == 0){
logger.error("Can Not Found Slb by Local Ip. NginxStatusFetcher ignore task! Local Ip : "+S.getIp()); logger.warn("Can Not Found Slb by Local Ip. NginxStatusFetcher ignore task! Local Ip : " + selfIp);
return; return;
} }