forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/telemetry"
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/imageprocessor"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/pipeline"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/streamqueue"
|
||||
)
|
||||
|
||||
// workerApp composes an independently deployable worker with owned Fx lifecycle.
|
||||
func workerApp() fx.Option {
|
||||
return fx.Options(fx.StopTimeout(55*time.Second), fx.Provide(config.New, config.NewPersistence, config.NewObjectStore, config.NewWorker, loggerConfig, newLogger, workerTelemetry, observedDatabase, observedObjects, newQueue, imageprocessor.New, newWorker), fx.WithLogger(newFxLogger), fx.Invoke(startWorker, observeWorker))
|
||||
}
|
||||
|
||||
// newQueue closes the queue after the worker has relinquished its processing loop.
|
||||
func newQueue(lc fx.Lifecycle, cfg *config.Worker) (*streamqueue.Queue, error) {
|
||||
queue, err := streamqueue.New(cfg.BrokerAddress, cfg.Consumer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lc.Append(fx.Hook{OnStop: func(context.Context) error {
|
||||
return queue.Close()
|
||||
}})
|
||||
return queue, nil
|
||||
}
|
||||
|
||||
// newWorker binds concrete adapters only at the composition root.
|
||||
func newWorker(database *postgres.Store, queue *streamqueue.Queue, objects *objectstore.Client, processor *imageprocessor.Processor, log *zap.Logger, runtime *telemetry.Runtime) (*pipeline.Worker, error) {
|
||||
worker, err := pipeline.New(database, queue, objects, processor, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return worker.WithObserver(runtime), nil
|
||||
}
|
||||
|
||||
// startWorker owns the background context and joins its goroutine during shutdown.
|
||||
func startWorker(lc fx.Lifecycle, worker *pipeline.Worker) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
lc.Append(fx.Hook{OnStart: func(context.Context) error {
|
||||
go func() {
|
||||
defer close(done)
|
||||
worker.Run(ctx)
|
||||
}()
|
||||
return nil
|
||||
}, OnStop: func(stop context.Context) error {
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
return nil
|
||||
case <-stop.Done():
|
||||
return stop.Err()
|
||||
}
|
||||
}})
|
||||
}
|
||||
|
||||
// replayJob explicitly retries a failed version or advances a pipeline version.
|
||||
func replayJob(rawID, rawVersion string) error {
|
||||
id, err := uuid.Parse(rawID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid attachment ID")
|
||||
}
|
||||
version, err := strconv.Atoi(rawVersion)
|
||||
if err != nil || version < 1 {
|
||||
return fmt.Errorf("positive pipeline version required")
|
||||
}
|
||||
cfg, err := config.NewPersistence()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 5 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer database.Close()
|
||||
return database.Replay(context.Background(), id, version)
|
||||
}
|
||||
Reference in New Issue
Block a user