-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_handler.go
More file actions
147 lines (132 loc) · 2.99 KB
/
process_handler.go
File metadata and controls
147 lines (132 loc) · 2.99 KB
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
package main
import (
"context"
"log"
"sync"
"time"
"github.com/go-cmd/cmd"
)
const (
DELAY_BEFORE_RUN_AGAIN = 5 * time.Second
RUN_CONFIRM_DELAY = 3 * time.Second
)
type ProcessRunStatusFunc func(id int64, runStatus string)
type LogsFunc func(id int64, time int64, _type string, message string)
type ProcessHandler struct {
processList *ProcessList
runStatusFunc ProcessRunStatusFunc
logsFunc LogsFunc
ShutDown bool
logs map[int64]*LogsList
logsLock *sync.RWMutex
}
func (ph *ProcessHandler) AddProcess(p *Process) {
ph.RemoveProcess(p.Id)
if !ph.processList.Add(p) {
return
}
ph.logsLock.Lock()
if ph.logs[p.Id] == nil {
ph.logs[p.Id] = NewLogsList()
}
ph.logsLock.Unlock()
go func() {
for {
lf := func(time int64, _type, message string) {
ph.logsLock.RLock()
ph.logs[p.Id].Add(&Log{
Time: time,
Type: _type,
Message: message,
})
ph.logsLock.RUnlock()
ph.logsFunc(p.Id, time, _type, message)
}
p.Run(lf)
var status cmd.Status
select {
case <-time.After(RUN_CONFIRM_DELAY):
p.RunStatus = "running"
ph.runStatusFunc(p.Id, "running")
status = <-p.StatusChan
case status = <-p.StatusChan:
}
log.Println("**********************************************************************************")
log.Println(status)
if status.Error != nil {
lf(time.Now().UnixMilli(), "err", status.Error.Error())
}
p.RunStatus = "idle"
ph.runStatusFunc(p.Id, "idle")
time.Sleep(DELAY_BEFORE_RUN_AGAIN)
if p.Status == 0 {
log.Println("ProcessHandler: AddProcess: breaking out of loop since p.Status is 0")
break
}
if ph.ShutDown {
log.Println("ProcessHandler: AddPrxoy: breaking out of loop since shutdown!")
break
}
}
}()
}
func (ph *ProcessHandler) RemoveProcess(id int64) {
p := ph.processList.Remove(id)
if p != nil {
p.Status = 0
p.Stop()
}
}
func (ph *ProcessHandler) Start(ctx context.Context, dbHandler *DBHandler) {
list, err := dbHandler.GetProcesses(ctx, true)
if err != nil {
return
}
for i := range list {
p := list[i]
ph.AddProcess(&p)
}
}
func (ph *ProcessHandler) Stop() {
ph.ShutDown = true
list := ph.processList.GetAll()
for i := range list {
p := list[i]
if p != nil {
p.Stop()
}
}
}
func (ph *ProcessHandler) GetLogs(id int64) []*Log {
ph.logsLock.RLock()
defer ph.logsLock.RUnlock()
if ph.logs[id] == nil {
return []*Log{}
}
return ph.logs[id].GetAll()
}
func (ph *ProcessHandler) DeleteLogs(id int64) {
ph.logsLock.Lock()
defer ph.logsLock.Unlock()
delete(ph.logs, id)
}
func (ph *ProcessHandler) ClearLogs(id int64) {
ph.logsLock.RLock()
ll := ph.logs[id]
ph.logsLock.RUnlock()
if ll != nil {
ll.Clear()
}
}
func NewProcessHandler(
runStatusFunc ProcessRunStatusFunc,
logsFunc LogsFunc,
) *ProcessHandler {
return &ProcessHandler{
processList: NewProcessList(),
runStatusFunc: runStatusFunc,
logsFunc: logsFunc,
logs: map[int64]*LogsList{},
logsLock: &sync.RWMutex{},
}
}