-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
72 lines (57 loc) · 1.21 KB
/
task.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
// Copyright 2015 Liu Dong <ddliuhb@gmail.com>.
// Licensed under the MIT license.
package spider
type Status int
const (
PENDING Status = iota
WORKING
FAILED
IGNORED
DONE
)
func NewTask(uri string) *Task {
task := &Task {
Uri: uri,
Status: PENDING,
Data: make(Data),
}
return task
}
type Task struct {
Uri string
Status Status
Depth uint
Data Data
Spider *Spider
Parent *Task
}
func (this *Task) IsEnded() bool {
return this.Status == FAILED || this.Status == IGNORED || this.Status == DONE
}
func (this *Task) Fail(reason interface{}) {
this.Spider.FailTask(this, reason)
}
func (this *Task) Ignore(reason interface{}) {
this.Spider.IgnoreTask(this, reason)
}
func (this *Task) Done() {
this.Spider.DoneTask(this)
}
func (this *Task) Start() {
this.Spider.StartTask(this)
}
// Create a new task from it
func (this *Task) Fork(uri string, data Data) {
task := NewTask(uri)
if data != nil {
task.Data = data
}
task.Parent = this
task.Depth = this.Depth + 1
this.Spider.AddTask(task)
}
func (this *Task) ForkUri(uris ...string) {
for _, uri := range uris {
this.Fork(uri, nil)
}
}