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

View file

@ -39,9 +39,10 @@ public class NginxStatusFetcher extends AbstractTask {
@Override
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){
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;
}