refactor: simplify journal logs following, fix corrosion unit name

This commit is contained in:
Pasha Sviderski
2026-04-08 19:38:02 +10:00
parent e96cce88c1
commit 943fea0515
3 changed files with 11 additions and 36 deletions
+3 -3
View File
@@ -52,18 +52,18 @@ func logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (io.Rea
} }
// follow synchronously follows the io.Reader, writing each new journal entry to channel. // follow synchronously follows the io.Reader, writing each new journal entry to channel.
func follow(ctx context.Context, reader io.Reader, outCh chan api.LogEntry) error { // It stops when the reader is exhausted or the context is cancelled.
func follow(ctx context.Context, reader io.Reader, outCh chan api.LogEntry) {
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
select { select {
case outCh <- entry(scanner.Bytes()): case outCh <- entry(scanner.Bytes()):
case <-ctx.Done(): case <-ctx.Done():
return nil return
} }
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)} outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)}
} }
return nil
} }
+2 -25
View File
@@ -1,7 +1,6 @@
package journal package journal
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
@@ -16,7 +15,7 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan
// Hard code unit check for now // Hard code unit check for now
switch unit { switch unit {
case "uncloud": case "uncloud":
case "corrosion": case "uncloud-corrosion":
case "docker": case "docker":
default: default:
return nil, fmt.Errorf("journal logs: invalid unit: %s", unit) return nil, fmt.Errorf("journal logs: invalid unit: %s", unit)
@@ -29,33 +28,11 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan
outCh := make(chan api.LogEntry) outCh := make(chan api.LogEntry)
switch opts.Follow {
case false:
go func() { go func() {
defer close(outCh) defer close(outCh)
follow(ctx, reader, outCh)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
outCh <- entry(scanner.Bytes())
}
if err := scanner.Err(); err != nil {
outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)}
}
}() }()
case true:
go func() {
defer close(outCh)
err := follow(ctx, reader, outCh)
if err != nil {
outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)}
}
}()
}
return outCh, nil return outCh, nil
} }
+3 -5
View File
@@ -12,8 +12,6 @@ import (
) )
func TestLogs(t *testing.T) { func TestLogs(t *testing.T) {
t.Parallel()
commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd { commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd {
return exec.CommandContext(ctx, "/usr/bin/tail", "testdata/logs") return exec.CommandContext(ctx, "/usr/bin/tail", "testdata/logs")
} }
@@ -29,8 +27,7 @@ func TestLogs(t *testing.T) {
for range ch { for range ch {
i++ i++
} }
assert.Equal(t, i, 6) assert.Equal(t, 6, i)
cancel()
commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd { commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd {
return exec.CommandContext(ctx, "/usr/bin/tail", "-f", "testdata/logs") return exec.CommandContext(ctx, "/usr/bin/tail", "-f", "testdata/logs")
@@ -47,5 +44,6 @@ func TestLogs(t *testing.T) {
for range ch { for range ch {
i++ i++
} }
assert.Equal(t, i, 6) // still six is hardbeats are not written here. // Still six because heartbeats are not written here and Tail is ignored as the command is overridden.
assert.Equal(t, 6, i)
} }