2022-03-19 17:10:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-03-20 23:10:12 +00:00
|
|
|
"os"
|
2022-03-19 17:10:50 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2022-03-19 17:10:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var jobs *jobHandler
|
|
|
|
|
|
|
|
type job struct {
|
|
|
|
cmd string
|
|
|
|
running bool
|
|
|
|
id int
|
|
|
|
pid int
|
|
|
|
exitCode int
|
2022-03-20 23:10:12 +00:00
|
|
|
proc *os.Process
|
2022-03-19 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *job) start(pid int) {
|
|
|
|
j.pid = pid
|
|
|
|
j.running = true
|
|
|
|
hooks.Em.Emit("job.start", j.lua())
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:10:12 +00:00
|
|
|
func (j *job) stop() {
|
|
|
|
// finish will be called in exec handle
|
|
|
|
j.proc.Kill()
|
|
|
|
}
|
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
func (j *job) finish() {
|
|
|
|
j.running = false
|
|
|
|
hooks.Em.Emit("job.done", j.lua())
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:10:12 +00:00
|
|
|
func (j *job) setHandle(handle *os.Process) {
|
|
|
|
j.proc = handle
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (j *job) lua() rt.Value {
|
|
|
|
jobFuncs := map[string]util.LuaExport{
|
|
|
|
"stop": {j.luaStop, 0, false},
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
luaJob := rt.NewTable()
|
|
|
|
util.SetExports(l, luaJob, jobFuncs)
|
2022-03-19 17:10:50 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
luaJob.Set(rt.StringValue("cmd"), rt.StringValue(j.cmd))
|
|
|
|
luaJob.Set(rt.StringValue("running"), rt.BoolValue(j.running))
|
|
|
|
luaJob.Set(rt.StringValue("id"), rt.IntValue(int64(j.id)))
|
|
|
|
luaJob.Set(rt.StringValue("pid"), rt.IntValue(int64(j.pid)))
|
|
|
|
luaJob.Set(rt.StringValue("exitCode"), rt.IntValue(int64(j.exitCode)))
|
2022-03-19 17:10:50 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return rt.TableValue(luaJob)
|
2022-03-19 17:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-03-20 23:10:12 +00:00
|
|
|
if j.running {
|
|
|
|
j.stop()
|
2022-05-13 02:27:49 +00:00
|
|
|
j.finish()
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 17:10:50 +00:00
|
|
|
type jobHandler struct {
|
|
|
|
jobs map[int]*job
|
|
|
|
latestID int
|
|
|
|
mu *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newJobHandler() *jobHandler {
|
|
|
|
return &jobHandler{
|
|
|
|
jobs: make(map[int]*job),
|
|
|
|
latestID: 0,
|
|
|
|
mu: &sync.RWMutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jobHandler) add(cmd string) {
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
j.latestID++
|
|
|
|
j.jobs[j.latestID] = &job{
|
|
|
|
cmd: cmd,
|
|
|
|
running: false,
|
|
|
|
id: j.latestID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jobHandler) getLatest() *job {
|
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
|
|
|
return j.jobs[j.latestID]
|
|
|
|
}
|
2022-03-20 23:10:12 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
|
|
|
jobFuncs := map[string]util.LuaExport{
|
|
|
|
"all": {j.luaAllJobs, 0, false},
|
|
|
|
"get": {j.luaGetJob, 1, false},
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
luaJob := rt.NewTable()
|
|
|
|
util.SetExports(rtm, luaJob, jobFuncs)
|
2022-03-20 23:10:12 +00:00
|
|
|
|
|
|
|
return luaJob
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-03-20 23:10:12 +00:00
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jobID, err := c.IntArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
job := j.jobs[int(jobID)]
|
|
|
|
if job == nil {
|
|
|
|
return c.Next(), nil
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, job.lua()), nil
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-03-20 23:10:12 +00:00
|
|
|
j.mu.RLock()
|
|
|
|
defer j.mu.RUnlock()
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
jobTbl := rt.NewTable()
|
2022-03-20 23:10:12 +00:00
|
|
|
for id, job := range j.jobs {
|
2022-04-04 10:40:02 +00:00
|
|
|
jobTbl.Set(rt.IntValue(int64(id)), job.lua())
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.TableValue(jobTbl)), nil
|
2022-03-20 23:10:12 +00:00
|
|
|
}
|