Compare commits

..

No commits in common. "480524441774b745f2c2c7335441291b6aef0bbb" and "c78d7f562762e9276050a06175724f341b3e46cc" have entirely different histories.

1 changed files with 21 additions and 22 deletions

43
job.go
View File

@ -1,10 +1,9 @@
package main package main
import ( import (
"io" "sync"
"os" "os"
"os/exec" "os/exec"
"sync"
"hilbish/util" "hilbish/util"
@ -26,9 +25,9 @@ type job struct {
// would just be so itll be the same binary command always (path changes) // would just be so itll be the same binary command always (path changes)
path string path string
handle *exec.Cmd handle *exec.Cmd
stdin io.Reader stdin *iolib.File
stdout io.Writer stdout *iolib.File
stderr io.Writer stderr *iolib.File
} }
func (j *job) start() error { func (j *job) start() error {
@ -37,9 +36,9 @@ func (j *job) start() error {
cmd := exec.Cmd{ cmd := exec.Cmd{
Path: j.path, Path: j.path,
Args: j.args, Args: j.args,
Stdin: j.stdin, Stdin: j.getStdio("in"),
Stdout: j.stdout, Stdout: j.getStdio("out"),
Stderr: j.stderr, Stderr: j.getStdio("err"),
} }
j.setHandle(&cmd) j.setHandle(&cmd)
} }
@ -76,9 +75,6 @@ func (j *job) setHandle(handle *exec.Cmd) {
j.handle = handle j.handle = handle
j.args = handle.Args j.args = handle.Args
j.path = handle.Path j.path = handle.Path
j.stdin = handle.Stdin
j.stdout = handle.Stdout
j.stderr = handle.Stderr
} }
func (j *job) getProc() *os.Process { func (j *job) getProc() *os.Process {
@ -90,12 +86,17 @@ func (j *job) getProc() *os.Process {
return nil return nil
} }
func (j *job) setStdio(typ string, f *iolib.File) { func (j *job) getStdio(typ string) *os.File {
// TODO: make this use std io/out/err values from job struct,
// which are lua files
var stdio *os.File
switch typ { switch typ {
case "in": j.stdin = f.File case "in": stdio = os.Stdin
case "out": j.stdout = f.File case "out": stdio = os.Stdout
case "err": j.stderr = f.File case "err": stdio = os.Stderr
} }
return stdio
} }
func (j *job) lua() rt.Value { func (j *job) lua() rt.Value {
@ -159,13 +160,8 @@ func (j *jobHandler) add(cmd string, args []string, path string) *job {
running: false, running: false,
id: j.latestID, id: j.latestID,
args: args, args: args,
path: path,
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
} }
j.jobs[j.latestID] = jb j.jobs[j.latestID] = jb
hooks.Em.Emit("job.add", jb.lua())
return jb return jb
} }
@ -181,7 +177,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, 2, false}, "add": {j.luaAddJob, 1, false},
} }
luaJob := rt.NewTable() luaJob := rt.NewTable()
@ -211,7 +207,10 @@ func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
func (j *jobHandler) luaAddJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (j *jobHandler) luaAddJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil { j.mu.RLock()
defer j.mu.RUnlock()
if err := c.Check1Arg(); err != nil {
return nil, err return nil, err
} }
cmd, err := c.StringArg(0) cmd, err := c.StringArg(0)