forked from ThreeKing2018/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasklog.go
52 lines (43 loc) · 878 Bytes
/
tasklog.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
package cronjobs
import (
"github.com/pkg/errors"
"sync"
)
var defaultMaxLogLen = 100
const (
TASKLOG_SUCCESS = 0 // 任务执行成功
TASKLOG_ERROR = -1 // 任务执行出错
TASKLOG_TIMEOUT = -2 // 任务执行超时
)
type TaskLog struct {
TaskId int
Output string
Error string
Status int
ProcessTime int
CreateTime int64
}
type TaskLogMana struct {
mu sync.Mutex
TaskLog []*TaskLog
Count int
}
func (t *TaskLogMana) Add(log *TaskLog) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.Count >= defaultMaxLogLen {
return errors.New("超过最大存储的日志数量")
}
t.TaskLog = append(t.TaskLog, log)
t.Count++
return nil
}
func (t *TaskLogMana) Detail(num int) []*TaskLog {
if num > defaultMaxLogLen {
num = defaultMaxLogLen
}
if num > len(t.TaskLog) {
num = len(t.TaskLog)
}
return t.TaskLog[:num]
}