feat: add baseline observability for lab 6
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"messenger/src/backend/internal/faults"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"messenger/src/backend/telemetry"
|
||||
)
|
||||
|
||||
func RunPipeline(mode string, args []string) error {
|
||||
@@ -58,13 +59,22 @@ func RunPipeline(mode string, args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var signals *telemetry.Signals
|
||||
if mode == "worker" {
|
||||
var stopTelemetry func()
|
||||
signals, stopTelemetry, err = telemetry.Start("messenger-worker")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stopTelemetry()
|
||||
}
|
||||
for ctx.Err() == nil {
|
||||
broker, e := repositories.NewJobBroker(ctx, address)
|
||||
if e == nil {
|
||||
if mode == "dispatcher" {
|
||||
e = dispatchJobs(ctx, store, broker)
|
||||
} else {
|
||||
e = consumeJobs(ctx, store, objects, broker, maxAttempts, timeout)
|
||||
e = consumeJobs(ctx, store, objects, broker, maxAttempts, timeout, signals)
|
||||
}
|
||||
broker.Close()
|
||||
}
|
||||
@@ -110,7 +120,7 @@ func dispatchJobs(ctx context.Context, store *repositories.AttachmentStore, brok
|
||||
return nil
|
||||
}
|
||||
|
||||
func consumeJobs(ctx context.Context, store *repositories.AttachmentStore, objects services.AttachmentObjects, broker *repositories.JobBroker, maxAttempts int, timeout time.Duration) error {
|
||||
func consumeJobs(ctx context.Context, store *repositories.AttachmentStore, objects services.AttachmentObjects, broker *repositories.JobBroker, maxAttempts int, timeout time.Duration, signals *telemetry.Signals) error {
|
||||
deliveries, err := broker.Consume()
|
||||
if err != nil {
|
||||
return errors.New("RabbitMQ consumer unavailable")
|
||||
@@ -132,12 +142,16 @@ func consumeJobs(ctx context.Context, store *repositories.AttachmentStore, objec
|
||||
continue
|
||||
}
|
||||
task, cancel := context.WithTimeout(ctx, timeout+15*time.Second)
|
||||
task, finish := signals.Job(task, job.Type, job.AttachmentID)
|
||||
var imageErr error
|
||||
token, workErr := store.StartAttempt(task, job, timeout+15*time.Second, maxAttempts)
|
||||
if workErr == nil && token != "" {
|
||||
faults.Crash("before_effect", job.AttachmentID)
|
||||
faults.Pause(task, "after_claim", job.AttachmentID)
|
||||
workErr = store.ProcessJob(task, job, token, maxAttempts, func(a repositories.Attachment, staging string) ([]repositories.Variant, string, error) {
|
||||
return processImage(task, objects, a, staging)
|
||||
variants, code, err := processImage(task, objects, a, staging)
|
||||
imageErr = err
|
||||
return variants, code, err
|
||||
})
|
||||
}
|
||||
cancel()
|
||||
@@ -148,6 +162,14 @@ func consumeJobs(ctx context.Context, store *repositories.AttachmentStore, objec
|
||||
if workErr == nil && token == "" || errors.Is(workErr, repositories.ErrStaleJob) {
|
||||
completed, workErr = store.JobTerminal(ctx, job.ID)
|
||||
}
|
||||
outcome := "success"
|
||||
switch {
|
||||
case workErr != nil || imageErr != nil:
|
||||
outcome = "error"
|
||||
case token == "" || !completed:
|
||||
outcome = "skipped"
|
||||
}
|
||||
finish(outcome)
|
||||
// Ack after COMMIT of ready/failed or durable retry intent, or terminal no-op.
|
||||
// A closed connection automatically returns unacked deliveries to RabbitMQ.
|
||||
if completed {
|
||||
|
||||
Reference in New Issue
Block a user