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 process 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, " ")
50 // Set up everything correctly if the functions have been provided
51 proc := exec.Command(cmdName, args...)
52 var outbuf bytes.Buffer
54 proc.Stdout = &CallbackFile{&outbuf, LuaFunctionJob(onStdout), userargs}
59 proc.Stderr = &CallbackFile{&outbuf, LuaFunctionJob(onStderr), userargs}
65 // Run the process in the background and create the onExit callback
67 jobFunc := JobFunction{LuaFunctionJob(onExit), string(outbuf.Bytes()), userargs}
74 // JobStop kills a job
75 func JobStop(cmd *exec.Cmd) {
79 // JobSend sends the given data into the job's stdin stream
80 func JobSend(cmd *exec.Cmd, data string) {
81 stdin, err := cmd.StdinPipe()
86 stdin.Write([]byte(data))