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"
|
|
|
|
|
|
|
|
"github.com/ansible-semaphore/semaphore/database"
|
|
|
|
"github.com/ansible-semaphore/semaphore/routes/sockets"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *task) log(msg string) {
|
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",
|
|
|
|
"m": msg,
|
|
|
|
"task_id": t.task.ID,
|
|
|
|
"project_id": t.projectID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.Message(user, b)
|
2016-04-17 12:41:36 +02:00
|
|
|
}
|
2016-04-08 21:41:20 +02:00
|
|
|
|
|
|
|
go func() {
|
2016-04-09 00:51:24 +02:00
|
|
|
_, err := database.Mysql.Exec("insert into task__output set task_id=?, output=?, time=NOW(6)", t.task.ID, msg)
|
2016-04-08 21:41:20 +02:00
|
|
|
if err != nil {
|
2016-04-17 12:41:36 +02:00
|
|
|
panic(err)
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *task) logPipe(scanner *bufio.Scanner) {
|
|
|
|
for scanner.Scan() {
|
|
|
|
t.log(scanner.Text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *task) logCmd(cmd *exec.Cmd) {
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
|
|
|
|
go t.logPipe(bufio.NewScanner(stderr))
|
|
|
|
go t.logPipe(bufio.NewScanner(stdout))
|
|
|
|
}
|