refactor moving status to pipeline not daemon
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type Status string
|
||||
type Action string
|
||||
|
||||
const (
|
||||
Idle Status = "idle"
|
||||
@@ -18,21 +19,19 @@ const (
|
||||
Injecting Status = "injecting"
|
||||
)
|
||||
|
||||
// Action represents commands sent to the pipeline to drive state
|
||||
// transitions or trigger work. Keeping this separate from Status
|
||||
// preserves directionality: callers send Actions, pipeline emits Statuses.
|
||||
type Action int
|
||||
|
||||
const (
|
||||
Inject Action = iota
|
||||
Inject Action = "inject"
|
||||
)
|
||||
|
||||
type Pipeline interface {
|
||||
Run(ctx context.Context) (<-chan Status, chan<- Action)
|
||||
Run(ctx context.Context)
|
||||
Stop()
|
||||
Status() Status
|
||||
Actions() chan<- Action
|
||||
}
|
||||
|
||||
type pipeline struct {
|
||||
status Status
|
||||
actionCh chan Action
|
||||
wg sync.WaitGroup
|
||||
cancel context.CancelFunc
|
||||
@@ -44,74 +43,75 @@ func New() Pipeline {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pipeline) Run(ctx context.Context) (<-chan Status, chan<- Action) {
|
||||
statusCh := make(chan Status, 1)
|
||||
func (p *pipeline) Status() Status {
|
||||
return p.status
|
||||
}
|
||||
|
||||
func (p *pipeline) Actions() chan<- Action {
|
||||
return p.actionCh
|
||||
}
|
||||
|
||||
func (p *pipeline) Stop() {
|
||||
if p.cancel != nil {
|
||||
p.cancel()
|
||||
}
|
||||
p.wg.Wait()
|
||||
}
|
||||
|
||||
func (p *pipeline) Run(ctx context.Context) {
|
||||
runCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
p.cancel = cancel
|
||||
p.wg.Add(1)
|
||||
go p.run(runCtx, statusCh)
|
||||
return statusCh, p.actionCh
|
||||
go p.run(runCtx)
|
||||
}
|
||||
|
||||
func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
||||
func (p *pipeline) run(ctx context.Context) {
|
||||
defer func() {
|
||||
// Ensure status channel is closed and wg is decremented
|
||||
close(statusCh)
|
||||
p.wg.Done()
|
||||
}()
|
||||
|
||||
log.Printf("Pipeline: Starting recording")
|
||||
statusCh <- Recording
|
||||
p.status = Recording
|
||||
|
||||
recorder := recording.NewDefaultRecorder()
|
||||
frameCh, errCh, err := recorder.Start(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Pipeline: Recording error: %v", err)
|
||||
statusCh <- Idle
|
||||
p.status = Idle
|
||||
return
|
||||
}
|
||||
defer recorder.Stop()
|
||||
|
||||
// Track statistics for logging
|
||||
frameCount := 0
|
||||
totalBytes := 0
|
||||
startTime := time.Now()
|
||||
|
||||
// Main event loop
|
||||
for {
|
||||
select {
|
||||
case frame, ok := <-frameCh:
|
||||
if !ok {
|
||||
// Frame channel closed, recording ended
|
||||
log.Printf("Pipeline: Recording ended. Total frames: %d, Total bytes: %d, Duration: %v",
|
||||
frameCount, totalBytes, time.Since(startTime))
|
||||
statusCh <- Transcribing
|
||||
|
||||
return
|
||||
}
|
||||
// Log frame details
|
||||
case frame := <-frameCh:
|
||||
frameCount++
|
||||
totalBytes += len(frame.Data)
|
||||
log.Printf("Pipeline: Received frame #%d - Size: %d bytes, Timestamp: %v, Total bytes so far: %d",
|
||||
frameCount, len(frame.Data), frame.Timestamp.Format("15:04:05.000"), totalBytes)
|
||||
// TODO: pass this channel to the transcriber
|
||||
|
||||
p.status = Transcribing
|
||||
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
log.Printf("Pipeline: Recording error: %v", err)
|
||||
statusCh <- Idle
|
||||
p.status = Idle
|
||||
return
|
||||
}
|
||||
|
||||
case action := <-p.actionCh:
|
||||
log.Printf("Pipeline: Received action: %v", action)
|
||||
if action == Inject {
|
||||
log.Printf("Pipeline: Inject action received, stopping recording")
|
||||
switch action {
|
||||
case Inject:
|
||||
log.Printf("Pipeline: Inject action received, stopping recording (TODO: stop transcribing)")
|
||||
|
||||
if err := recorder.Stop(); err != nil {
|
||||
log.Printf("Pipeline: Error stopping recorder: %v", err)
|
||||
}
|
||||
statusCh <- Injecting
|
||||
p.status = Injecting
|
||||
return
|
||||
}
|
||||
|
||||
@@ -121,10 +121,3 @@ func (p *pipeline) run(ctx context.Context, statusCh chan<- Status) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pipeline) Stop() {
|
||||
if p.cancel != nil {
|
||||
p.cancel()
|
||||
}
|
||||
p.wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user