mirror of https://github.com/Hilbis/Hilbish
feat: implement job management
parent
c46807613e
commit
8714f54915
8
api.go
8
api.go
|
@ -141,11 +141,9 @@ Check out the {blue}{bold}guide{reset} command to get started.
|
||||||
|
|
||||||
// hilbish.jobs table
|
// hilbish.jobs table
|
||||||
jobs = newJobHandler()
|
jobs = newJobHandler()
|
||||||
/*
|
jobModule := jobs.loader(rtm)
|
||||||
jobModule := jobs.loader(L)
|
// util.Document(L, jobModule, "(Background) job interface.")
|
||||||
util.Document(L, jobModule, "(Background) job interface.")
|
mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
|
||||||
L.SetField(mod, "jobs", jobModule)
|
|
||||||
*/
|
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
return rt.TableValue(mod), nil
|
||||||
}
|
}
|
||||||
|
|
79
job.go
79
job.go
|
@ -3,6 +3,10 @@ package main
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"hilbish/util"
|
||||||
|
|
||||||
|
rt "github.com/arnodel/golua/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jobs *jobHandler
|
var jobs *jobHandler
|
||||||
|
@ -19,7 +23,7 @@ type job struct {
|
||||||
func (j *job) start(pid int) {
|
func (j *job) start(pid int) {
|
||||||
j.pid = pid
|
j.pid = pid
|
||||||
j.running = true
|
j.running = true
|
||||||
// hooks.Em.Emit("job.start", j.lua())
|
hooks.Em.Emit("job.start", j.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) stop() {
|
func (j *job) stop() {
|
||||||
|
@ -29,39 +33,36 @@ func (j *job) stop() {
|
||||||
|
|
||||||
func (j *job) finish() {
|
func (j *job) finish() {
|
||||||
j.running = false
|
j.running = false
|
||||||
// hooks.Em.Emit("job.done", j.lua())
|
hooks.Em.Emit("job.done", j.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) setHandle(handle *os.Process) {
|
func (j *job) setHandle(handle *os.Process) {
|
||||||
j.proc = handle
|
j.proc = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func (j *job) lua() rt.Value {
|
||||||
func (j *job) lua() *lua.LTable {
|
jobFuncs := map[string]util.LuaExport{
|
||||||
// returns lua table for job
|
"stop": {j.luaStop, 0, false},
|
||||||
// because userdata is gross
|
|
||||||
jobFuncs := map[string]lua.LGFunction{
|
|
||||||
"stop": j.luaStop,
|
|
||||||
}
|
}
|
||||||
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
|
luaJob := rt.NewTable()
|
||||||
|
util.SetExports(l, luaJob, jobFuncs)
|
||||||
|
|
||||||
l.SetField(luaJob, "cmd", lua.LString(j.cmd))
|
luaJob.Set(rt.StringValue("cmd"), rt.StringValue(j.cmd))
|
||||||
l.SetField(luaJob, "running", lua.LBool(j.running))
|
luaJob.Set(rt.StringValue("running"), rt.BoolValue(j.running))
|
||||||
l.SetField(luaJob, "id", lua.LNumber(j.id))
|
luaJob.Set(rt.StringValue("id"), rt.IntValue(int64(j.id)))
|
||||||
l.SetField(luaJob, "pid", lua.LNumber(j.pid))
|
luaJob.Set(rt.StringValue("pid"), rt.IntValue(int64(j.pid)))
|
||||||
l.SetField(luaJob, "exitCode", lua.LNumber(j.exitCode))
|
luaJob.Set(rt.StringValue("exitCode"), rt.IntValue(int64(j.exitCode)))
|
||||||
|
|
||||||
return luaJob
|
return rt.TableValue(luaJob)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) luaStop(L *lua.LState) int {
|
func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
if j.running {
|
if j.running {
|
||||||
j.stop()
|
j.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return c.Next(), nil
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
type jobHandler struct {
|
type jobHandler struct {
|
||||||
jobs map[int]*job
|
jobs map[int]*job
|
||||||
|
@ -96,42 +97,46 @@ func (j *jobHandler) getLatest() *job {
|
||||||
return j.jobs[j.latestID]
|
return j.jobs[j.latestID]
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
||||||
func (j *jobHandler) loader(L *lua.LState) *lua.LTable {
|
jobFuncs := map[string]util.LuaExport{
|
||||||
jobFuncs := map[string]lua.LGFunction{
|
"all": {j.luaAllJobs, 0, false},
|
||||||
"all": j.luaAllJobs,
|
"get": {j.luaGetJob, 1, false},
|
||||||
"get": j.luaGetJob,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
|
luaJob := rt.NewTable()
|
||||||
|
util.SetExports(rtm, luaJob, jobFuncs)
|
||||||
|
|
||||||
return luaJob
|
return luaJob
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jobHandler) luaGetJob(L *lua.LState) int {
|
func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
j.mu.RLock()
|
j.mu.RLock()
|
||||||
defer j.mu.RUnlock()
|
defer j.mu.RUnlock()
|
||||||
|
|
||||||
jobID := L.CheckInt(1)
|
if err := c.Check1Arg(); err != nil {
|
||||||
job := j.jobs[jobID]
|
return nil, err
|
||||||
if job != nil {
|
}
|
||||||
return 0
|
jobID, err := c.IntArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
L.Push(job.lua())
|
|
||||||
|
|
||||||
return 1
|
job := j.jobs[int(jobID)]
|
||||||
|
if job == nil {
|
||||||
|
return c.Next(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.PushingNext1(t.Runtime, job.lua()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jobHandler) luaAllJobs(L *lua.LState) int {
|
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
j.mu.RLock()
|
j.mu.RLock()
|
||||||
defer j.mu.RUnlock()
|
defer j.mu.RUnlock()
|
||||||
|
|
||||||
jobTbl := L.NewTable()
|
jobTbl := rt.NewTable()
|
||||||
for id, job := range j.jobs {
|
for id, job := range j.jobs {
|
||||||
jobTbl.Insert(id, job.lua())
|
jobTbl.Set(rt.IntValue(int64(id)), job.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
L.Push(jobTbl)
|
return c.PushingNext1(t.Runtime, rt.TableValue(jobTbl)), nil
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in New Issue