Repository navigation
Expand file tree
/
Copy pathmanager.go
More file actions
134 lines (122 loc) · 2.77 KB
/
Copy pathmanager.go
File metadata and controls
134 lines (122 loc) · 2.77 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
package manager
import (
"fmt"
"log"
"os"
"strconv"
"sync"
"time"
"github.com/olekukonko/tablewriter"
//"github.com/shirou/gopsutil/process"
)
const MaxJobs int = 100
//Manager represent a process Manager
type Manager struct {
sync.RWMutex
run chan int
done chan int
Jobs [MaxJobs]*Job
jobsCount int
}
//New return a new process manager
func New() *Manager {
m := &Manager{
run: make(chan int), //channel to signal running a job
done: make(chan int),
}
return m
}
//RunAndWait runs a Job on the manager and wait for process end
//CreateJob creates a new manager.Job from a command string
func (m *Manager) CreateJob(cmd, spec, shell string) (j *Job, err error) {
m.Lock()
defer m.Unlock()
if m.jobsCount >= MaxJobs {
err = fmt.Errorf("Manager.CreateJob: Error creating job: Max jobs reached")
return
}
j = &Job{
cmd: cmd,
Spec: spec,
shell: shell,
count: 0,
process: nil,
done: m.done,
run: m.run,
id: m.jobsCount,
}
m.Jobs[m.jobsCount] = j
m.jobsCount++
return
}
//getJob return job from jobID
func (m *Manager) getJob(id int) (j *Job, ok bool) {
m.RLock()
defer m.RUnlock()
if id >= m.jobsCount {
log.Printf("Manager.getJob: Error getting jobID %d >= jobCount=%d\n", id, m.jobsCount)
return
}
j = m.Jobs[id]
if j == nil {
log.Printf("Manager.getJob: Error getting jobID")
return
}
ok = true
return
}
//Start manager service. Receives Jobs on channel Receiver
func (m *Manager) Start() {
go func() {
for {
select {
case jobID := <-m.run:
log.Printf("Manager.main: received run signal for job %d\n", jobID)
if job, ok := m.getJob(jobID); ok {
go job.start()
}
case jobID := <-m.done:
log.Printf("Manager.main: received done signal for job %d\n", jobID)
if job, ok := m.getJob(jobID); ok {
go job.finish()
}
}
}
}()
go func() {
for {
time.Sleep(10 * time.Second)
go m.PrintStats()
}
}()
}
func (m *Manager) PrintStats() {
m.RLock()
defer m.RUnlock()
if m.jobsCount == 0 {
return
}
fmt.Println("Current Jobs:")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "STATUS", "PID", "COUNT", "MEANRSS", "MAXRSS", "CMD"})
table.SetAutoWrapText(false)
var id, status, pid, count, cmd string
// for jobID, js := range m.Jobs {
for jid := 0; jid < m.jobsCount; jid++ {
id = strconv.Itoa(jid)
job, _ := m.getJob(jid)
cmd = job.cmd
status = "stopped"
count = strconv.Itoa(job.count)
pid = "-"
if job.process != nil {
status = "running"
pid = strconv.Itoa(job.process.GetPid())
}
meanrss := fmt.Sprintf("%.2f MB", job.Stats.MeanRss/1024)
maxrss := fmt.Sprintf("%.2f MB", job.Stats.MaxRss/1024)
data := []string{id, status, pid, count, meanrss, maxrss, cmd}
table.Append(data)
}
table.Render()
}