Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
41fdab39
W
wechaty
项目概览
.www.
/
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,发现更多精彩内容 >>
提交
41fdab39
编写于
10月 09, 2017
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
switch from Watchrat to NPM Watchdog
上级
3a9f20b6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
27 addition
and
211 deletion
+27
-211
package.json
package.json
+1
-0
src/puppet-web/event.ts
src/puppet-web/event.ts
+6
-5
src/puppet-web/puppet-web.ts
src/puppet-web/puppet-web.ts
+15
-14
src/puppet.ts
src/puppet.ts
+5
-5
src/watchrat.spec.ts
src/watchrat.spec.ts
+0
-72
src/watchrat.ts
src/watchrat.ts
+0
-115
未找到文件。
package.json
浏览文件 @
41fdab39
...
...
@@ -101,6 +101,7 @@
"
retry-promise
"
:
"
^1.0.0
"
,
"
rxjs
"
:
"
^5.4.3
"
,
"
state-switch
"
:
"
^0.1.13
"
,
"
watchdog
"
:
"
^0.1.3
"
,
"
ws
"
:
"
^3.2.0
"
,
"
xml2js
"
:
"
^0.4.19
"
},
...
...
src/puppet-web/event.ts
浏览文件 @
41fdab39
...
...
@@ -16,6 +16,10 @@
* limitations under the License.
*
*/
import
{
WatchdogFood
,
}
from
'
watchdog
'
import
{
log
,
}
from
'
../config
'
...
...
@@ -27,9 +31,6 @@ import {
import
{
ScanInfo
,
}
from
'
../puppet
'
import
{
WatchratFood
,
}
from
'
../watchrat
'
import
Firer
from
'
./firer
'
import
PuppetWeb
from
'
./puppet-web
'
...
...
@@ -80,10 +81,10 @@ async function onScan(this: PuppetWeb, data: ScanInfo): Promise<void> {
}
// feed watchDog a `scan` type of food
const
food
=
{
const
food
:
WatchdogFood
=
{
data
,
type
:
'
scan
'
,
}
as
WatchratFood
}
this
.
emit
(
'
watchdog
'
,
food
)
this
.
emit
(
'
scan
'
,
data
.
url
,
data
.
code
)
}
...
...
src/puppet-web/puppet-web.ts
浏览文件 @
41fdab39
...
...
@@ -16,6 +16,11 @@
* limitations under the License.
*
*/
import
{
Watchdog
,
WatchdogFood
,
}
from
'
watchdog
'
import
{
config
,
log
,
...
...
@@ -35,10 +40,6 @@ import {
import
Room
from
'
../room
'
import
RxQueue
from
'
../rx-queue
'
import
Misc
from
'
../misc
'
import
{
Watchrat
,
WatchratFood
,
}
from
'
../watchrat
'
import
{
Bridge
,
...
...
@@ -62,8 +63,8 @@ export class PuppetWeb extends Puppet {
public
bridge
:
Bridge
public
scanInfo
:
ScanInfo
|
null
public
puppetWatch
rat
:
Watchrat
<
PuppetFoodType
>
public
scanWatch
rat
:
Watchrat
<
ScanFoodType
>
public
puppetWatch
dog
:
Watchdog
<
PuppetFoodType
>
public
scanWatch
dog
:
Watchdog
<
ScanFoodType
>
private
fileId
:
number
...
...
@@ -74,10 +75,10 @@ export class PuppetWeb extends Puppet {
this
.
fileId
=
0
const
PUPPET_TIMEOUT
=
60
*
1000
// 1 minute
this
.
puppetWatch
rat
=
new
Watchrat
<
PuppetFoodType
>
(
'
PuppetWeb
'
,
PUPPET_TIMEOUT
)
this
.
puppetWatch
dog
=
new
Watchdog
<
PuppetFoodType
>
(
PUPPET_TIMEOUT
,
'
PuppetWeb
'
)
const
SCAN_TIMEOUT
=
4
*
60
*
1000
// 4 minutes
this
.
scanWatch
rat
=
new
Watchrat
<
ScanFoodType
>
(
'
Scan
'
,
SCAN_TIMEOUT
)
this
.
scanWatch
dog
=
new
Watchdog
<
ScanFoodType
>
(
SCAN_TIMEOUT
,
'
Scan
'
)
}
public
toString
()
{
return
`Class PuppetWeb({profile:
${
this
.
options
.
profile
}
})`
}
...
...
@@ -106,7 +107,7 @@ export class PuppetWeb extends Puppet {
*/
this
.
state
.
current
(
'
live
'
)
const
food
:
Watch
rat
Food
=
{
const
food
:
Watch
dog
Food
=
{
data
:
'
inited
'
,
timeout
:
2
*
60
*
1000
,
// 2 mins for first login
}
...
...
@@ -136,9 +137,9 @@ export class PuppetWeb extends Puppet {
public
initWatchdogForPuppet
():
void
{
const
puppet
=
this
const
dog
=
this
.
puppetWatch
rat
const
dog
=
this
.
puppetWatch
dog
puppet
.
on
(
'
ding
'
,
data
=>
this
.
puppetWatch
rat
.
feed
({
puppet
.
on
(
'
ding
'
,
data
=>
this
.
puppetWatch
dog
.
feed
({
data
,
type
:
'
ding
'
,
}))
...
...
@@ -163,7 +164,7 @@ export class PuppetWeb extends Puppet {
*/
public
initWatchdogForScan
():
void
{
const
puppet
=
this
const
dog
=
this
.
scanWatch
rat
const
dog
=
this
.
scanWatch
dog
puppet
.
on
(
'
scan
'
,
info
=>
dog
.
feed
({
data
:
info
,
...
...
@@ -189,7 +190,7 @@ export class PuppetWeb extends Puppet {
try
{
await
this
.
bridge
.
reload
()
}
catch
(
e
)
{
log
.
error
(
'
PuppetWeb
'
,
'
initScanWatch
rat
() on(reset) exception: %s
'
,
e
)
log
.
error
(
'
PuppetWeb
'
,
'
initScanWatch
dog
() on(reset) exception: %s
'
,
e
)
this
.
emit
(
'
error
'
,
e
)
}
})
...
...
@@ -214,7 +215,7 @@ export class PuppetWeb extends Puppet {
}
log
.
verbose
(
'
PuppetWeb
'
,
'
quit() kill watchdog before do quit
'
)
this
.
puppetWatch
rat
.
sleep
()
this
.
puppetWatch
dog
.
sleep
()
this
.
state
.
target
(
'
dead
'
)
this
.
state
.
current
(
'
dead
'
,
false
)
...
...
src/puppet.ts
浏览文件 @
41fdab39
...
...
@@ -19,6 +19,9 @@
import
{
EventEmitter
}
from
'
events
'
import
{
StateSwitch
}
from
'
state-switch
'
import
{
WatchdogFood
,
}
from
'
watchdog
'
import
{
Sayable
,
...
...
@@ -32,9 +35,6 @@ import {
}
from
'
./message
'
import
Profile
from
'
./profile
'
import
Room
from
'
./room
'
import
{
WatchratFood
,
}
from
'
./watchrat
'
import
{
WechatyEvent
,
}
from
'
./wechaty
'
...
...
@@ -76,7 +76,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public
emit
(
event
:
'
room-leave
'
,
room
:
Room
,
leaverList
:
Contact
[])
:
boolean
public
emit
(
event
:
'
room-topic
'
,
room
:
Room
,
topic
:
string
,
oldTopic
:
string
,
changer
:
Contact
)
:
boolean
public
emit
(
event
:
'
scan
'
,
url
:
string
,
code
:
number
)
:
boolean
public
emit
(
event
:
'
watchdog
'
,
food
:
Watch
rat
Food
)
:
boolean
public
emit
(
event
:
'
watchdog
'
,
food
:
Watch
dog
Food
)
:
boolean
public
emit
(
event
:
never
,
...
args
:
any
[])
:
boolean
public
emit
(
...
...
@@ -97,7 +97,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public
on
(
event
:
'
room-leave
'
,
listener
:
(
room
:
Room
,
leaverList
:
Contact
[])
=>
void
)
:
this
public
on
(
event
:
'
room-topic
'
,
listener
:
(
room
:
Room
,
topic
:
string
,
oldTopic
:
string
,
changer
:
Contact
)
=>
void
)
:
this
public
on
(
event
:
'
scan
'
,
listener
:
(
info
:
ScanInfo
)
=>
void
)
:
this
public
on
(
event
:
'
watchdog
'
,
listener
:
(
data
:
Watch
rat
Food
)
=>
void
)
:
this
public
on
(
event
:
'
watchdog
'
,
listener
:
(
data
:
Watch
dog
Food
)
=>
void
)
:
this
public
on
(
event
:
never
,
listener
:
any
)
:
this
public
on
(
...
...
src/watchrat.spec.ts
已删除
100755 → 0
浏览文件 @
3a9f20b6
#!/usr/bin/env ts-node
// tslint:disable:no-shadowed-variable
import
*
as
test
from
'
blue-tape
'
import
*
as
sinon
from
'
sinon
'
const
sinonTest
=
require
(
'
sinon-test
'
)(
sinon
)
// import { log } from './config'
// log.level('silly')
import
{
Watchrat
,
WatchratFood
,
}
from
'
./watchrat
'
test
(
'
starve to death
'
,
sinonTest
(
async
function
(
t
)
{
const
TIMEOUT
=
1
*
1000
const
EXPECTED_FOOD
=
{
data
:
'
dummy
'
,
timeout
:
TIMEOUT
,
}
as
WatchratFood
const
watchrat
=
new
Watchrat
(
'
TestWatchrat
'
,
TIMEOUT
)
watchrat
.
on
(
'
reset
'
,
(
food
,
left
)
=>
{
t
.
equal
(
left
,
0
,
'
timeLeft should equal to 0 when reset
'
)
t
.
deepEqual
(
food
,
EXPECTED_FOOD
,
'
should get food back when reset
'
)
})
watchrat
.
feed
(
EXPECTED_FOOD
)
this
.
clock
.
tick
(
TIMEOUT
+
1
)
t
.
end
()
}))
test
(
'
feed in the middle
'
,
sinonTest
(
async
function
(
t
)
{
// console.log('this', this)
const
TIMEOUT
=
1
*
1000
const
FEED_TIME
=
0.3
*
1000
const
watchrat
=
new
Watchrat
(
'
TestWatchrat
'
,
TIMEOUT
)
watchrat
.
on
(
'
reset
'
,
()
=>
{
t
.
fail
(
'
should not be reset
'
)
})
watchrat
.
feed
({
data
:
'
dummy
'
})
this
.
clock
.
tick
(
FEED_TIME
)
const
left
=
watchrat
.
feed
({
data
:
'
dummy
'
})
t
.
equal
(
left
,
TIMEOUT
-
FEED_TIME
,
'
should get the time left dependes on the FEED_TIME
'
)
t
.
end
()
}))
test
(
'
sleep()
'
,
sinonTest
(
async
function
(
t
)
{
const
TIMEOUT
=
1
*
1000
const
FEED_TIME
=
0.3
*
1000
const
watchrat
=
new
Watchrat
(
'
TestWatchrat
'
,
TIMEOUT
)
watchrat
.
on
(
'
reset
'
,
()
=>
{
t
.
fail
(
'
should not be reset
'
)
})
watchrat
.
feed
({
data
:
'
dummy
'
})
this
.
clock
.
tick
(
FEED_TIME
)
watchrat
.
sleep
()
this
.
clock
.
tick
(
TIMEOUT
*
2
)
const
left
=
watchrat
.
left
()
t
.
ok
(
left
<
0
,
'
time should already passed by...
'
)
t
.
end
()
}))
src/watchrat.ts
已删除
100644 → 0
浏览文件 @
3a9f20b6
import
{
EventEmitter
}
from
'
events
'
import
{
log
}
from
'
./config
'
export
type
WatchratEvent
=
'
feed
'
|
'
reset
'
|
'
sleep
'
export
type
WatchratListener
=
(
food
:
WatchratFood
,
left
:
number
)
=>
void
export
interface
WatchratFood
<
T
=
any
>
{
data
:
any
,
timeout
?
:
number
,
// millisecond
type
?
:
T
}
export
class
Watchrat
<
T
=
any
>
extends
EventEmitter
{
private
timer
:
NodeJS
.
Timer
|
undefined
|
null
// `undefined` stands for the first time init. `null` will be set by `stopTimer`
private
lastFeed
:
number
private
lastFood
:
WatchratFood
<
T
>
constructor
(
public
name
=
'
Meow
'
,
public
defaultTimeout
=
60
*
1000
,
)
{
super
()
log
.
verbose
(
'
Watchrat
'
,
'
%s: constructor(name=%s, defaultTimeout=%d)
'
,
name
,
name
,
defaultTimeout
)
}
public
on
(
event
:
'
feed
'
,
listener
:
WatchratListener
)
:
this
public
on
(
event
:
'
reset
'
,
listener
:
WatchratListener
)
:
this
public
on
(
event
:
'
sleep
'
,
listener
:
WatchratListener
)
:
this
public
on
(
event
:
never
,
listener
:
never
)
:
never
public
on
(
event
:
WatchratEvent
,
listener
:
WatchratListener
):
this
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: on(%s)
'
,
this
.
name
,
event
)
super
.
on
(
event
,
listener
)
return
this
}
private
startTimer
(
timeout
:
number
):
void
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: startTimer()
'
,
this
.
name
)
if
(
this
.
timer
)
{
throw
new
Error
(
'
timer already exist!
'
)
}
this
.
timer
=
setTimeout
(()
=>
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: startTimer() setTimeout() after %d
'
,
this
.
name
,
this
.
defaultTimeout
)
this
.
emit
(
'
reset
'
,
this
.
lastFood
,
0
)
},
timeout
)
this
.
timer
.
unref
()
// should not block node quit
return
}
private
stopTimer
(
sleep
=
false
):
void
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: stopTimer()
'
,
this
.
name
)
if
(
typeof
this
.
timer
===
'
undefined
'
)
{
// first time
log
.
verbose
(
'
Watchrat
'
,
'
%s: stopTimer() first run(or after sleep)
'
,
this
.
name
)
return
}
if
(
this
.
timer
)
{
clearTimeout
(
this
.
timer
)
this
.
timer
=
null
}
else
if
(
!
sleep
)
{
throw
new
Error
(
'
timer is already stoped!
'
)
}
}
public
left
():
number
{
let
left
if
(
Number
.
isInteger
(
this
.
lastFeed
))
{
// console.log('lastFeed=', this.lastFeed)
// console.log('timeout=', this.lastFood.timeout)
// console.log('Date.now()=', Date.now())
left
=
this
.
lastFeed
+
this
.
defaultTimeout
-
Date
.
now
()
log
.
verbose
(
'
Watchrat
'
,
'
%s: timerLeft() = %d
'
,
this
.
name
,
left
)
}
else
{
left
=
0
log
.
verbose
(
'
Watchrat
'
,
'
%s: timerLeft() first feed, left=%s
'
,
this
.
name
,
left
)
}
return
left
}
public
feed
(
food
:
WatchratFood
<
T
>
):
number
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: feed(%s)
'
,
this
.
name
,
food
)
if
(
!
food
.
timeout
)
{
food
.
timeout
=
this
.
defaultTimeout
}
const
left
=
this
.
left
()
this
.
stopTimer
()
this
.
startTimer
(
food
.
timeout
)
this
.
lastFeed
=
Date
.
now
()
this
.
lastFood
=
food
this
.
emit
(
'
feed
'
,
food
,
left
)
return
left
}
public
sleep
():
void
{
log
.
verbose
(
'
Watchrat
'
,
'
%s: sleep()
'
,
this
.
name
)
this
.
stopTimer
(
true
)
this
.
timer
=
undefined
}
}
export
default
Watchrat
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录