-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
87 lines (76 loc) · 2.16 KB
/
status.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
// Copyright 2020 Eryx <evorui аt gmail dοt com>, All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package injob
import (
"sort"
"sync"
)
const (
StatusOK uint64 = 1 << 1
StatusER uint64 = 1 << 2
jobExecLogMax = 10
)
type DaemonStatus struct {
Jobs []*JobStatus `json:"jobs" toml:"jobs"`
}
type JobExecLog struct {
Created int64 `json:"created" toml:"created"`
Updated int64 `json:"updated" toml:"updated"`
Status uint64 `json:"status" toml:"status"`
Message string `json:"message,omitempty" toml:"message,omitempty"`
}
type JobStatus struct {
mu sync.RWMutex
ExecNum int64 `json:"exec_num" toml:"exec_num"`
NextExecTime int64 `json:"next_exec_time" toml:"next_exec_time"`
ExecLogs []*JobExecLog `json:"exec_logs" toml:"exec_logs"`
}
func newJobExecLog() *JobExecLog {
return &JobExecLog{
Created: timenow(),
}
}
func (it *JobExecLog) OK(args ...string) *JobExecLog {
it.Status = StatusOK
if len(args) == 1 {
it.Message = args[0]
}
return it
}
func (it *JobExecLog) ER(msg string) *JobExecLog {
it.Status = StatusER
it.Message = msg
return it
}
func (it *JobStatus) LogSync(log *JobExecLog) {
it.mu.Lock()
defer it.mu.Unlock()
it.ExecNum++
log.Updated = timenow()
it.ExecLogs = append(it.ExecLogs, log)
sort.Slice(it.ExecLogs, func(i, j int) bool {
return it.ExecLogs[i].Created < it.ExecLogs[j].Created
})
if len(it.ExecLogs) > jobExecLogMax {
it.ExecLogs = it.ExecLogs[len(it.ExecLogs)-jobExecLogMax:]
}
}
func (it *JobStatus) LastLog() *JobExecLog {
it.mu.RLock()
defer it.mu.RUnlock()
if len(it.ExecLogs) > 0 {
return it.ExecLogs[len(it.ExecLogs)-1]
}
return nil
}