Skip to content

Commit 3d5a7f3

Browse files
committed
feat: Worker 日志中存储 context 信息
1 parent 6cdd314 commit 3d5a7f3

6 files changed

Lines changed: 49 additions & 16 deletions

File tree

internal/db/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS execution_logs (
2525
worker_id TEXT NOT NULL,
2626
request_id TEXT NOT NULL,
2727
status INTEGER,
28+
stdin TEXT,
2829
stdout TEXT,
2930
stderr TEXT,
3031
result TEXT,

internal/db/store.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ func Open(dbPath string) (*Store, error) {
3939
database.Close()
4040
return nil, fmt.Errorf("执行迁移失败: %w", err)
4141
}
42-
if err = ensureExecutionLogsResultColumn(database); err != nil {
42+
if err = ensureExecutionLogsColumn(database, "result"); err != nil {
4343
database.Close()
4444
return nil, fmt.Errorf("升级 execution_logs.result 列失败: %w", err)
4545
}
46+
if err = ensureExecutionLogsColumn(database, "stdin"); err != nil {
47+
database.Close()
48+
return nil, fmt.Errorf("升级 execution_logs.stdin 列失败: %w", err)
49+
}
4650
store := &Store{db: database}
4751
store.AppConfig = &AppConfigDao{store: database}
4852
return store, nil
@@ -200,12 +204,13 @@ func (s *Store) SetWorkerEnabled(ctx context.Context, id string, enabled bool) (
200204
// InsertWorkerLog 写入函数执行日志。
201205
func (s *Store) InsertWorkerLog(ctx context.Context, log model.WorkerLog) error {
202206
_, err := s.db.ExecContext(ctx, `
203-
INSERT INTO execution_logs(id, worker_id, request_id, status, stdout, stderr, result, error, duration_ms)
204-
VALUES(?,?,?,?,?,?,?,?,?)`,
207+
INSERT INTO execution_logs(id, worker_id, request_id, status, stdin, stdout, stderr, result, error, duration_ms)
208+
VALUES(?,?,?,?,?,?,?,?,?,?)`,
205209
log.ID,
206210
log.WorkerID,
207211
log.RequestID,
208212
log.Status,
213+
log.Stdin,
209214
log.Stdout,
210215
log.Stderr,
211216
log.Result,
@@ -234,7 +239,7 @@ WHERE worker_id = ?`, workerID).Scan(&total); err != nil {
234239

235240
offset := (page - 1) * pageSize
236241
rows, err := s.db.QueryContext(ctx, `
237-
SELECT id, worker_id, request_id, status, stdout, stderr, result, error, duration_ms, created_at
242+
SELECT id, worker_id, request_id, status, stdin, stdout, stderr, result, error, duration_ms, created_at
238243
FROM execution_logs
239244
WHERE worker_id = ?
240245
ORDER BY created_at DESC, id DESC
@@ -246,13 +251,15 @@ LIMIT ? OFFSET ?`, workerID, pageSize, offset)
246251

247252
result := make([]model.WorkerLog, 0, pageSize)
248253
for rows.Next() {
254+
var stdinText sql.NullString
249255
var resultText sql.NullString
250256
var item model.WorkerLog
251257
if err := rows.Scan(
252258
&item.ID,
253259
&item.WorkerID,
254260
&item.RequestID,
255261
&item.Status,
262+
&stdinText,
256263
&item.Stdout,
257264
&item.Stderr,
258265
&resultText,
@@ -262,6 +269,11 @@ LIMIT ? OFFSET ?`, workerID, pageSize, offset)
262269
); err != nil {
263270
return nil, 0, err
264271
}
272+
if stdinText.Valid {
273+
item.Stdin = stdinText.String
274+
} else {
275+
item.Stdin = ""
276+
}
265277
if resultText.Valid {
266278
item.Result = resultText.String
267279
} else {
@@ -310,7 +322,7 @@ func boolToInt(v bool) int {
310322
return 0
311323
}
312324

313-
func ensureExecutionLogsResultColumn(database *sql.DB) error {
325+
func ensureExecutionLogsColumn(database *sql.DB, columnName string) error {
314326
rows, err := database.Query(`PRAGMA table_info(execution_logs)`)
315327
if err != nil {
316328
return err
@@ -329,14 +341,14 @@ func ensureExecutionLogsResultColumn(database *sql.DB) error {
329341
if err := rows.Scan(&cid, &name, &colType, &notNull, &defaultV, &primaryID); err != nil {
330342
return err
331343
}
332-
if name == "result" {
344+
if name == columnName {
333345
return nil
334346
}
335347
}
336348
if err := rows.Err(); err != nil {
337349
return err
338350
}
339351

340-
_, err = database.Exec(`ALTER TABLE execution_logs ADD COLUMN result TEXT`)
352+
_, err = database.Exec(fmt.Sprintf(`ALTER TABLE execution_logs ADD COLUMN %s TEXT`, columnName))
341353
return err
342354
}

internal/model/worker_log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type WorkerLog struct {
88
WorkerID string `json:"worker_id"`
99
RequestID string `json:"request_id"`
1010
Status int `json:"status"`
11+
Stdin string `json:"stdin"`
1112
Stdout string `json:"stdout"`
1213
Stderr string `json:"stderr"`
1314
Result string `json:"result"`

internal/router/server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package router
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"io"
78
"io/fs"
@@ -104,7 +105,7 @@ func (s *Server) handleInvoke(c *gin.Context) {
104105

105106
workerDir := filepath.Join(s.dataDir, "workers", fn.ID)
106107
execResult := executor.Run(timeoutCtx, fn, workerDir, input)
107-
s.recordRunningLog(fn.ID, requestID, execResult)
108+
s.recordRunningLog(fn.ID, requestID, input, execResult)
108109

109110
if execResult.TimedOut {
110111
common.ErrorResponse(c, http.StatusGatewayTimeout, "execution timeout")
@@ -347,7 +348,7 @@ func (s *Server) insertWorkerLogAsync(entry model.WorkerLog) {
347348
}
348349

349350
// recordRunningLog 统一整理执行结果并异步记录 Worker 运行日志。
350-
func (s *Server) recordRunningLog(workerID string, requestID string, execResult executor.ExecuteResult) {
351+
func (s *Server) recordRunningLog(workerID string, requestID string, input model.WorkerInput, execResult executor.ExecuteResult) {
351352
statusForLog := execResult.Status
352353
if statusForLog == 0 {
353354
if execResult.TimedOut {
@@ -366,11 +367,20 @@ func (s *Server) recordRunningLog(workerID string, requestID string, execResult
366367
}
367368
}
368369

370+
stdinText := ""
371+
payload, err := json.Marshal(input)
372+
if err != nil {
373+
log.Printf("序列化 WorkerInput 失败: %v", err)
374+
} else {
375+
stdinText = string(payload)
376+
}
377+
369378
s.insertWorkerLogAsync(model.WorkerLog{
370379
ID: uuid.NewString(),
371380
WorkerID: workerID,
372381
RequestID: requestID,
373382
Status: statusForLog,
383+
Stdin: stdinText,
374384
Stdout: execResult.Stdout,
375385
Stderr: execResult.Stderr,
376386
Result: execResult.Result,

migrations/001_init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CREATE TABLE execution_logs (
1515
worker_id TEXT NOT NULL,
1616
request_id TEXT NOT NULL,
1717
status INTEGER,
18+
stdin TEXT,
1819
stdout TEXT,
1920
stderr TEXT,
2021
result TEXT,

pages/src/components/worker-log-list.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type WorkerLogItem = {
55
worker_id: string;
66
request_id: string;
77
status: number;
8+
stdin: string;
89
stdout: string;
910
stderr: string;
1011
result: string;
@@ -71,6 +72,7 @@ export default function WorkerLogList({
7172
items.map((item) => {
7273
const expanded = expandedIds.has(item.id);
7374
const formattedResult = formatResultForDisplay(item.result);
75+
const formattedStdin = formatResultForDisplay(item.stdin);
7476
return (
7577
<div key={item.id} className="rounded-lg border border-default-200 p-3">
7678
<div className="flex items-start justify-between gap-3" onClick={() => onToggle(item.id)}>
@@ -98,19 +100,25 @@ export default function WorkerLogList({
98100
<pre className="max-h-40 overflow-auto rounded-md bg-warning-50 p-2 font-mono text-xs whitespace-pre-wrap break-words text-warning-700">{item.stderr}</pre>
99101
</div>
100102
) : null}
101-
{item.stdout ? (
102-
<div>
103-
<p className="mb-1 text-xs font-medium text-success">stdout</p>
104-
<pre className="max-h-40 overflow-auto rounded-md bg-success-50 p-2 font-mono text-xs whitespace-pre-wrap break-words text-success-700">{item.stdout}</pre>
105-
</div>
106-
) : null}
107103
{item.result ? (
108104
<div>
109105
<p className="mb-1 text-xs font-medium text-primary">result</p>
110106
<pre className="max-h-40 overflow-auto rounded-md bg-primary-50 p-2 font-mono text-xs whitespace-pre-wrap break-words text-primary-700">{formattedResult}</pre>
111107
</div>
112108
) : null}
113-
{!item.error && !item.stderr && !item.stdout && !item.result ? (
109+
{item.stdin ? (
110+
<div>
111+
<p className="mb-1 text-xs font-medium text-secondary">stdin</p>
112+
<pre className="max-h-40 overflow-auto rounded-md bg-secondary-50 p-2 font-mono text-xs whitespace-pre-wrap break-words text-secondary-700">{formattedStdin}</pre>
113+
</div>
114+
) : null}
115+
{item.stdout ? (
116+
<div>
117+
<p className="mb-1 text-xs font-medium text-default-700">stdout</p>
118+
<pre className="max-h-40 overflow-auto rounded-md bg-default-100 p-2 font-mono text-xs whitespace-pre-wrap break-words text-default-800">{item.stdout}</pre>
119+
</div>
120+
) : null}
121+
{!item.error && !item.stderr && !item.result && !item.stdin && !item.stdout ? (
114122
<p className="text-xs text-default-400">无详细输出</p>
115123
) : null}
116124
</div>

0 commit comments

Comments
 (0)