-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
131 lines (121 loc) · 3.28 KB
/
message.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"strings"
)
const MAX_OUTFILE_READ_LEN = 16 * 1024
type Message struct {
Executable string `json:"cmd"`
Arguments []string `json:"args"`
Mailto string `json:"mailto"`
Workdir string `json:"workdir"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
Tube string `json:"tube"`
Priority int `json:"pri"`
Delay int `json:"delay"`
User string `json:user`
Env []string `json:env`
}
func NewMessage(executable string, args []string, mailto, workdir, stdout, stderr, tube string, pri, delay int) (*Message, error) {
if tube == "" {
return nil, errors.New("Missing required param -tube")
}
if workdir == "" {
workdir = "/tmp"
}
absoluteWorkdir, e := filepath.Abs(workdir)
if e != nil {
return nil, e
}
if stdout == "" {
stdout = "/dev/null"
}
if stderr == "" {
stderr = "/dev/null"
}
u, e := user.Current()
if e != nil {
return nil, e
}
return &Message{executable, args, mailto, absoluteWorkdir, stdout, stderr, tube, pri, delay, u.Username, os.Environ()}, nil
}
func MessagesFromJSON(jsonstr []byte) ([]*Message, error) {
vals := make([]*Message, 0)
e := json.Unmarshal(jsonstr, &vals)
if e != nil {
return nil, e
}
messages := make([]*Message, len(vals))
for i, m := range vals {
msg, e := NewMessage(m.Executable, m.Arguments, m.Mailto, m.Workdir, m.Stdout, m.Stderr, m.Tube, m.Priority, m.Delay)
if e != nil {
return nil, e
}
messages[i] = msg
}
return messages, nil
}
func (this *Message) getCommand() string {
cmd := this.Executable
if len(this.Arguments) > 0 {
cmd += " " + strings.Join(this.Arguments, " ")
}
return cmd
}
// Read up to MAX_OUTFILE_READ_LEN from the files we send stdout or stderr to
func (this *Message) readOutputFile(path string) ([]byte, error) {
if path == "/dev/stdout" || path == "/dev/stderr" {
return []byte{}, nil
}
f, e := os.Open(path)
if e != nil {
return nil, e
}
br := bufio.NewReader(f)
lr := &io.LimitedReader{br, MAX_OUTFILE_READ_LEN}
buf := make([]byte, MAX_OUTFILE_READ_LEN)
n, e := lr.Read(buf)
if e != nil {
return nil, e
}
return buf[:n], nil
}
func (this *Message) readOut() string {
hostname, _ := os.Hostname()
content := make([]byte, 0)
content = append(content, []byte(fmt.Sprintf("hostname: %v\nstdout: %v\nstderr: %v\n", hostname, this.Stdout, this.Stderr))...)
stdout, e := this.readOutputFile(this.Stdout)
if e != nil {
content = append(content, []byte(
fmt.Sprintf("Could not read stdout output from [%s]. %s\n", this.Stdout, e))...)
} else {
content = append(content, []byte("STDOUT:\n")...)
content = append(content, stdout...)
content = append(content, []byte("\n")...)
}
stderr, e := this.readOutputFile(this.Stderr)
if e != nil {
content = append(content, []byte(
fmt.Sprintf("Could not read stderr output from [%s]. %s\n", this.Stderr, e))...)
} else {
content = append(content, []byte("STDERR:\n")...)
content = append(content, stderr...)
}
return string(content)
}
type ErrMessage struct {
Cmd string `json:"cmd"`
Error string `json:"error"`
Log string `json:"log"`
}
func NewErrMessage(msg *Message, e error) *ErrMessage {
return &ErrMessage{msg.getCommand(), e.Error(), msg.readOut()}
}