2016-04-08 21:41:20 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-05-01 13:24:09 +02:00
|
|
|
"encoding/json"
|
2016-05-17 21:12:54 +02:00
|
|
|
"fmt"
|
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
|
|
|
|
2016-05-24 11:55:48 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/sockets"
|
2017-02-23 06:12:16 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
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() {
|
2017-03-14 16:50:14 +01:00
|
|
|
_, err := db.Mysql.Exec("insert into task__output (task_id, task, output, time) VALUES (?, '', ?, ?)", t.task.ID, msg, now)
|
2016-04-08 21:41:20 +02:00
|
|
|
if err != nil {
|
2017-03-14 16:50:14 +01:00
|
|
|
fmt.Printf("Failed to insert task output: %s\n", err.Error())
|
2016-04-17 12:41:36 +02:00
|
|
|
panic(err)
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:12:54 +02:00
|
|
|
func (t *task) updateStatus() {
|
|
|
|
for _, user := range t.users {
|
|
|
|
b, err := json.Marshal(&map[string]interface{}{
|
|
|
|
"type": "update",
|
|
|
|
"start": t.task.Start,
|
|
|
|
"end": t.task.End,
|
|
|
|
"status": t.task.Status,
|
|
|
|
"task_id": t.task.ID,
|
|
|
|
"project_id": t.projectID,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.Message(user, b)
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
if _, err := db.Mysql.Exec("update task set status=?, start=?, end=? where id=?", t.task.Status, t.task.Start, t.task.End, t.task.ID); err != nil {
|
2017-03-14 16:50:14 +01:00
|
|
|
fmt.Printf("Failed to update task status: %s\n", err.Error())
|
2016-05-17 21:12:54 +02:00
|
|
|
t.log("Fatal error with database!")
|
|
|
|
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))
|
|
|
|
}
|