feat: add hilbish.job.add function

this is mainly to accomodate for the employer
handler (#152)
extended-job-api
TorchedSammy 2022-05-13 17:08:15 -04:00
parent 3374dfd28f
commit 827c25fb57
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 23 additions and 2 deletions

25
job.go
View File

@ -79,16 +79,19 @@ func newJobHandler() *jobHandler {
} }
} }
func (j *jobHandler) add(cmd string) { func (j *jobHandler) add(cmd string) *job {
j.mu.Lock() j.mu.Lock()
defer j.mu.Unlock() defer j.mu.Unlock()
j.latestID++ j.latestID++
j.jobs[j.latestID] = &job{ jb := &job{
cmd: cmd, cmd: cmd,
running: false, running: false,
id: j.latestID, id: j.latestID,
} }
j.jobs[j.latestID] = jb
return jb
} }
func (j *jobHandler) getLatest() *job { func (j *jobHandler) getLatest() *job {
@ -102,6 +105,7 @@ func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
jobFuncs := map[string]util.LuaExport{ jobFuncs := map[string]util.LuaExport{
"all": {j.luaAllJobs, 0, false}, "all": {j.luaAllJobs, 0, false},
"get": {j.luaGetJob, 1, false}, "get": {j.luaGetJob, 1, false},
"add": {j.luaAddJob, 1, false},
} }
luaJob := rt.NewTable() luaJob := rt.NewTable()
@ -130,6 +134,23 @@ func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.PushingNext1(t.Runtime, job.lua()), nil return c.PushingNext1(t.Runtime, job.lua()), nil
} }
func (j *jobHandler) luaAddJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
j.mu.RLock()
defer j.mu.RUnlock()
if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
jb := j.add(cmd)
return c.PushingNext1(t.Runtime, jb.lua()), nil
}
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { 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()