-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
124 lines (110 loc) · 2.8 KB
/
job.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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package collector
import (
"context"
"sync"
"time"
"github.com/admpub/log"
"github.com/admpub/nging/application/library/collector/exec"
"github.com/admpub/nging/application/library/collector/export"
"github.com/admpub/nging/application/library/cron"
"github.com/admpub/nging/application/model"
"github.com/webx-top/com"
"github.com/webx-top/db"
)
var GoProcs = sync.Map{}
func NewProc(r *exec.Rules) *GoProcess {
return &GoProcess{
Rules: r,
}
}
type GoProcess struct {
Rules *exec.Rules
Done bool
}
func (g *GoProcess) Close(k interface{}) {
g.Done = true
GoProcs.Delete(k)
}
func (g *GoProcess) IsExited() bool {
return g.Done
}
func Exit(k interface{}) (bool, error) {
proc, exists := GoProcs.Load(k)
if exists {
if p, ok := proc.(*GoProcess); ok {
p.Close(k)
}
}
return exists, nil
}
func Go(k interface{}, r *exec.Rules, f func(), ctx context.Context) (err error) {
_, err = Exit(k)
if err != nil {
return
}
process := NewProc(r)
GoProcs.Store(k, process)
r.SetExitedFn(process.IsExited)
go func() {
defer func() {
if r := recover(); r != nil {
log.Error(err)
process.Close(k)
}
}()
select {
case <-ctx.Done():
process.Close(k)
return
default:
f()
process.Close(k)
}
}()
return
}
//CollectPageJob 计划任务调用方式
func CollectPageJob(id string) cron.Runner {
return func(timeout time.Duration) (out string, runingErr string, onErr error, isTimeout bool) {
m := model.NewCollectorPage(nil)
err := m.Get(nil, db.Cond{`id`: id})
if err != nil {
onErr = cron.ErrFailure
runingErr = `获取页面采集参数失败:` + err.Error()
return
}
data, err := m.FullData()
if err != nil {
onErr = cron.ErrFailure
runingErr = `获取页面采集规则失败:` + err.Error()
return
}
data.SetExportFn(export.Export)
result, err := data.Collect(false, nil, nil)
if err != nil {
runingErr = `采集出错:` + err.Error()
return
}
b, err := com.JSONEncode(result, ` `)
if err != nil {
runingErr = err.Error()
return
}
out = string(b)
return
}
}