Streamline run log and update handling

This commit is contained in:
npc0-hue
2026-09-03 18:24:39 +08:00
parent 330b1c0130
commit 26c07e232d
3 changed files with 59 additions and 8 deletions
+16 -2
View File
@@ -32,6 +32,7 @@ const (
defaultLifecycleTimeout = 30 * time.Second
maxLifecycleTimeout = 2 * time.Hour
maxLifecycleOutputBytes = 4096
maxLifecycleLogChunk = 64 * 1024
)
var (
@@ -960,10 +961,16 @@ func (supervisor OSProcessSupervisor) Run(ctx context.Context, command ProcessCo
for key, value := range command.Env {
cmd.Env = append(cmd.Env, key+"="+value)
}
stdoutLimit := maxLifecycleOutputBytes
stderrLimit := maxLifecycleOutputBytes
if command.OutputLine != nil {
stdoutLimit = 0
stderrLimit = 0
}
var stdout bytes.Buffer
var stderr bytes.Buffer
stdoutWriter := newLifecycleOutputWriter(&stdout, maxLifecycleOutputBytes, command, "stdout")
stderrWriter := newLifecycleOutputWriter(&stderr, maxLifecycleOutputBytes, command, "stderr")
stdoutWriter := newLifecycleOutputWriter(&stdout, stdoutLimit, command, "stdout")
stderrWriter := newLifecycleOutputWriter(&stderr, stderrLimit, command, "stderr")
cmd.Stdout = stdoutWriter
cmd.Stderr = stderrWriter
if err := cmd.Start(); err != nil {
@@ -1014,6 +1021,9 @@ func (writer *lifecycleOutputWriter) Write(p []byte) (int, error) {
_, _ = writer.buffer.Write(p)
}
}
if writer.command.OutputLine == nil {
return len(p), nil
}
writer.pending += string(p)
for {
index := strings.IndexByte(writer.pending, '\n')
@@ -1024,6 +1034,10 @@ func (writer *lifecycleOutputWriter) Write(p []byte) (int, error) {
writer.pending = writer.pending[index+1:]
writer.logLine(line)
}
for len(writer.pending) >= maxLifecycleLogChunk {
writer.logLine(writer.pending[:maxLifecycleLogChunk])
writer.pending = writer.pending[maxLifecycleLogChunk:]
}
return len(p), nil
}