Process

/app/lib/exec/exec.go (3.0 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
package exec

import (
"bytes"
"cmp"
"fmt"
"io"
"net/url"
"os/exec"
"slices"
"strings"
"time"

"github.com/pkg/errors"
"github.com/samber/lo"

"{{{ .Package }}}/app/util"
)

type writer struct {
Key string
fn func(key string, b []byte) error
}

func (w *writer) Write(p []byte) (int, error) {
err := w.fn(w.Key, p)
return len(p), err
}

type Exec struct {
Key string `json:"key"`
Idx int `json:"idx"`
Cmd string `json:"cmd"`
Env []string `json:"env,omitempty"`
Path string `json:"path"`
Started *time.Time `json:"started"`
PID int `json:"pid"`
Completed *time.Time `json:"completed"`
ExitCode int `json:"exitCode"`
Link string `json:"link,omitempty"`
Buffer *bytes.Buffer `json:"-"`
execCmd *exec.Cmd
}

func NewExec(key string, idx int, cmd string, path string, envvars ...string) *Exec {
return &Exec{Key: key, Idx: idx, Cmd: cmd, Env: envvars, Path: path, Buffer: &bytes.Buffer{}}
}

func (e *Exec) WebPath() string {
return fmt.Sprintf("/admin/exec/%s/%d", url.QueryEscape(e.Key), e.Idx)
}

func (e *Exec) Start(fns ...func(key string, b []byte) error) error {
if e.Started != nil {
return errors.New("process already started")
}
var w io.Writer = e.Buffer
lo.ForEach(fns, func(fn func(key string, b []byte) error, _ int) {
w = io.MultiWriter(w, &writer{Key: e.String(), fn: fn})
})
e.Started = util.TimeCurrentP()
cmd, err := util.StartProcess(e.Cmd, e.Path, nil, w, w, e.Env...)
if err != nil {
return err
}
e.execCmd = cmd
e.PID = cmd.Process.Pid
defer func() {
go func() {
_ = e.Wait()
}()
}()
return nil
}

func (e *Exec) Kill() error {
if e.execCmd == nil {
return errors.New("not started")
}
return e.execCmd.Process.Kill()
}

func (e *Exec) Wait() error {
if e.execCmd == nil {
return errors.New("not started")
}
exit, err := e.execCmd.Process.Wait()
if err != nil {
_ = e.execCmd.Wait()
for (e.execCmd.ProcessState == nil || !e.execCmd.ProcessState.Exited()) && time.Since(util.TimeCurrent()) < (4*time.Second) {
time.Sleep(500 * time.Millisecond)
}
exit = e.execCmd.ProcessState
}

e.Completed = util.TimeCurrentP()
if exit != nil {
e.ExitCode = exit.ExitCode()
}
return nil
}

func (e *Exec) String() string {
return fmt.Sprintf("%s:%d", e.Key, e.Idx)
}

type Execs []*Exec

func (m Execs) Get(key string, idx int) *Exec {
return lo.FindOrElse(m, nil, func(x *Exec) bool {
return x.Key == key && x.Idx == idx
})
}

func (m Execs) GetByKey(key string) Execs {
return lo.Filter(m, func(x *Exec, _ int) bool {
return x.Key == key
})
}

func (m Execs) Running() int {
return lo.CountBy(m, func(x *Exec) bool {
return x.Completed == nil
})
}

func (m Execs) Sort() {
slices.SortFunc(m, func(l *Exec, r *Exec) int {
lk, rk := strings.ToLower(l.Key), strings.ToLower(r.Key)
if lk != rk {
return cmp.Compare(lk, rk)
}
return cmp.Compare(l.Idx, r.Idx)
})
}