feat: use sqlite logstore (#514)

This commit is contained in:
Gareth
2024-10-12 11:26:22 -07:00
committed by GitHub
parent 5948c67652
commit 4d557a1146
22 changed files with 1080 additions and 896 deletions

View File

@@ -3,7 +3,6 @@ package orchestrator
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
@@ -11,7 +10,6 @@ import (
v1 "github.com/garethgeorge/backrest/gen/go/v1"
"github.com/garethgeorge/backrest/internal/hook"
"github.com/garethgeorge/backrest/internal/logwriter"
"github.com/garethgeorge/backrest/internal/oplog"
"github.com/garethgeorge/backrest/internal/orchestrator/logging"
"github.com/garethgeorge/backrest/internal/orchestrator/repo"
@@ -162,38 +160,12 @@ func (t *taskRunnerImpl) Logger(ctx context.Context) *zap.Logger {
return logging.Logger(ctx, "[tasklog] ").Named(t.t.Name())
}
func (t *taskRunnerImpl) LogrefWriter() (string, tasks.LogrefWriter, error) {
id := make([]byte, 16)
if _, err := rand.Read(id); err != nil {
return "", nil, fmt.Errorf("read random: %w", err)
func (t *taskRunnerImpl) LogrefWriter() (string, io.WriteCloser, error) {
randBytes := make([]byte, 8)
if _, err := rand.Read(randBytes); err != nil {
return "", nil, err
}
idStr := hex.EncodeToString(id)
liveID, writer, err := t.orchestrator.logStore.NewLiveWriter(idStr)
if err != nil {
return "", nil, fmt.Errorf("new log writer: %w", err)
}
return liveID, &logrefWriter{
logmgr: t.orchestrator.logStore,
id: liveID,
writer: writer,
}, nil
}
type logrefWriter struct {
logmgr *logwriter.LogManager
id string
writer io.WriteCloser
}
var _ tasks.LogrefWriter = &logrefWriter{}
func (l *logrefWriter) Write(p []byte) (n int, err error) {
return l.writer.Write(p)
}
func (l *logrefWriter) Close() (string, error) {
if err := l.writer.Close(); err != nil {
return "", err
}
return l.logmgr.Finalize(l.id)
id := fmt.Sprintf("op%d-logref-%x", t.op.Id, randBytes)
writer, err := t.orchestrator.logStore.Create(id, t.op.GetId(), time.Duration(0))
return id, writer, err
}