2016-04-08 21:41:20 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-05-01 13:24:09 +02:00
|
|
|
"encoding/json"
|
2016-04-08 21:41:20 +02:00
|
|
|
"os/exec"
|
2016-05-08 11:36:17 +02:00
|
|
|
"time"
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-05-24 11:55:48 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/sockets"
|
2018-03-27 22:12:47 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2016-04-08 21:41:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t *task) log(msg string) {
|
2016-05-17 21:12:54 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
2016-04-17 12:41:36 +02:00
|
|
|
for _, user := range t.users {
|
2016-05-01 13:24:09 +02:00
|
|
|
b, err := json.Marshal(&map[string]interface{}{
|
|
|
|
"type": "log",
|
2016-05-08 11:36:17 +02:00
|
|
|
"output": msg,
|
2016-05-17 21:12:54 +02:00
|
|
|
"time": now,
|
2016-05-01 13:24:09 +02:00
|
|
|
"task_id": t.task.ID,
|
|
|
|
"project_id": t.projectID,
|
|
|
|
})
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
util.LogPanic(err)
|
2016-05-01 13:24:09 +02:00
|
|
|
|
|
|
|
sockets.Message(user, b)
|
2016-04-17 12:41:36 +02:00
|
|
|
}
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2021-08-31 00:12:33 +02:00
|
|
|
pool.logger <- logRecord{
|
|
|
|
task: t,
|
|
|
|
output: msg,
|
|
|
|
time: now,
|
|
|
|
}
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Readln reads from the pipe
|
2017-07-12 04:08:00 +02:00
|
|
|
func Readln(r *bufio.Reader) (string, error) {
|
|
|
|
var (
|
2018-03-27 22:12:47 +02:00
|
|
|
isPrefix = true
|
|
|
|
err error
|
2017-07-12 04:08:00 +02:00
|
|
|
line, ln []byte
|
|
|
|
)
|
|
|
|
for isPrefix && err == nil {
|
|
|
|
line, isPrefix, err = r.ReadLine()
|
|
|
|
ln = append(ln, line...)
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
2017-07-12 04:08:00 +02:00
|
|
|
return string(ln), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *task) logPipe(reader *bufio.Reader) {
|
|
|
|
|
2017-07-12 06:24:02 +02:00
|
|
|
line, err := Readln(reader)
|
|
|
|
for err == nil {
|
|
|
|
t.log(line)
|
|
|
|
line, err = Readln(reader)
|
2017-07-12 03:26:55 +02:00
|
|
|
}
|
2017-07-12 04:08:00 +02:00
|
|
|
|
2017-07-12 06:24:02 +02:00
|
|
|
if err != nil && err.Error() != "EOF" {
|
2017-07-12 06:48:49 +02:00
|
|
|
//don't panic on this errors, sometimes it throw not dangerous "read |0: file already closed" error
|
2018-03-27 22:12:47 +02:00
|
|
|
util.LogWarningWithFields(err, log.Fields{"error": "Failed to read task output"})
|
2017-07-12 04:08:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *task) logCmd(cmd *exec.Cmd) {
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
|
2017-07-12 04:08:00 +02:00
|
|
|
go t.logPipe(bufio.NewReader(stderr))
|
|
|
|
go t.logPipe(bufio.NewReader(stdout))
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
func (t *task) panicOnError(err error, msg string) {
|
|
|
|
if err != nil {
|
|
|
|
t.log(msg)
|
|
|
|
util.LogPanicWithFields(err, log.Fields{"error": msg})
|
|
|
|
}
|
|
|
|
}
|