-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.go
152 lines (128 loc) · 3.58 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// MIT License
// Copyright (c) 2023 wetrycode
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package tegenaria
import (
"runtime"
)
// EventType 事件类型
type EventType int
const (
// START 启动
START EventType = iota
// HEARTBEAT 心跳
HEARTBEAT
// PAUSE 暂停
PAUSE
// ERROR 错误
ERROR
// EXIT 退出
EXIT
)
// Hook 事件处理函数类型
type Hook func(params ...interface{}) error
// EventHooksInterface 事件处理函数接口
type EventHooksInterface interface {
// Start 处理引擎启动事件
Start(params ...interface{}) error
// Stop 处理引擎停止事件
Pause(params ...interface{}) error
// Error处理错误事件
Error(params ...interface{}) error
// Exit 退出引擎事件
Exit(params ...interface{}) error
// Heartbeat 心跳检查事件
Heartbeat(params ...interface{}) error
// EventsWatcher 事件监听器
EventsWatcher(ch chan EventType) error
SetCurrentSpider(spider SpiderInterface)
}
type DefaultHooks struct {
spider SpiderInterface
}
// NewDefaultHooks 构建新的默认事件监听器
func NewDefaultHooks() *DefaultHooks {
return &DefaultHooks{}
}
// Start 处理START事件
func (d *DefaultHooks) Start(params ...interface{}) error {
return nil
}
// Pause 处理STOP事件
func (d *DefaultHooks) Pause(params ...interface{}) error {
return nil
}
// Error 处理ERROR事件
func (d *DefaultHooks) Error(params ...interface{}) error {
return nil
}
// Exit 处理EXIT事件
func (d *DefaultHooks) Exit(params ...interface{}) error {
return nil
}
// Heartbeat 处理HEARTBEAT事件
func (d *DefaultHooks) Heartbeat(params ...interface{}) error {
return nil
}
// DefaultWatcher 默认的事件监听器
// ch 用于接收事件
// hooker 事件处理实例化接口,比如DefaultHooks
func DefaultWatcher(ch chan EventType, hooker EventHooksInterface) error {
for {
select {
case event := <-ch:
switch event {
case START:
err := hooker.Start()
if err != nil {
return err
}
case PAUSE:
err := hooker.Pause()
if err != nil {
return err
}
case ERROR:
err := hooker.Error()
if err != nil {
return nil
}
case HEARTBEAT:
err := hooker.Heartbeat()
if err != nil {
return err
}
case EXIT:
err := hooker.Exit()
if err != nil {
return err
}
return nil
default:
}
default:
}
runtime.Gosched()
}
}
// EventsWatcher DefualtHooks 的事件监听器
func (d *DefaultHooks) EventsWatcher(ch chan EventType) error {
return DefaultWatcher(ch, d)
}
func (d *DefaultHooks) SetCurrentSpider(spider SpiderInterface) {
d.spider = spider
}