10 // Jobs are the way plugins can run processes in the background
11 // A job is simply a process that gets executed asynchronously
12 // There are callbacks for when the job exits, when the job creates stdout
13 // and when the job creates stderr
15 // These jobs run in a separate goroutine but the lua callbacks need to be
16 // executed in the main thread (where the Lua VM is running) so they are
17 // put into the jobs channel which gets read by the main loop
19 // JobFunction is a representation of a job (this data structure is what is loaded
20 // into the jobs channel)
21 type JobFunction struct {
22 function func(string, ...string)
27 // A CallbackFile is the data structure that makes it possible to catch stderr and stdout write events
28 type CallbackFile struct {
31 callback func(string, ...string)
35 func (f *CallbackFile) Write(data []byte) (int, error) {
36 // This is either stderr or stdout
37 // In either case we create a new job function callback and put it in the jobs channel
38 jobFunc := JobFunction{f.callback, string(data), f.args}
40 return f.Writer.Write(data)
43 // JobStart starts a shell command in the background with the given callbacks
44 // It returns an *exec.Cmd as the job id
45 func JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd {
46 split := strings.Split(cmd, " ")
49 return JobSpawn(cmdName, cmdArgs, onStdout, onStderr, onExit, userargs...)
52 // JobSpawn starts a process with args in the background with the given callbacks
53 // It returns an *exec.Cmd as the job id
54 func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd {
55 // Set up everything correctly if the functions have been provided
56 proc := exec.Command(cmdName, cmdArgs...)
57 var outbuf bytes.Buffer
59 proc.Stdout = &CallbackFile{&outbuf, LuaFunctionJob(onStdout), userargs}
64 proc.Stderr = &CallbackFile{&outbuf, LuaFunctionJob(onStderr), userargs}
70 // Run the process in the background and create the onExit callback
72 jobFunc := JobFunction{LuaFunctionJob(onExit), string(outbuf.Bytes()), userargs}
79 // JobStop kills a job
80 func JobStop(cmd *exec.Cmd) {
84 // JobSend sends the given data into the job's stdin stream
85 func JobSend(cmd *exec.Cmd, data string) {
86 stdin, err := cmd.StdinPipe()
91 stdin.Write([]byte(data))