Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
browser-backend
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
browser-backend
Commits
79ed9599
Commit
79ed9599
authored
Nov 11, 2020
by
huangjiamin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
失败处理
parent
0964a9e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
14 deletions
+106
-14
IpResourceRepository.java
...owserbackend/browser/repository/IpResourceRepository.java
+2
-0
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+104
-14
No files found.
src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepository.java
View file @
79ed9599
...
@@ -80,4 +80,6 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
...
@@ -80,4 +80,6 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
* @return list
* @return list
*/
*/
List
<
IpResource
>
findByStatusInAndIsDeletedAndIsLocked
(
List
<
Integer
>
status
,
boolean
isDeleted
,
boolean
isLocked
);
List
<
IpResource
>
findByStatusInAndIsDeletedAndIsLocked
(
List
<
Integer
>
status
,
boolean
isDeleted
,
boolean
isLocked
);
List
<
IpResource
>
findByValidTimeLessThanAndIsDeleted
(
long
toEpochMilli
,
boolean
b
);
}
}
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
79ed9599
package
com
.
edgec
.
browserbackend
.
browser
.
task
;
package
com
.
edgec
.
browserbackend
.
browser
.
task
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.edgec.browserbackend.account.domain.Account
;
import
com.edgec.browserbackend.account.domain.Account
;
import
com.edgec.browserbackend.account.domain.IpChargeRequestDto
;
import
com.edgec.browserbackend.account.domain.IpChargeRequestDto
;
...
@@ -10,6 +11,7 @@ import com.edgec.browserbackend.account.service.AccountService;
...
@@ -10,6 +11,7 @@ import com.edgec.browserbackend.account.service.AccountService;
import
com.edgec.browserbackend.browser.domain.IpCountRecord
;
import
com.edgec.browserbackend.browser.domain.IpCountRecord
;
import
com.edgec.browserbackend.browser.domain.IpResource
;
import
com.edgec.browserbackend.browser.domain.IpResource
;
import
com.edgec.browserbackend.browser.domain.IpType
;
import
com.edgec.browserbackend.browser.domain.IpType
;
import
com.edgec.browserbackend.browser.dto.DeleteIpResultDto
;
import
com.edgec.browserbackend.browser.dto.IpBuyResultDto
;
import
com.edgec.browserbackend.browser.dto.IpBuyResultDto
;
import
com.edgec.browserbackend.browser.dto.IpInfoResultDto
;
import
com.edgec.browserbackend.browser.dto.IpInfoResultDto
;
import
com.edgec.browserbackend.browser.dto.ShopRequestDto
;
import
com.edgec.browserbackend.browser.dto.ShopRequestDto
;
...
@@ -26,6 +28,8 @@ import org.slf4j.Logger;
...
@@ -26,6 +28,8 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -107,18 +111,18 @@ public class BrowserTask {
...
@@ -107,18 +111,18 @@ public class BrowserTask {
@Scheduled
(
cron
=
"0 0/1 * * * ?"
)
@Scheduled
(
cron
=
"0 0/1 * * * ?"
)
public
void
buyIpTasks
()
{
public
void
buyIpTasks
()
{
String
logs
=
"【BrowserTask -- buyIpTasks】 "
;
String
logs
=
"【BrowserTask -- buyIpTasks】 "
;
log
.
info
(
logs
+
"****************ip resource purchase start****************"
);
log
.
info
(
"{}, ****************ip resource purchase start****************"
,
logs
);
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
(
logs
+
"ip resource purchase tasks, {}"
,
ipResources
.
size
());
log
.
info
(
"{}, ip resource purchase tasks : {}"
,
logs
,
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
);
log
.
info
(
"{}, ip resource purchase : {}"
,
logs
,
ipResource
.
getId
()
);
try
{
try
{
boolean
result
=
false
;
boolean
result
=
false
;
...
@@ -141,19 +145,18 @@ public class BrowserTask {
...
@@ -141,19 +145,18 @@ 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
(
"{}, ip resource purchase params : {}"
,
logs
,
JSONObject
.
toJSONString
(
map
));
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
);
log
.
info
(
"{}, ip resource purchase result : {}"
,
logs
,
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
(
logs
+
"fail to buy ip, {}"
,
ipBuyResultDto
.
getErrorCode
());
log
.
error
(
"{}, fail to buy ip : {}"
,
logs
,
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
);
log
.
info
(
"{}, ip resource purchase successful : {}"
,
logs
,
ipResource
);
AtomicInteger
index
=
new
AtomicInteger
();
AtomicInteger
index
=
new
AtomicInteger
();
ipBuyResultDto
.
getIplist
().
forEach
(
ipBuyResultDto
.
getIplist
().
forEach
(
x
->
{
x
->
{
...
@@ -173,7 +176,7 @@ public class BrowserTask {
...
@@ -173,7 +176,7 @@ public class BrowserTask {
result
=
true
;
result
=
true
;
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
logs
+
"ip resource purchase Exception {}"
,
e
.
getMessage
(),
e
);
log
.
error
(
"{}, ip resource purchase Exception {}"
,
logs
,
e
.
getMessage
(),
e
);
result
=
false
;
result
=
false
;
}
}
...
@@ -181,10 +184,14 @@ public class BrowserTask {
...
@@ -181,10 +184,14 @@ public class BrowserTask {
* 购买失败处理
* 购买失败处理
*
*
* 购买后,长时间未能成功的ip资源视为购买失败。
* 购买后,长时间未能成功的ip资源视为购买失败。
* 购买malaysia实例失败后不进行重新购买
* 购买时长超过20分钟的ip资源释放,并将购买花销退回账户余额。
* 购买时长超过20分钟的ip资源释放,并将购买花销退回账户余额。
*/
*/
if
(
result
==
false
&&
(
ipResource
.
getPurchasedTime
()
<
Instant
.
now
().
minusSeconds
(
1200
).
toEpochMilli
()))
{
boolean
overtime
=
ipResource
.
getPurchasedTime
()
<
Instant
.
now
().
minusSeconds
(
7200
).
toEpochMilli
();
log
.
info
(
logs
+
"ip resource purchase failure, {}"
,
ipResource
);
/*boolean problemRegion = "malaysia".equals(ipResource.getRegion());
boolean deal = overtime || problemRegion;*/
if
(!
result
&&
overtime
)
{
log
.
info
(
"{}, ip resource purchase failure, {}"
,
logs
,
ipResource
);
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResource
,
3
,
0
);
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResource
,
3
,
0
);
accountService
.
chargeByMoney
(
ipResource
.
getOwner
(),
-
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
)
{
...
@@ -197,11 +204,11 @@ public class BrowserTask {
...
@@ -197,11 +204,11 @@ public class BrowserTask {
}
}
}
finally
{
}
finally
{
long
end
=
System
.
currentTimeMillis
();
long
end
=
System
.
currentTimeMillis
();
log
.
info
(
logs
+
"buy ip task {} execution time is: "
+
(
end
-
start
)
/
1000
+
"s"
,
ipResource
.
getId
()
);
log
.
info
(
"{}, buy ip task {} execution time is: {}s"
,
logs
,
ipResource
.
getId
(),
(
end
-
start
)
/
1000
);
try
{
try
{
ipResourceRepository
.
unLockTask
(
ipResource
.
getId
());
ipResourceRepository
.
unLockTask
(
ipResource
.
getId
());
}
catch
(
Throwable
th
)
{
}
catch
(
Throwable
th
)
{
log
.
error
(
logs
+
"unlock failed"
,
th
);
log
.
error
(
"{}, unlock failed : {}"
,
logs
,
th
.
getMessage
()
,
th
);
//try again
//try again
ipResourceRepository
.
unLockTask
(
ipResource
.
getId
());
ipResourceRepository
.
unLockTask
(
ipResource
.
getId
());
}
}
...
@@ -210,7 +217,6 @@ public class BrowserTask {
...
@@ -210,7 +217,6 @@ public class BrowserTask {
},
},
ThreadPoolUtils
.
buyIpTasksPool
ThreadPoolUtils
.
buyIpTasksPool
);
);
futureList
.
add
(
future
);
futureList
.
add
(
future
);
}
}
CompletableFuture
.
allOf
(
futureList
.
toArray
(
new
CompletableFuture
[
0
])).
join
();
CompletableFuture
.
allOf
(
futureList
.
toArray
(
new
CompletableFuture
[
0
])).
join
();
...
@@ -333,6 +339,76 @@ public class BrowserTask {
...
@@ -333,6 +339,76 @@ public class BrowserTask {
}
}
}
}
private
final
static
String
PROD
=
"prod"
;
/**
* 删除逾期IP
*
* 每天凌晨1点查询 {@code validTime}早于7天前,并且未被删除的ip。
* 调用{@code deleteOverdueIp}删除IP资源
*/
@Scheduled
(
cron
=
"0 0/1 * * * ?"
)
public
void
deleteOverdueIps
()
{
String
logs
=
"【deleteOverdueIps】 "
;
List
<
IpResource
>
list
=
ipResourceRepository
.
findByValidTimeLessThanAndIsDeleted
(
Instant
.
now
().
minusSeconds
(
60
*
60
*
24
*
7
).
toEpochMilli
(),
false
);
log
.
error
(
"Scheduled {}, list size : {}"
,
logs
,
list
.
size
());
//list.forEach(this::deleteOverdueIp);
}
/**
* 删除逾期IP
*
* 调用uri查看是否还存在指定资源
* 如果不存在,则{@code deleted}置为true
*
* @param ipResource ipResource
*/
private
void
deleteOverdueIp
(
IpResource
ipResource
){
String
logs
=
"【deleteOverdueIp】 "
;
String
path
=
"/intelligroup/ipresources?accountId=browser&ip="
;
try
{
if
(
doDelete
(
path
,
ipResource
.
getAddr
()))
{
ipResource
.
setShopIds
(
null
);
ipResource
.
setDeleted
(
true
);
ipResourceRepository
.
save
(
ipResource
);
}
else
{
log
.
error
(
"Scheduled {}, {} delete failed"
,
logs
,
ipResource
.
getAddr
());
}
}
catch
(
Exception
e
)
{
log
.
error
(
"Scheduled {}, Exception : {}"
,
logs
,
e
.
getMessage
(),
e
);
}
}
private
final
static
String
SUCCESS
=
"success"
;
/**
* 封装HttpClientUtils.doDelete方法
*
* @param path path
* @param params params
* @return String
* @throws IOException IOException
*/
private
boolean
doDelete
(
String
path
,
String
params
){
String
logs
=
"【doDelete】 "
;
try
{
String
uri
=
(
PROD
.
equals
(
profiles
)
?
CLOUDAMURL
:
TESTURL
)
+
path
+
params
;
log
.
info
(
"Scheduled {}, {} delete , params : {}"
,
logs
,
params
,
params
);
String
result
=
HttpClientutils
.
doDelete
(
uri
,
buildGetHeader
());
log
.
info
(
"Scheduled {}, {} delete , result : {}"
,
logs
,
params
,
result
);
DeleteIpResultDto
deleteIpResultDto
=
JSON
.
parseObject
(
result
,
DeleteIpResultDto
.
class
);
if
(
SUCCESS
.
equals
(
deleteIpResultDto
.
getResult
()))
{
return
true
;
}
NotifyUtils
.
sendMessage
(
"防关联浏览器 ip "
+
params
+
" 删除失败"
,
NotifyUtils
.
MsgType
.
WEBHOOK
);
log
.
error
(
"Scheduled {}, {} delete failed, ErrorCode : {}, result : {}"
,
logs
,
params
,
deleteIpResultDto
.
getErrorCode
(),
deleteIpResultDto
.
getResult
());
}
catch
(
Exception
e
){
log
.
error
(
"Scheduled {}, Exception : {}"
,
logs
,
e
.
getMessage
(),
e
);
}
return
false
;
}
private
static
QueryIpUrlList
getNextUrl
(
List
<
QueryIpUrlList
>
queryIpUrlLists
,
int
count
)
{
private
static
QueryIpUrlList
getNextUrl
(
List
<
QueryIpUrlList
>
queryIpUrlLists
,
int
count
)
{
if
(
CollectionUtils
.
isEmpty
(
queryIpUrlLists
))
{
if
(
CollectionUtils
.
isEmpty
(
queryIpUrlLists
))
{
...
@@ -384,6 +460,20 @@ public class BrowserTask {
...
@@ -384,6 +460,20 @@ public class BrowserTask {
static
OkHttpClient
client
=
new
OkHttpClient
(
builder
);
static
OkHttpClient
client
=
new
OkHttpClient
(
builder
);
public
static
String
doDelete
(
String
url
,
Map
<
String
,
String
>
headers
)
throws
IOException
{
Headers
httpHeaders
=
Headers
.
of
(
headers
);
Request
request
=
new
Request
.
Builder
()
.
delete
()
.
url
(
url
)
.
headers
(
httpHeaders
)
.
build
();
Call
call
=
client
.
newCall
(
request
);
Response
response
=
call
.
execute
();
ResponseBody
responseBody
=
response
.
body
();
assert
responseBody
!=
null
;
return
responseBody
.
string
();
}
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
headers
)
throws
IOException
{
public
static
String
doGet
(
String
url
,
Map
<
String
,
String
>
headers
)
throws
IOException
{
Headers
httpHeaders
=
Headers
.
of
(
headers
);
Headers
httpHeaders
=
Headers
.
of
(
headers
);
Request
request
=
new
Request
.
Builder
()
Request
request
=
new
Request
.
Builder
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment