-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathTaskRunner_logging.go
More file actions
207 lines (165 loc) · 4.36 KB
/
Copy pathTaskRunner_logging.go
File metadata and controls
207 lines (165 loc) · 4.36 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package tasks
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os/exec"
"time"
"github.com/semaphoreui/semaphore/pkg/tz"
"github.com/semaphoreui/semaphore/api/sockets"
"github.com/semaphoreui/semaphore/pkg/conv"
"github.com/semaphoreui/semaphore/pkg/task_logger"
"github.com/semaphoreui/semaphore/util"
log "github.com/sirupsen/logrus"
)
func (t *TaskRunner) Log(msg string) {
t.LogWithTime(tz.Now(), msg)
}
func (t *TaskRunner) Logf(format string, a ...any) {
t.LogfWithTime(tz.Now(), format, a...)
}
func (t *TaskRunner) LogWithTime(now time.Time, msg string) {
t.sendToWs(now, msg)
t.pool.logger <- logRecord{
task: t,
output: msg,
time: now,
}
for _, l := range t.logListeners {
l(now, msg)
}
}
func (t *TaskRunner) sendToWs(now time.Time, msg string) {
for _, user := range t.users {
b, err := json.Marshal(&map[string]any{
"type": "log",
"output": msg,
"time": now,
"task_id": t.Task.ID,
"project_id": t.Task.ProjectID,
})
util.LogPanic(err)
sockets.Message(user, b)
}
}
func (t *TaskRunner) LogfWithTime(now time.Time, format string, a ...any) {
t.LogWithTime(now, fmt.Sprintf(format, a...))
}
func (t *TaskRunner) LogCmd(cmd *exec.Cmd) {
stderr, _ := cmd.StderrPipe()
stdout, _ := cmd.StdoutPipe()
go t.logPipe(stderr)
go t.logPipe(stdout)
}
func (t *TaskRunner) WaitLog() {
t.logWG.Wait()
}
func (t *TaskRunner) SetCommit(hash, message string) {
t.Task.CommitHash = &hash
// Sanitize before persisting to the DB. This is the persistence point for
// remote-runner commit reports; local tasks reach it too via the task logger
// (LocalExecutor.SetCommit -> Logger.SetCommit), and that local mirror
// additionally sanitizes the copy it exposes as task extra vars.
// See conv.TruncateValidUTF8.
t.Task.CommitMessage = conv.TruncateValidUTF8(message)
if err := t.pool.store.UpdateTask(t.Task); err != nil {
t.panicOnError(err, "Failed to update task commit")
}
}
func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
if status == t.Task.Status {
return
}
switch t.Task.Status { // check old status
case task_logger.TaskConfirmed:
if status == task_logger.TaskWaitingConfirmation {
return
}
case task_logger.TaskRunningStatus:
if status == task_logger.TaskWaitingStatus {
return
}
case task_logger.TaskStoppingStatus:
if status == task_logger.TaskWaitingStatus || status == task_logger.TaskRunningStatus || status == task_logger.TaskWaitingConfirmation {
//panic("stopping TaskRunner cannot be " + status)
return
}
case task_logger.TaskSuccessStatus:
case task_logger.TaskFailStatus:
case task_logger.TaskStoppedStatus:
return
}
t.Task.Status = status
if status == task_logger.TaskRunningStatus {
now := tz.Now()
t.Task.Start = &now
}
t.saveStatus()
if localJob, ok := t.job.(*LocalExecutor); ok {
localJob.SetStatus(status)
}
if status == task_logger.TaskFailStatus {
t.sendMailAlert()
}
if status.IsNotifiable() {
t.sendTelegramAlert()
t.sendSlackAlert()
t.sendRocketChatAlert()
t.sendMicrosoftTeamsAlert()
t.sendDingTalkAlert()
t.sendGotifyAlert()
}
for _, l := range t.statusListeners {
l(status)
}
log.WithFields(log.Fields{
"task_id": t.Task.ID,
"context": "task_logger",
"status": status,
}).Info("Task status updated")
}
func (t *TaskRunner) panicOnError(err error, msg string) {
if err == nil {
return
}
t.Log(msg)
util.LogPanicF(err, log.Fields{"error": msg})
}
func (t *TaskRunner) logPipe(reader io.Reader) {
t.logWG.Add(1)
linesCh := make(chan string, 100000)
go func() {
defer t.logWG.Done()
for line := range linesCh {
t.Log(line)
}
}()
scanner := bufio.NewScanner(reader)
const maxCapacity = 10 * 1024 * 1024 // 10 MB
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
for scanner.Scan() {
line := scanner.Text()
linesCh <- line
}
close(linesCh)
err := scanner.Err()
if err != nil {
msg := "Failed to read TaskRunner output"
switch err.Error() {
case "EOF",
"os: process already finished",
"read |0: file already closed":
return // it is ok
case "bufio.Scanner: token too long":
msg = "TaskRunner output exceeds the maximum allowed size of 10MB"
}
t.kill() // kill the job because stdout cannot be read.
log.WithError(err).WithFields(log.Fields{
"task_id": t.Task.ID,
"context": "task_logger",
}).Error(msg)
t.Log("Fatal error: " + msg)
}
}