Stream live server logs over SSE

This commit is contained in:
npc0-hue
2026-08-03 22:28:54 +08:00
parent 5d4fca14f9
commit 7eac1926dd
48 changed files with 1526 additions and 263 deletions
+40
View File
@@ -1,7 +1,12 @@
package service
import (
"errors"
"strings"
"time"
"browser.local/platform/domain"
"browser.local/platform/repo"
"browser.local/platform/validator"
)
@@ -19,6 +24,11 @@ func (svc *CoreService) IngestLogBatch(batch domain.LogBatchIngest) (domain.LogB
stamp := svc.now()
stream, err := svc.store.LogStreams().Get(batch.LogStreamID)
if errors.Is(err, repo.ErrNotFound) {
if repairErr := svc.ensureJobLogStreamForBatch(batch, stamp); repairErr == nil {
stream, err = svc.store.LogStreams().Get(batch.LogStreamID)
}
}
if err != nil {
return domain.LogBatchIngestResult{}, err
}
@@ -76,6 +86,7 @@ func (svc *CoreService) IngestLogBatch(batch domain.LogBatchIngest) (domain.LogB
if err := svc.projectGameMapTrajectoryEvents(projectionBatch); err != nil {
return domain.LogBatchIngestResult{}, err
}
svc.publishLogEvents(stream, storedBatch.Entries)
return domain.LogBatchIngestResult{
Accepted: true,
LogStreamID: batch.LogStreamID,
@@ -86,6 +97,35 @@ func (svc *CoreService) IngestLogBatch(batch domain.LogBatchIngest) (domain.LogB
}, nil
}
func (svc *CoreService) ensureJobLogStreamForBatch(batch domain.LogBatchIngest, stamp time.Time) error {
jobID, ok := jobIDFromLogBatch(batch)
if !ok {
return repo.ErrNotFound
}
job, err := svc.store.Jobs().Get(jobID)
if err != nil {
return err
}
if job.ServerInstanceID != batch.ServerInstanceID || job.RunEndpointID != batch.RunEndpointID {
return validationError("log batch job scope does not match stream")
}
return svc.ensureJobLogStreams(job, stamp)
}
func jobIDFromLogBatch(batch domain.LogBatchIngest) (string, bool) {
streamKey := strings.TrimSpace(batch.StreamKey)
if streamKey == "" || !strings.HasPrefix(batch.LogStreamID, "job.") {
return "", false
}
suffix := "." + streamKey
body := strings.TrimPrefix(batch.LogStreamID, "job.")
if !strings.HasSuffix(body, suffix) {
return "", false
}
jobID := strings.TrimSuffix(body, suffix)
return jobID, strings.TrimSpace(jobID) != ""
}
func logBatchRecordMatches(record domain.LogBatchRecord, batch domain.LogBatchIngest) bool {
if record.Checksum == batch.Checksum {
return true