fix: reuse standard files for jobs

extended-job-api
TorchedSammy 2022-05-17 17:43:42 -04:00
parent c78d7f5627
commit b5c78a39a8
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 18 additions and 16 deletions

34
job.go
View File

@ -1,9 +1,10 @@
package main package main
import ( import (
"sync" "io"
"os" "os"
"os/exec" "os/exec"
"sync"
"hilbish/util" "hilbish/util"
@ -25,9 +26,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 *iolib.File stdin io.Reader
stdout *iolib.File stdout io.Writer
stderr *iolib.File stderr io.Writer
} }
func (j *job) start() error { func (j *job) start() error {
@ -36,9 +37,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.getStdio("in"), Stdin: j.stdin,
Stdout: j.getStdio("out"), Stdout: j.stdout,
Stderr: j.getStdio("err"), Stderr: j.stderr,
} }
j.setHandle(&cmd) j.setHandle(&cmd)
} }
@ -75,6 +76,9 @@ 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 {
@ -86,17 +90,12 @@ func (j *job) getProc() *os.Process {
return nil return nil
} }
func (j *job) getStdio(typ string) *os.File { func (j *job) setStdio(typ string, f *iolib.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": stdio = os.Stdin case "in": j.stdin = f.File
case "out": stdio = os.Stdout case "out": j.stdout = f.File
case "err": stdio = os.Stderr case "err": j.stderr = f.File
} }
return stdio
} }
func (j *job) lua() rt.Value { func (j *job) lua() rt.Value {
@ -160,6 +159,9 @@ 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,
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
} }
j.jobs[j.latestID] = jb j.jobs[j.latestID] = jb