Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.Veneno.
wechaty
提交
9842a24f
W
wechaty
项目概览
.Veneno.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9842a24f
编写于
7月 06, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
seprate watchdog code to indenpendent file
上级
018c3c47
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
163 addition
and
94 deletion
+163
-94
src/puppet-web-event.js
src/puppet-web-event.js
+9
-3
src/puppet-web-watchdog.js
src/puppet-web-watchdog.js
+128
-0
src/puppet-web.js
src/puppet-web.js
+12
-80
test/puppet-web-watchdog.spec.js
test/puppet-web-watchdog.spec.js
+12
-10
test/puppet-web.spec.js
test/puppet-web.spec.js
+2
-1
未找到文件。
src/puppet-web-event.js
浏览文件 @
9842a24f
...
...
@@ -68,7 +68,11 @@ function onBrowserDead(e) {
this
.
isBrowserBirthing
=
true
const
TIMEOUT
=
180000
// 180s / 3m
this
.
watchDog
(
`onBrowserDead() set a timeout of
${
Math
.
floor
(
TIMEOUT
/
1000
)}
seconds to prevent unknown state change`
,
{
timeout
:
TIMEOUT
})
// this.watchDog(`onBrowserDead() set a timeout of ${Math.floor(TIMEOUT / 1000)} seconds to prevent unknown state change`, {timeout: TIMEOUT})
this
.
emit
(
'
watchdog
'
,
{
data
:
`onBrowserDead() set a timeout of
${
Math
.
floor
(
TIMEOUT
/
1000
)}
seconds to prevent unknown state change`
,
timeout
:
TIMEOUT
})
log
.
verbose
(
'
PuppetWebEvent
'
,
'
onBrowserDead(%s)
'
,
e
.
message
||
e
)
if
(
!
this
.
browser
||
!
this
.
bridge
)
{
...
...
@@ -112,7 +116,8 @@ function onBrowserDead(e) {
function
onServerDing
(
data
)
{
log
.
silly
(
'
PuppetWebEvent
'
,
'
onServerDing(%s)
'
,
data
)
this
.
watchDog
(
data
)
// this.watchDog(data)
this
.
emit
(
'
watchdog
'
,
{
data
})
}
function
onServerScan
(
data
)
{
...
...
@@ -131,7 +136,8 @@ function onServerScan(data) {
}
// feed watchDog a `scan` type of food
this
.
watchDog
(
data
,
{
type
:
'
scan
'
})
// this.watchDog(data, {type: 'scan'})
this
.
emit
(
'
watchdog
'
,
{
data
,
type
:
'
SCAN
'
})
this
.
emit
(
'
scan
'
,
data
)
}
...
...
src/puppet-web-watchdog.js
0 → 100644
浏览文件 @
9842a24f
/**
*
* wechaty: Wechat for Bot. and for human who talk to bot/robot
*
* Class PuppetWeb Watchdog
*
* monitor puppet
*
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
/**************************************
*
* Class PuppetWeb
*
***************************************/
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
Event
=
require
(
'
./puppet-web-event
'
)
const
Watchdog
=
{
onFeed
}
// feed me in time(after 1st feed), or I'll restart system
function
onFeed
({
data
,
type
=
'
GARBAGE
'
,
timeout
=
60000
// 60s default. can be override in options but be careful about the number zero(0)
}
=
{})
{
log
.
verbose
(
'
PuppetWebWatchdog
'
,
'
onFeed: %s, %d, [%s]
'
,
type
,
timeout
,
data
)
switch
(
type
)
{
case
'
POISON
'
:
clearWatchDogTimer
.
call
(
this
)
return
case
'
GARBAGE
'
:
case
'
SCAN
'
:
break
default
:
throw
new
Error
(
'
unsupport type:
'
+
type
)
}
setWatchDogTimer
.
call
(
this
,
timeout
)
this
.
emit
(
'
heartbeat
'
,
data
+
'
@
'
+
type
)
monitorScan
.
call
(
this
,
type
)
autoSaveSession
.
call
(
this
)
}
function
clearWatchDogTimer
()
{
if
(
this
.
watchDogTimer
)
{
clearTimeout
(
this
.
watchDogTimer
)
this
.
watchDogTimer
=
null
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
clearWatchDogTimer() cleared
'
)
}
else
{
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
clearWatchDogTimer() nothing to clear
'
)
}
}
function
setWatchDogTimer
(
timeout
)
{
clearWatchDogTimer
.
call
(
this
)
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
setWatchDogTimer(%d)
'
,
timeout
)
this
.
watchDogTimer
=
setTimeout
(
watchDogReset
.
bind
(
this
,
timeout
),
timeout
)
// block quit, force to use quit() // this.watchDogTimer.unref() // dont block quit
}
function
watchDogReset
(
timeout
)
{
log
.
verbose
(
'
PuppetWebWatchdog
'
,
'
watchDogReset() timeout %d
'
,
timeout
)
const
e
=
new
Error
(
'
watchdog reset after
'
+
Math
.
floor
(
timeout
/
1000
)
+
'
seconds
'
)
this
.
emit
(
'
error
'
,
e
)
return
Event
.
onBrowserDead
.
call
(
this
,
e
)
}
/**
*
* Deal with Sessions(cookies)
* save every 5 mins
*
*/
function
autoSaveSession
()
{
const
SAVE_SESSION_INTERVAL
=
5
*
60
*
1000
// 5 mins
if
(
Date
.
now
()
-
this
.
watchDogLastSaveSession
>
SAVE_SESSION_INTERVAL
)
{
log
.
verbose
(
'
PuppetWebWatchdog
'
,
'
watchDog() saveSession(%s) after %d minutes
'
,
this
.
profile
,
Math
.
floor
(
SAVE_SESSION_INTERVAL
/
1000
/
60
))
this
.
browser
.
saveSession
()
this
.
watchDogLastSaveSession
=
Date
.
now
()
}
}
/**
*
* Deal with SCAN events
*
* if web browser stay at login qrcode page long time,
* sometimes the qrcode will not refresh, leave there expired.
* so we need to refresh the page after a while
*
*/
function
monitorScan
(
type
)
{
log
.
verbose
(
'
PuppetWebWatchdog
'
,
'
monitorScan(%s)
'
,
type
)
const
scanTimeout
=
10
*
60
*
1000
// 10 mins
if
(
type
===
'
SCAN
'
)
{
// watchDog was feed a 'scan' data
this
.
lastScanEventTime
=
Date
.
now
()
}
if
(
this
.
logined
())
{
// XXX: login status right?
this
.
lastScanEventTime
=
null
}
else
if
(
this
.
lastScanEventTime
&&
Date
.
now
()
-
this
.
lastScanEventTime
>
scanTimeout
)
{
log
.
warn
(
'
PuppetWebWatchdog
'
,
'
monirotScan() refresh browser for no food of type scan after %s mins
'
,
Math
.
floor
(
scanTimeout
/
1000
/
60
))
// try to fix the problem
this
.
browser
.
refresh
()
this
.
lastScanEventTime
=
Date
.
now
()
}
}
module
.
exports
=
Watchdog
\ No newline at end of file
src/puppet-web.js
浏览文件 @
9842a24f
...
...
@@ -33,6 +33,7 @@ const Browser = require('./puppet-web-browser')
const
Bridge
=
require
(
'
./puppet-web-bridge
'
)
const
Event
=
require
(
'
./puppet-web-event
'
)
const
Watchdog
=
require
(
'
./puppet-web-watchdog
'
)
class
PuppetWeb
extends
Puppet
{
constructor
({
...
...
@@ -54,6 +55,8 @@ class PuppetWeb extends Puppet {
init
()
{
log
.
verbose
(
'
PuppetWeb
'
,
`init() with port:
${
this
.
port
}
, head:
${
this
.
head
}
, profile:
${
this
.
profile
}
`
)
this
.
on
(
'
watchdog
'
,
Watchdog
.
onFeed
.
bind
(
this
))
return
co
.
call
(
this
,
function
*
()
{
yield
this
.
initAttach
(
this
)
...
...
@@ -68,7 +71,8 @@ class PuppetWeb extends Puppet {
this
.
bridge
=
yield
this
.
initBridge
()
log
.
verbose
(
'
PuppetWeb
'
,
'
initBridge() done
'
)
this
.
watchDog
(
'
inited
'
)
// start watchdog
// this.watchDog('inited') // start watchdog
this
.
emit
(
'
watchdog
'
,
{
data
:
'
inited
'
})
})
.
catch
(
e
=>
{
// Reject
log
.
error
(
'
PuppetWeb
'
,
'
init exception: %s
'
,
e
.
message
)
...
...
@@ -83,8 +87,13 @@ class PuppetWeb extends Puppet {
quit
()
{
log
.
verbose
(
'
PuppetWeb
'
,
'
quit()
'
)
// this.clearWatchDogTimer()
this
.
emit
(
'
watchdog
'
,
{
data
:
'
PuppetWeb.quit()
'
,
type
:
'
POISON
'
})
return
co
.
call
(
this
,
function
*
()
{
this
.
clearWatchDogTimer
()
if
(
this
.
bridge
)
{
yield
this
.
bridge
.
quit
().
catch
(
e
=>
{
// fail safe
...
...
@@ -194,83 +203,6 @@ class PuppetWeb extends Puppet {
})
}
clearWatchDogTimer
()
{
if
(
this
.
watchDogTimer
)
{
clearTimeout
(
this
.
watchDogTimer
)
this
.
watchDogTimer
=
null
}
}
setWatchDogTimer
(
timeout
)
{
this
.
clearWatchDogTimer
()
this
.
watchDogTimer
=
setTimeout
(
this
.
watchDogReset
.
bind
(
this
,
timeout
),
timeout
)
this
.
watchDogTimer
.
unref
()
// dont block quit
}
// feed me in time(after 1st feed), or I'll restart system
watchDog
(
data
,
{
timeout
,
type
}
=
{})
{
log
.
silly
(
'
PuppetWeb
'
,
'
watchDog(%s)
'
,
data
)
timeout
=
timeout
||
60000
// 60s default. can be override in options but be careful about the number zero(0)
type
=
type
||
'
food
'
// just a name
this
.
setWatchDogTimer
(
timeout
)
this
.
emit
(
'
heartbeat
'
,
data
)
/**
*
* Deal with Sessions(cookies)
*
* save every 5 mins
*
*/
const
SAVE_SESSION_INTERVAL
=
5
*
60
*
1000
// 5 mins
// if no lastSaveSession set(means 1st time), or timeout
if
(
!
this
.
watchDogLastSaveSession
)
{
this
.
watchDogLastSaveSession
=
Date
.
now
()
}
else
if
(
Date
.
now
()
-
this
.
watchDogLastSaveSession
>
SAVE_SESSION_INTERVAL
)
{
log
.
verbose
(
'
PuppetWeb
'
,
'
watchDog() saveSession(%s) after %d minutes
'
,
this
.
profile
,
Math
.
floor
(
SAVE_SESSION_INTERVAL
/
1000
/
60
))
this
.
browser
.
saveSession
()
this
.
watchDogLastSaveSession
=
Date
.
now
()
}
/**
*
* Deal with SCAN events
*
* if web browser stay at login qrcode page long time,
* sometimes the qrcode will not refresh, leave there expired.
* so we need to refresh the page after a while
*
*/
if
(
type
===
'
scan
'
)
{
// watchDog was feed a 'scan' data
log
.
verbose
(
'
PuppetWeb
'
,
'
watchDog() got a food with type scan
'
)
this
.
lastScanEventTime
=
Date
.
now
()
}
if
(
this
.
logined
())
{
// XXX: login status right?
this
.
lastScanEventTime
=
null
}
else
if
(
this
.
lastScanEventTime
)
{
const
scanTimeout
=
10
*
60
*
1000
// 10 mins
if
(
Date
.
now
()
-
this
.
lastScanEventTime
>
scanTimeout
)
{
log
.
warn
(
'
PuppetWeb
'
,
'
watchDog() refresh browser for no food of type scan after %s mins
'
,
Math
.
floor
(
scanTimeout
/
1000
/
60
))
// try to fix the problem
this
.
browser
.
refresh
()
}
}
}
watchDogReset
(
timeout
)
{
log
.
verbose
(
'
PuppetWeb
'
,
'
watchDogReset() timeout %d
'
,
timeout
)
const
e
=
new
Error
(
'
watchdog reset after
'
+
Math
.
floor
(
timeout
/
1000
)
+
'
seconds
'
)
this
.
emit
(
'
error
'
,
e
)
return
Event
.
onBrowserDead
.
call
(
this
,
e
)
}
self
(
message
)
{
if
(
!
this
.
userId
)
{
...
...
test/puppet-web-watchdog.spec.js
浏览文件 @
9842a24f
...
...
@@ -16,25 +16,27 @@ test('Puppet Web watchdog timer', function(t) {
t
.
ok
(
pw
,
'
should instantiate a PuppetWeb
'
)
co
(
function
*
()
{
// pw.browser = yield pw.initBrowser()
// t.ok(pw.browser, 'should init the browser')
// pw.bridge = yield pw.initBridge()
// t.ok(pw.bridge, 'should init the bridge')
// yield pw.bridge.quit().catch(e => {/* fail safe */})
// yield pw.browser.quit().catch(e => {/* fail safe */})
// t.pass('should kill both browser & bridge')
yield
pw
.
init
()
pw
.
quit
()
// yield pw.bridge.quit()
// pw.bridge = null
// yield pw.browser.quit()
// pw.browser = null
let
errorCounter
=
0
pw
.
once
(
'
error
'
,
e
=>
errorCounter
=
1
)
pw
.
watchDog
(
'
feed_and_active_it
'
,
{
timeout
:
1
})
yield
new
Promise
((
resolve
)
=>
setTimeout
(()
=>
resolve
(),
2
))
// wait untill reset
pw
.
emit
(
'
watchdog
'
,
{
data
:
'
feed_and_active_it
'
,
timeout
:
1
})
yield
new
Promise
((
resolve
)
=>
setTimeout
(
_
=>
resolve
(),
10
))
// wait untill reset
t
.
equal
(
errorCounter
,
1
,
'
should get event[error] after watchdog timeout
'
)
pw
.
once
(
'
error
'
,
e
=>
t
.
fail
(
'
waitDing() triggered watchDogReset()
'
))
const
EXPECTED_DING_DATA
=
'
dingdong
'
pw
.
watchDog
(
'
feed to extend the dog life
'
)
pw
.
emit
(
'
watchdog
'
,
{
data
:
'
feed to extend the dog life
'
}
)
const
origLogLevel
=
log
.
level
log
.
level
=
'
silly
'
...
...
test/puppet-web.spec.js
浏览文件 @
9842a24f
...
...
@@ -12,7 +12,7 @@ const PROFILE = 'unit-test-session.wechaty.json'
const
PuppetWeb
=
require
(
'
../src/puppet-web
'
)
const
Message
=
require
(
'
../src/message
'
)
test
(
'
PuppetWeb smoke testing
'
,
function
(
t
)
{
false
&&
test
(
'
PuppetWeb smoke testing
'
,
function
(
t
)
{
let
pw
=
new
PuppetWeb
({
port
:
PORT
,
head
:
HEAD
,
profile
:
PROFILE
})
t
.
ok
(
pw
,
'
should instantiated a PuppetWeb
'
)
...
...
@@ -91,6 +91,7 @@ log.level = 'info'
setTimeout
(
_
=>
{
reject
(
'
no response timeout after
'
+
2
*
maxTime
)
},
2
*
maxTime
)
.
unref
()
return
testDing
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录