mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
|
package runners
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"os/exec"
|
||
|
"time"
|
||
|
|
||
|
"github.com/ansible-semaphore/semaphore/lib"
|
||
|
"github.com/ansible-semaphore/semaphore/services/tasks"
|
||
|
"github.com/ansible-semaphore/semaphore/util"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type runningJob struct {
|
||
|
status lib.TaskStatus
|
||
|
logRecords []LogRecord
|
||
|
job *tasks.LocalJob
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) Log(msg string) {
|
||
|
p.LogWithTime(time.Now(), msg)
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) Logf(format string, a ...any) {
|
||
|
p.LogfWithTime(time.Now(), format, a...)
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) LogWithTime(now time.Time, msg string) {
|
||
|
p.logRecords = append(
|
||
|
p.logRecords,
|
||
|
LogRecord{
|
||
|
Time: now,
|
||
|
Message: msg,
|
||
|
},
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) LogfWithTime(now time.Time, format string, a ...any) {
|
||
|
p.LogWithTime(now, fmt.Sprintf(format, a...))
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) LogCmd(cmd *exec.Cmd) {
|
||
|
stderr, _ := cmd.StderrPipe()
|
||
|
stdout, _ := cmd.StdoutPipe()
|
||
|
|
||
|
go p.logPipe(bufio.NewReader(stderr))
|
||
|
go p.logPipe(bufio.NewReader(stdout))
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) SetStatus(status lib.TaskStatus) {
|
||
|
if p.status == status {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
p.status = status
|
||
|
p.job.SetStatus(status)
|
||
|
}
|
||
|
|
||
|
func (p *runningJob) logPipe(reader *bufio.Reader) {
|
||
|
line, err := tasks.Readln(reader)
|
||
|
for err == nil {
|
||
|
p.Log(line)
|
||
|
line, err = tasks.Readln(reader)
|
||
|
}
|
||
|
|
||
|
if err != nil && err.Error() != "EOF" {
|
||
|
//don't panic on these errors, sometimes it throws not dangerous "read |0: file already closed" error
|
||
|
util.LogWarningWithFields(err, log.Fields{"error": "Failed to read TaskRunner output"})
|
||
|
}
|
||
|
}
|