Commit 74b4513f authored by xuxin's avatar xuxin

专线自动回落

parent 8bccdd6e
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.service.GlobalFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xuxin
* @date 2020/8/3 18:28
* @description
*/
@RestController
@RequestMapping("/globalfield")
public class GlobalFieldController {
@Autowired
private GlobalFieldService globalFieldService;
@GetMapping("/speciallinestate")
public ResultDto querySpecialLineState() {
ResultDto resultDto = new ResultDto();
resultDto.setStatus(0);
resultDto.setData(globalFieldService.querySpecialLineState());
return resultDto;
}
}
package com.edgec.browserbackend.browser.domain;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author xuxin
* @date 2020/8/3 14:37
* @description 用来存放一些全局变量
*/
@Data
@Document(collection = "globalfield")
public class GlobalField {
@Id
private String id;
/**
* 专线状态
* on:正常
* off: 挂掉了
*/
private String specialLineState;
/**
* 注册礼金
* 目前是注册就送 16 元
*/
private Integer registerGift;
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.GlobalField;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author xuxin
* @date 2020/8/3 16:54
* @description
*/
public interface GlobalFieldRepository extends MongoRepository<GlobalField, String> {
}
package com.edgec.browserbackend.browser.repository;
/**
* @author xuxin
* @date 2020/8/3 17:00
* @description
*/
public interface GlobalFieldRepositoryCustom {
}
package com.edgec.browserbackend.browser.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
/**
* @author xuxin
* @date 2020/8/3 17:02
* @description
*/
public class GlobalFieldRepositoryCustomImpl implements GlobalFieldRepositoryCustom {
@Autowired
private MongoTemplate mongoTemplate;
}
......@@ -34,4 +34,6 @@ public interface IpResourceRepositoryCustom {
List<IpResource> findShopIdInListAndStatus(List<String> shopIds, boolean isDeleted, int status);
List<IpResource> findShopIdInListAndRegionLike(List<String> shopIds, boolean isDeleted, String region);
List<IpResource> specialLineCheckTask();
}
......@@ -15,6 +15,8 @@ import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant;
import java.util.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sample;
import static org.springframework.data.mongodb.core.query.Criteria.where;
public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCustom {
......@@ -73,7 +75,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(100);
SampleOperation sample = sample(100);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults();
......@@ -90,7 +92,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(20);
SampleOperation sample = sample(20);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults();
......@@ -214,5 +216,22 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
return result;
}
@Override
public List<IpResource> specialLineCheckTask() {
AggregationOperation match = Aggregation.match(
Criteria.where("status").in(Arrays.asList(0L, 2L))
.and("specialLine").is(true)
.and("bind").is(true)
.and("isDeleted").is(false)
.and("validTime").gt(Instant.now().toEpochMilli())
);
AggregationOperation sample = sample(20);
Aggregation aggregation = newAggregation(match, sample);
AggregationResults<IpResource> results = mongoTemplate.aggregate(aggregation, IpResource.class, IpResource.class);
List<IpResource> list = results.getMappedResults();
return list;
}
}
package com.edgec.browserbackend.browser.service;
/**
* @author xuxin
* @date 2020/8/3 18:03
* @description
*/
public interface GlobalFieldService {
String querySpecialLineState();
}
package com.edgec.browserbackend.browser.service.Impl;
import com.edgec.browserbackend.browser.repository.GlobalFieldRepository;
import com.edgec.browserbackend.browser.service.GlobalFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author xuxin
* @date 2020/8/3 18:04
* @description
*/
@Service
public class GlobalFieldServiceImpl implements GlobalFieldService {
@Autowired
private GlobalFieldRepository globalFieldRepository;
@Override
public String querySpecialLineState() {
String state = "on";
if (!globalFieldRepository.findAll().isEmpty()) {
state = globalFieldRepository.findAll().get(0).getSpecialLineState();
}
return state;
}
}
package com.edgec.browserbackend.browser.task;
import com.edgec.browserbackend.account.domain.QueryIpUrlList;
import com.edgec.browserbackend.account.repository.QueryIpUrlListRepository;
import com.edgec.browserbackend.browser.domain.GlobalField;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.repository.GlobalFieldRepository;
import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.common.utils.Trans;
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.Component;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/3 14:29
* @description
*/
@Component
public class SpecialLineCheckTask {
private static final Logger log = LoggerFactory.getLogger(SpecialLineCheckTask.class);
@Autowired
private IpResourceRepository ipResourceRepository;
@Autowired
private QueryIpUrlListRepository queryIpUrlListRepository;
@Autowired
private GlobalFieldRepository globalFieldRepository;
@Scheduled(cron = "0 0/5 * * * ?")
public void healthCheck() {
// 1. 随机获取符合条件的 20 个 ip
List<IpResource> ipResourceList = ipResourceRepository.specialLineCheckTask();
// 2. 对获取的ip进行检查
int[] arr = getCheckArray(ipResourceList);
int finalSuccess = arr[0];
int finalFail = arr[1];
GlobalField globalField = getGlobalField();
// 3. 获取 specialLine 的状态,并判断是否需要更新数据库状态
String specialLineState = null;
if ("on".equals(globalField.getSpecialLineState())) {
specialLineState = finalFail == ipResourceList.size() ? "off" : null;
}
if ("off".equals(globalField.getSpecialLineState())) {
specialLineState = finalSuccess > 5 ? "on" : null;
}
if (!StringUtils.isEmpty(specialLineState)) {
globalField.setSpecialLineState(specialLineState);
globalFieldRepository.save(globalField);
}
}
private int[] getCheckArray(List<IpResource> ipResourceList) {
List<QueryIpUrlList> queryIpUrlLists = queryIpUrlListRepository.findAll();
int finalSuccess = 0;
int finalFail = 0;
for (IpResource ipResource : ipResourceList) {
int success = 0;
for (QueryIpUrlList queryIpUrlList : queryIpUrlLists) {
Trans trans = new Trans(ipResource.getProxyUsername(), ipResource.getProxyPassword());
String result = trans.get(queryIpUrlList.getUrl());
if (result.contains(ipResource.getAddr())) {
success++;
break;
}
}
if (success == 0) {
finalFail++;
}
if (success == 1) {
finalSuccess++;
}
}
int[] arr = {finalSuccess, finalFail};
return arr;
}
private GlobalField getGlobalField() {
List<GlobalField> globalFieldList = globalFieldRepository.findAll();
GlobalField globalField = null;
if (globalFieldList.isEmpty()) {
GlobalField g = new GlobalField();
g.setSpecialLineState("on");
globalField = globalFieldRepository.save(g);
} else {
globalField = globalFieldList.get(0);
}
return globalField;
}
}
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