Commit b66ed401 authored by renjie's avatar renjie

健康检查

parent e9069583
......@@ -46,6 +46,7 @@ public class IpResource implements Serializable {
private boolean isLocked;
private long lockTimestamp;
private long healthLockTimestamp;
private String unit;
private int period;
......@@ -285,4 +286,11 @@ public class IpResource implements Serializable {
this.specialLine = specialLine;
}
public long getHealthLockTimestamp() {
return healthLockTimestamp;
}
public void setHealthLockTimestamp(long healthLockTimestamp) {
this.healthLockTimestamp = healthLockTimestamp;
}
}
......@@ -10,6 +10,10 @@ public interface IpResourceRepositoryCustom {
boolean unLockTask(String id);
boolean healthLock(IpResource ipResource);
boolean unLockHealth(String id);
List<IpResource> sampleTasks(int status, long timestamp);
List<IpResource> sampleTasks(List<Integer> status);
......
......@@ -34,7 +34,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
BasicQuery basicQuery = new BasicQuery(doc);
Criteria criteria = new Criteria();
criteria.orOperator(where("id").is(ipResource.getId()).and("isLocked").is(false).and("status").is(ipResource.getStatus()).and("isDeleted").is(false),
where("lockTimestamp").lte(Instant.now().minusSeconds(300).toEpochMilli()).and("status").is(ipResource.getStatus())).and("isDeleted").is(false);
where("lockTimestamp").lte(Instant.now().minusSeconds(300).toEpochMilli()).and("status").is(ipResource.getStatus()).and("isDeleted").is(false));
basicQuery.addCriteria(criteria);
Update update = new Update();
update.set("isLocked", true).set("lockTimestamp", Instant.now().toEpochMilli());
......@@ -61,11 +61,44 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
return true;
}
@Override
public boolean healthLock(IpResource ipResource) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
Criteria criteria = new Criteria();
criteria.orOperator(where("id").is(ipResource.getId()).and("isLocked").is(false).and("status").is(ipResource.getStatus()).and("isDeleted").is(false),
where("healthLockTimestamp").lte(Instant.now().minusSeconds(300).toEpochMilli()).and("status").is(ipResource.getStatus()).and("isDeleted").is(false));
basicQuery.addCriteria(criteria);
Update update = new Update();
update.set("isLocked", true).set("healthLockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean unLockHealth(String id) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(id));
Update update = new Update();
update.set("isLocked", false).set("healthLockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public List<IpResource> sampleTasks(int status, long timestamp) {
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").is(status).and("isLocked").is(false).and("isDeleted").is(false),
where("status").is(status).and("isLocked").is(true).and("lockTimestamp").lte(timestamp)).and("isDeleted").is(false);
where("status").is(status).and("isLocked").is(true).and("lockTimestamp").lte(timestamp).and("isDeleted").is(false));
MatchOperation match = Aggregation.match(matchCriteria);
......@@ -79,8 +112,8 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override
public List<IpResource> sampleTasks(List<Integer> status) {
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").in(status).and("isLocked").is(false).and("isDeleted").is(false).and("lockTimestamp").lte(Instant.now().toEpochMilli()),
where("status").in(status).and("isLocked").is(true).and("lockTimestamp").lte((Instant.now().toEpochMilli()) - 60*1000*30)).and("isDeleted").is(false);
matchCriteria.orOperator(where("status").in(status).and("isLocked").is(false).and("isDeleted").is(false).and("healthLockTimestamp").lte(((Instant.now().toEpochMilli()) - 60*1000*30)),
where("status").in(status).and("isLocked").is(true).and("healthLockTimestamp").lte((Instant.now().toEpochMilli()) - (60*1000*30 + 60*1000*5)).and("isDeleted").is(false));
MatchOperation match = Aggregation.match(matchCriteria);
......
......@@ -279,7 +279,10 @@ public class IpResourceServiceImpl implements IpResourceService {
break;
}
ipResource.setStatus(6);
ipResource.setValidTime(Instant.now().plusSeconds(3600*24*30).toEpochMilli());
if (ipResourceRequestDto.getUnit().equals("week"))
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli());
else
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli());
ipResource.setUsername(USERNAME);
ipResource.setPort(port);
}
......
......@@ -416,8 +416,6 @@ public class ShopServiceImpl implements ShopService {
if (shops == null || shops.getNumberOfElements() < 1)
return new ShopPageResultDto();
List<ShopResultDto> shopResultDtos = new ArrayList<>();
logger.error("shops.size" + shops.getNumberOfElements());
logger.error("shops.content.size " + shops.getContent().size());
shops.getContent().stream().forEach(x -> {
IpResource ipResource = ipResourceRepository.findFirstByShopIdAndIsDeleted(x.getShopId(), false);
if (ipResource == null)
......
......@@ -219,36 +219,43 @@ public class BrowserTask {
}
}
// @Scheduled(cron = "0 0/1 * * * ?")
@Scheduled(cron = "0 0/1 * * * ?")
public void healthCheck() {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
List<IpResource> ipResources = ipResourceRepository.sampleTasks(Arrays.asList(0, 2));
for (IpResource ipResource : ipResources) {
long start = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) {
if (ipResourceRepository.healthLock(ipResource)) {
try {
QueryIpUrlList queryIpUrlList = queryIpUrlListRepository.findAll().get(0);
Trans trans;
if (ipResource.isSpecialLine()) {
trans = new Trans();
Trans trans = new Trans(ipResource.getProxyUsername(), ipResource.getProxyPassword());
String sp_result = trans.get(queryIpUrlList.getUrl(), true);
if (!sp_result.contains(ipResource.getAddr()))
NotifyUtils.sendMessage("防关联浏览器 ip " + ipResource.getAddr() + " 专线代理异常", NotifyUtils.MsgType.WEBHOOK);
log.error(sp_result);
}
String result;
Trans trans = new Trans(ipResource.getAddr(), Integer.valueOf(ipResource.getPort().size() > 1?ipResource.getPort().get(1):ipResource.getPort().get(0)), ipResource.getUsername(), ipResource.getPassword());
result = trans.get(queryIpUrlList.getUrl(), false);
if (!result.contains(ipResource.getAddr()))
NotifyUtils.sendMessage("防关联浏览器 ip " + ipResource.getAddr() + " 代理异常", NotifyUtils.MsgType.WEBHOOK);
} catch (Exception e) {
NotifyUtils.sendMessage("防关联浏览器 ip " + ipResource.getAddr() + " 代理异常", e, NotifyUtils.MsgType.WEBHOOK);
log.error(e.getMessage(), e);
} finally {
long end = System.currentTimeMillis();
log.debug("queryIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId());
try {
ipResourceRepository.unLockTask(ipResource.getId());
ipResourceRepository.unLockHealth(ipResource.getId());
} catch (Throwable th) {
log.error("unlock failed", th);
//try again
ipResourceRepository.unLockTask(ipResource.getId());
ipResourceRepository.unLockHealth(ipResource.getId());
}
}
}
}, ThreadPoolUtils.queryIpTasksPool);
}, ThreadPoolUtils.queryIpHealth);
}
}
......
......@@ -38,14 +38,14 @@ public abstract class ThreadPoolUtils {
});
private static final int IPTRANSACTION_POOL_COUNT = 10;
private static final int IPTRANSACTION_POOL_COUNT = 20;
public static final ScheduledExecutorService queryIpTransactionPool = Executors.newScheduledThreadPool(IPTRANSACTION_POOL_COUNT, new ThreadFactory() {
public static final ExecutorService queryIpHealth = Executors.newFixedThreadPool(IPTRANSACTION_POOL_COUNT, new ThreadFactory() {
int count = 1;
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "browser-ipTransaction-task-" + count++);
return new Thread(runnable, "browser-ipHealthCheck-task-" + count++);
}
});
}
......@@ -34,8 +34,11 @@ public class Trans {
this.username = username;
}
public Trans() {
public Trans(String username, String password) {
this.host = specialHost;
this.port = specialPort;
this.password = password;
this.username = username;
}
/**
......@@ -88,11 +91,12 @@ public class Trans {
} catch (ClientProtocolException e) {
NotifyUtils.sendMessage("防关联浏览器 ip " + host + " 代理异常", e, NotifyUtils.MsgType.WEBHOOK);
logger.error(e.getMessage(), e);
return "";
} catch (IOException e) {
NotifyUtils.sendMessage("防关联浏览器 ip " + host + " 代理异常", e, NotifyUtils.MsgType.WEBHOOK);
logger.error(e.getMessage(), e);
return "";
}
System.out.println(sb.toString());
return sb.toString();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment