micro/internal/shell/job.go

97 lines
2.7 KiB
Go
Raw Normal View History

2019-08-03 01:29:47 +00:00
package shell
import (
"bytes"
"io"
"os/exec"
)
var Jobs chan JobFunction
func init() {
Jobs = make(chan JobFunction, 100)
}
// Jobs are the way plugins can run processes in the background
// A job is simply a process that gets executed asynchronously
// There are callbacks for when the job exits, when the job creates stdout
// and when the job creates stderr
// These jobs run in a separate goroutine but the lua callbacks need to be
// executed in the main thread (where the Lua VM is running) so they are
// put into the jobs channel which gets read by the main loop
// JobFunction is a representation of a job (this data structure is what is loaded
// into the jobs channel)
type JobFunction struct {
Function func(string, []interface{})
2019-08-03 01:29:47 +00:00
Output string
2019-08-03 06:46:25 +00:00
Args []interface{}
2019-08-03 01:29:47 +00:00
}
// A CallbackFile is the data structure that makes it possible to catch stderr and stdout write events
type CallbackFile struct {
io.Writer
callback func(string, []interface{})
2019-08-03 06:46:25 +00:00
args []interface{}
2019-08-03 01:29:47 +00:00
}
2020-08-10 16:24:29 +00:00
// Job stores the executing command for the job, and the stdin pipe
type Job struct {
*exec.Cmd
Stdin io.WriteCloser
}
2019-08-03 01:29:47 +00:00
func (f *CallbackFile) Write(data []byte) (int, error) {
// This is either stderr or stdout
// In either case we create a new job function callback and put it in the jobs channel
jobFunc := JobFunction{f.callback, string(data), f.args}
Jobs <- jobFunc
return f.Writer.Write(data)
}
// JobStart starts a shell command in the background with the given callbacks
// It returns an *exec.Cmd as the job id
2020-08-10 16:24:29 +00:00
func JobStart(cmd string, onStdout, onStderr, onExit func(string, []interface{}), userargs ...interface{}) *Job {
2019-08-03 01:29:47 +00:00
return JobSpawn("sh", []string{"-c", cmd}, onStdout, onStderr, onExit, userargs...)
}
// JobSpawn starts a process with args in the background with the given callbacks
// It returns an *exec.Cmd as the job id
2020-08-10 16:24:29 +00:00
func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit func(string, []interface{}), userargs ...interface{}) *Job {
2019-08-03 01:29:47 +00:00
// Set up everything correctly if the functions have been provided
proc := exec.Command(cmdName, cmdArgs...)
var outbuf bytes.Buffer
if onStdout != nil {
proc.Stdout = &CallbackFile{&outbuf, onStdout, userargs}
2019-08-03 01:29:47 +00:00
} else {
proc.Stdout = &outbuf
}
if onStderr != nil {
proc.Stderr = &CallbackFile{&outbuf, onStderr, userargs}
2019-08-03 01:29:47 +00:00
} else {
proc.Stderr = &outbuf
}
2020-08-10 16:24:29 +00:00
stdin, _ := proc.StdinPipe()
2019-08-03 01:29:47 +00:00
go func() {
// Run the process in the background and create the onExit callback
proc.Run()
jobFunc := JobFunction{onExit, outbuf.String(), userargs}
2019-08-03 01:29:47 +00:00
Jobs <- jobFunc
}()
2020-08-10 16:24:29 +00:00
return &Job{proc, stdin}
2019-08-03 01:29:47 +00:00
}
// JobStop kills a job
2020-08-10 16:24:29 +00:00
func JobStop(j *Job) {
j.Process.Kill()
2019-08-03 01:29:47 +00:00
}
// JobSend sends the given data into the job's stdin stream
2020-08-10 16:24:29 +00:00
func JobSend(j *Job, data string) {
j.Stdin.Write([]byte(data))
2019-08-03 01:29:47 +00:00
}