Commit 6f542436 authored by Administrator's avatar Administrator

Merge branch 'feature-purchaseFailure' into 'master'

购买失败处理以及注释

See merge request !195
parents 5d81e952 7133ef7e
...@@ -2,8 +2,6 @@ package com.edgec.browserbackend.browser.service; ...@@ -2,8 +2,6 @@ package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.dto.ShopRequestDto; import com.edgec.browserbackend.browser.dto.ShopRequestDto;
import java.util.List;
public interface IpAndShopService { public interface IpAndShopService {
void bindShop(String username, ShopRequestDto shopRequestDto); void bindShop(String username, ShopRequestDto shopRequestDto);
...@@ -11,6 +9,4 @@ public interface IpAndShopService { ...@@ -11,6 +9,4 @@ public interface IpAndShopService {
void unBindShop(String username, ShopRequestDto shopRequestDto); void unBindShop(String username, ShopRequestDto shopRequestDto);
void unBindShops(String username, ShopRequestDto shopRequestDto); void unBindShops(String username, ShopRequestDto shopRequestDto);
//void unBindShops(String username, String id, List<String> shopIds);
} }
...@@ -106,23 +106,26 @@ public class BrowserTask { ...@@ -106,23 +106,26 @@ public class BrowserTask {
*/ */
@Scheduled(cron = "0 0/1 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void buyIpTasks() { public void buyIpTasks() {
String logs = "【BrowserTask -- buyIpTasks】 ";
log.info(logs + "****************ip resource purchase start****************");
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL; String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
long time = Instant.now().minusSeconds(300).toEpochMilli(); long time = Instant.now().minusSeconds(300).toEpochMilli();
List<IpResource> ipResources = ipResourceRepository.sampleTasks(6, time); List<IpResource> ipResources = ipResourceRepository.sampleTasks(6, time);
log.info("buyIpTasks sample {} tasks", ipResources.size()); log.info(logs + "ip resource purchase tasks, {}", ipResources.size());
List<CompletableFuture> futureList = new ArrayList<>(); List<CompletableFuture> futureList = new ArrayList<>();
for (IpResource ipResource : ipResources) { for (IpResource ipResource : ipResources) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
CompletableFuture future = CompletableFuture.runAsync(() -> {
CompletableFuture future = CompletableFuture.runAsync(
() -> {
if (ipResourceRepository.lockTask(ipResource)) { if (ipResourceRepository.lockTask(ipResource)) {
log.info(logs + "ip resource purchase , {}", ipResource);
try { try {
boolean result = false; boolean result = false;
Map<String, String> header = buildPostHeader(); Map<String, String> header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResource.getUsername()); map.put("name", ipResource.getUsername());
if("uk".equals(ipResource.getRegion())) { if ("uk".equals(ipResource.getRegion())) {
map.put("region", "london"); map.put("region", "london");
} else { } else {
map.put("region", ipResource.getRegion()); map.put("region", ipResource.getRegion());
...@@ -138,13 +141,19 @@ public class BrowserTask { ...@@ -138,13 +141,19 @@ public class BrowserTask {
// HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header); // HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null; IpBuyResultDto ipBuyResultDto = null;
try { try {
log.info(logs + "ip resource purchase params, {}", map);
String requestResult = HttpClientutils.doPost(URL + "/intelligroup/ipresources?accountId=browser", header, JSONObject.toJSONString(map)); String requestResult = HttpClientutils.doPost(URL + "/intelligroup/ipresources?accountId=browser", header, JSONObject.toJSONString(map));
log.info(logs + "ip resource purchase result, {}", requestResult);
ipBuyResultDto = JSONObject.parseObject(requestResult, IpBuyResultDto.class); ipBuyResultDto = JSONObject.parseObject(requestResult, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) { if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) {
log.error("fail to buy ip"); log.error(logs + "fail to buy ip, {}", ipBuyResultDto.getErrorCode());
log.error(ipBuyResultDto.getErrorCode());
} }
if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() >= 1) { if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() >= 1) {
log.info(logs + "ip resource purchase successful, {}", ipResource);
AtomicInteger index = new AtomicInteger(); AtomicInteger index = new AtomicInteger();
ipBuyResultDto.getIplist().forEach( ipBuyResultDto.getIplist().forEach(
x -> { x -> {
...@@ -164,12 +173,20 @@ public class BrowserTask { ...@@ -164,12 +173,20 @@ public class BrowserTask {
result = true; result = true;
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(logs + "ip resource purchase Exception {}", e.getMessage(), e);
result = false; result = false;
} }
/**
* 购买失败处理
*
* 购买后,长时间未能成功的ip资源视为购买失败。
* 购买时长超过20分钟的ip资源释放,并将购买花销退回账户余额。
*/
if (result == false && (ipResource.getPurchasedTime() < Instant.now().minusSeconds(7200).toEpochMilli())) { if (result == false && (ipResource.getPurchasedTime() < Instant.now().minusSeconds(7200).toEpochMilli())) {
log.info(logs + "ip resource purchase failure, {}", ipResource);
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResource, 3, 0); IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResource, 3, 0);
accountService.chargeByMoney(ipResource.getUsername(), -ipResource.getPrice(), ipChargeRequestDto); accountService.chargeByMoney(ipResource.getOwner(), -ipResource.getPrice(), ipChargeRequestDto);
if (ipResource.getShopIds() != null && ipResource.getShopIds().size() > 0) { if (ipResource.getShopIds() != null && ipResource.getShopIds().size() > 0) {
ShopRequestDto shopRequestDto = new ShopRequestDto(); ShopRequestDto shopRequestDto = new ShopRequestDto();
shopRequestDto.setIpId(ipResource.getId()); shopRequestDto.setIpId(ipResource.getId());
...@@ -180,11 +197,11 @@ public class BrowserTask { ...@@ -180,11 +197,11 @@ public class BrowserTask {
} }
} finally { } finally {
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
log.debug("buyIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId()); log.info(logs + "buy ip task {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId());
try { try {
ipResourceRepository.unLockTask(ipResource.getId()); ipResourceRepository.unLockTask(ipResource.getId());
} catch (Throwable th) { } catch (Throwable th) {
log.error("unlock failed", th); log.error(logs + "unlock failed", th);
//try again //try again
ipResourceRepository.unLockTask(ipResource.getId()); ipResourceRepository.unLockTask(ipResource.getId());
} }
...@@ -199,40 +216,6 @@ public class BrowserTask { ...@@ -199,40 +216,6 @@ public class BrowserTask {
CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join(); CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join();
} }
private static int OVER40MIN = 40 * 60 * 1000;
/**
* 购买失败处理
* 购买后,长时间未能成功的ip资源视为购买失败。
* 每半个小时进行一次,将状态为3、购买时长超过40分钟的ip资源释放,
* 并将购买花销退回账户余额
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void purchaseFailureHandling() {
long now = Instant.now().minusSeconds(300).toEpochMilli();
//List<IpResource> ipResources = ipResourceRepository.findByStatusInAndIsDeletedAndIsLocked(Arrays.asList(3, 6));
List<IpResource> ipResources = ipResourceRepository.findByStatusInAndIsDeletedAndIsLocked(Arrays.asList(3, 6), false, false);
ipResources.forEach(ipResource -> {
if (ipResourceRepository.lockTask(ipResource)) {
if((now - ipResource.getPurchasedTime()) > OVER40MIN){
String username = ipResource.getUsername();
String id = ipResource.getId();
List<String> shopIds = ipResource.getShopIds();
log.error("Ip resource purchase failure, {}", ipResource);
log.error("charge by money, {}, {}, {}", username, -ipResource.getPrice(), buildIpChargeRequestDto(ipResource, 3, 0));
//accountService.chargeByMoney(username, -ipResource.getPrice(), buildIpChargeRequestDto(ipResource, 3, 0));
if (shopIds != null && shopIds.size() > 0) {
log.error("unbind Shops, {}, {}, {}", username, id, shopIds);
//ipAndShopService.unBindShops(username, ipResource.getId(), shopIds);
}
log.error("delete ip resource, {}", id);
//ipResourceRepository.deleteById(id);
}
}
ipResourceRepository.unLockTask(ipResource.getId());
});
}
@Scheduled(cron = "0 0/1 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void queryIpTasks() { public void queryIpTasks() {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL; String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
......
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