forked from longtao.wu/droboticflow
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func (runtime *Runtime) reconcileCaptureStatuses(ctx context.Context) error {
|
|
runtime.captureStatusMu.Lock()
|
|
defer runtime.captureStatusMu.Unlock()
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
records, err := runtime.store.ListCaptureStatusPage(ctx, runtime.captureStatusCursor, 50)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var failures []error
|
|
for _, record := range records {
|
|
if err := ctx.Err(); err != nil {
|
|
return errors.Join(append(failures, err)...)
|
|
}
|
|
switch record.Status {
|
|
case domain.CaptureRecordLaunching, domain.CaptureRecordQueued, domain.CaptureRecordRunning, domain.CaptureRecordWaiting:
|
|
if err := runtime.reconcileCaptureRun(ctx, &record); err != nil {
|
|
failures = append(failures, fmt.Errorf("capture %s: %w", record.ID, err))
|
|
}
|
|
}
|
|
runtime.captureStatusCursor = record.ID
|
|
if ctx.Err() != nil {
|
|
return errors.Join(append(failures, ctx.Err())...)
|
|
}
|
|
}
|
|
if len(records) < 50 {
|
|
runtime.captureStatusCursor = ""
|
|
}
|
|
return errors.Join(failures...)
|
|
}
|