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
b66ed401
Commit
b66ed401
authored
May 12, 2020
by
renjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
健康检查
parent
e9069583
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
79 additions
and
23 deletions
+79
-23
IpResource.java
...a/com/edgec/browserbackend/browser/domain/IpResource.java
+8
-0
IpResourceRepositoryCustom.java
...ackend/browser/repository/IpResourceRepositoryCustom.java
+4
-0
IpResourceRepositoryCustomImpl.java
...nd/browser/repository/IpResourceRepositoryCustomImpl.java
+37
-4
IpResourceServiceImpl.java
...erbackend/browser/service/Impl/IpResourceServiceImpl.java
+4
-1
ShopServiceImpl.java
.../browserbackend/browser/service/Impl/ShopServiceImpl.java
+0
-2
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+16
-9
ThreadPoolUtils.java
...om/edgec/browserbackend/common/utils/ThreadPoolUtils.java
+3
-3
Trans.java
...ain/java/com/edgec/browserbackend/common/utils/Trans.java
+7
-4
No files found.
src/main/java/com/edgec/browserbackend/browser/domain/IpResource.java
View file @
b66ed401
...
...
@@ -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
;
}
}
src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepositoryCustom.java
View file @
b66ed401
...
...
@@ -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
);
...
...
src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepositoryCustomImpl.java
View file @
b66ed401
...
...
@@ -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
);
...
...
src/main/java/com/edgec/browserbackend/browser/service/Impl/IpResourceServiceImpl.java
View file @
b66ed401
...
...
@@ -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
);
}
...
...
src/main/java/com/edgec/browserbackend/browser/service/Impl/ShopServiceImpl.java
View file @
b66ed401
...
...
@@ -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
)
...
...
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
b66ed401
...
...
@@ -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
.
lockTas
k
(
ipResource
))
{
if
(
ipResourceRepository
.
healthLoc
k
(
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
.
unLock
Task
(
ipResource
.
getId
());
ipResourceRepository
.
unLock
Health
(
ipResource
.
getId
());
}
catch
(
Throwable
th
)
{
log
.
error
(
"unlock failed"
,
th
);
//try again
ipResourceRepository
.
unLock
Task
(
ipResource
.
getId
());
ipResourceRepository
.
unLock
Health
(
ipResource
.
getId
());
}
}
}
},
ThreadPoolUtils
.
queryIp
TasksPool
);
},
ThreadPoolUtils
.
queryIp
Health
);
}
}
...
...
src/main/java/com/edgec/browserbackend/common/utils/ThreadPoolUtils.java
View file @
b66ed401
...
...
@@ -38,14 +38,14 @@ public abstract class ThreadPoolUtils {
});
private
static
final
int
IPTRANSACTION_POOL_COUNT
=
1
0
;
private
static
final
int
IPTRANSACTION_POOL_COUNT
=
2
0
;
public
static
final
ScheduledExecutorService
queryIpTransactionPool
=
Executors
.
newSchedul
edThreadPool
(
IPTRANSACTION_POOL_COUNT
,
new
ThreadFactory
()
{
public
static
final
ExecutorService
queryIpHealth
=
Executors
.
newFix
edThreadPool
(
IPTRANSACTION_POOL_COUNT
,
new
ThreadFactory
()
{
int
count
=
1
;
@Override
public
Thread
newThread
(
Runnable
runnable
)
{
return
new
Thread
(
runnable
,
"browser-ip
Transaction
-task-"
+
count
++);
return
new
Thread
(
runnable
,
"browser-ip
HealthCheck
-task-"
+
count
++);
}
});
}
src/main/java/com/edgec/browserbackend/common/utils/Trans.java
View file @
b66ed401
...
...
@@ -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
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