mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
refactor: simplify journal logs following, fix corrosion unit name
This commit is contained in:
@@ -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.
|
||||
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)
|
||||
|
||||
for scanner.Scan() {
|
||||
select {
|
||||
case outCh <- entry(scanner.Bytes()):
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
outCh <- api.LogEntry{Err: fmt.Errorf("journal logs: %w", err)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package journal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
@@ -16,7 +15,7 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan
|
||||
// Hard code unit check for now
|
||||
switch unit {
|
||||
case "uncloud":
|
||||
case "corrosion":
|
||||
case "uncloud-corrosion":
|
||||
case "docker":
|
||||
default:
|
||||
return nil, fmt.Errorf("journal logs: invalid unit: %s", unit)
|
||||
@@ -29,32 +28,10 @@ func Logs(ctx context.Context, unit string, opts api.ServiceLogsOptions) (<-chan
|
||||
|
||||
outCh := make(chan api.LogEntry)
|
||||
|
||||
switch opts.Follow {
|
||||
case false:
|
||||
|
||||
go func() {
|
||||
defer close(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)}
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
defer close(outCh)
|
||||
follow(ctx, reader, outCh)
|
||||
}()
|
||||
|
||||
return outCh, nil
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ import (
|
||||
)
|
||||
|
||||
func TestLogs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, "/usr/bin/tail", "testdata/logs")
|
||||
}
|
||||
@@ -29,8 +27,7 @@ func TestLogs(t *testing.T) {
|
||||
for range ch {
|
||||
i++
|
||||
}
|
||||
assert.Equal(t, i, 6)
|
||||
cancel()
|
||||
assert.Equal(t, 6, i)
|
||||
|
||||
commandContext = func(ctx context.Context, _ string, _ ...string) *exec.Cmd {
|
||||
return exec.CommandContext(ctx, "/usr/bin/tail", "-f", "testdata/logs")
|
||||
@@ -47,5 +44,6 @@ func TestLogs(t *testing.T) {
|
||||
for range ch {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user