File: job.go

package info (click to toggle)
micro 2.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: sh: 265; makefile: 77; xml: 53
file content (98 lines) | stat: -rw-r--r-- 2,765 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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, []any)
	Output   string
	Args     []any
}

// 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, []any)
	args     []any
}

// Job stores the executing command for the job, and the stdin pipe
type Job struct {
	*exec.Cmd
	Stdin io.WriteCloser
}

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
func JobStart(cmd string, onStdout, onStderr, onExit func(string, []any), userargs ...any) *Job {
	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
func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit func(string, []any), userargs ...any) *Job {
	// 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}
	} else {
		proc.Stdout = &outbuf
	}
	if onStderr != nil {
		proc.Stderr = &CallbackFile{&outbuf, onStderr, userargs}
	} else {
		proc.Stderr = &outbuf
	}
	stdin, _ := proc.StdinPipe()

	go func() {
		// Run the process in the background and create the onExit callback
		proc.Run()
		if onExit != nil {
			jobFunc := JobFunction{onExit, outbuf.String(), userargs}
			Jobs <- jobFunc
		}
	}()

	return &Job{proc, stdin}
}

// JobStop kills a job
func JobStop(j *Job) {
	j.Process.Kill()
}

// JobSend sends the given data into the job's stdin stream
func JobSend(j *Job, data string) {
	j.Stdin.Write([]byte(data))
}