forked from mtusi2026/mtusi2026lab6
lab6
This commit is contained in:
+39
-14
@@ -16,6 +16,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -27,6 +28,7 @@ import (
|
||||
"mtusi-messenger/internal/imagecheck"
|
||||
"mtusi-messenger/internal/imageproc"
|
||||
"mtusi-messenger/internal/platform/objectstore"
|
||||
"mtusi-messenger/internal/telemetry"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
@@ -83,6 +85,7 @@ type Worker struct {
|
||||
|
||||
mu sync.RWMutex
|
||||
running bool
|
||||
busy atomic.Int64
|
||||
}
|
||||
|
||||
func New(repo Repository, store Store, config Config, log *slog.Logger) *Worker {
|
||||
@@ -140,6 +143,12 @@ func (w *Worker) setRunning(value bool) {
|
||||
w.running = value
|
||||
}
|
||||
|
||||
// Busy returns the number of worker slots currently processing a job.
|
||||
func (w *Worker) Busy() int64 { return w.busy.Load() }
|
||||
|
||||
// Capacity returns the configured worker concurrency.
|
||||
func (w *Worker) Capacity() int { return w.config.Concurrency }
|
||||
|
||||
func (w *Worker) Ready() (consumerRunning, brokerConnected bool) {
|
||||
w.mu.RLock()
|
||||
running := w.running
|
||||
@@ -160,6 +169,10 @@ func (w *Worker) handle(ctx context.Context, delivery amqp.Delivery) broker.Outc
|
||||
return broker.Drop
|
||||
}
|
||||
|
||||
ctx, span := telemetry.StartConsumerSpan(ctx, message)
|
||||
defer span.End()
|
||||
started := time.Now()
|
||||
|
||||
result, err := w.repo.ClaimJob(ctx, message, w.config.Owner, w.config.LeaseTTL)
|
||||
if err != nil {
|
||||
w.log.WarnContext(ctx, "claim job failed", slog.String("job_id", message.JobID.String()), slog.Any("error", err))
|
||||
@@ -188,14 +201,19 @@ func (w *Worker) handle(ctx context.Context, delivery amqp.Delivery) broker.Outc
|
||||
return broker.Ack
|
||||
}
|
||||
|
||||
return w.process(ctx, result)
|
||||
outcome, label := w.process(ctx, result)
|
||||
telemetry.RecordJobAttempt(ctx, message.Type, label, time.Since(started))
|
||||
|
||||
return outcome
|
||||
}
|
||||
|
||||
func (w *Worker) process(
|
||||
ctx context.Context,
|
||||
result domain.ClaimResult,
|
||||
) broker.Outcome {
|
||||
) (broker.Outcome, string) {
|
||||
job := result.Job
|
||||
w.busy.Add(1)
|
||||
defer w.busy.Add(-1)
|
||||
attachment := result.Attachment
|
||||
|
||||
failpoint.Check("WORKER_AFTER_CLAIM", job.ID)
|
||||
@@ -264,13 +282,18 @@ func (w *Worker) process(
|
||||
slog.String("job_id", job.ID.String()),
|
||||
)
|
||||
|
||||
return broker.Ack
|
||||
return broker.Ack, "superseded"
|
||||
}
|
||||
|
||||
if errors.Is(err, errCompletionCASLost) {
|
||||
return broker.Ack, "superseded"
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
failpoint.Check("WORKER_AFTER_COMMIT", job.ID)
|
||||
telemetry.RecordJobReady(ctx, job.PipelineVersion, time.Since(job.CreatedAt))
|
||||
|
||||
return broker.Ack
|
||||
return broker.Ack, "succeeded"
|
||||
}
|
||||
|
||||
return w.fail(ctx, job, err)
|
||||
@@ -384,7 +407,7 @@ func (w *Worker) runJob(
|
||||
if !completed {
|
||||
w.log.WarnContext(ctx, "completion CAS lost", slog.String("job_id", job.ID.String()))
|
||||
|
||||
return nil
|
||||
return errCompletionCASLost
|
||||
}
|
||||
|
||||
w.log.InfoContext(ctx, "attachment ready",
|
||||
@@ -451,7 +474,7 @@ func (w *Worker) spoolSource(ctx context.Context, key string) (*os.File, int64,
|
||||
return file, size, hex.EncodeToString(hasher.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func (w *Worker) fail(ctx context.Context, job domain.Job, err error) broker.Outcome {
|
||||
func (w *Worker) fail(ctx context.Context, job domain.Job, err error) (broker.Outcome, string) {
|
||||
if ctx.Err() != nil {
|
||||
// Parent cancellation means graceful shutdown. Keep the attempt number:
|
||||
// an operator restart must not consume the processing budget. The job
|
||||
@@ -465,12 +488,12 @@ func (w *Worker) fail(ctx context.Context, job domain.Job, err error) broker.Out
|
||||
time.Now().Add(time.Second), true, "shutdown",
|
||||
)
|
||||
if retryErr != nil {
|
||||
return broker.Requeue
|
||||
return broker.Requeue, "failed"
|
||||
}
|
||||
|
||||
_ = failed
|
||||
|
||||
return broker.Ack
|
||||
return broker.Ack, "shutdown"
|
||||
}
|
||||
|
||||
code := "processing_failed"
|
||||
@@ -504,13 +527,13 @@ func (w *Worker) fail(ctx context.Context, job domain.Job, err error) broker.Out
|
||||
|
||||
ok, failErr := w.repo.FailJob(ctx, job.ID, job.Attempt, w.config.Owner, code)
|
||||
if failErr != nil {
|
||||
return broker.Requeue
|
||||
return broker.Requeue, "failed"
|
||||
}
|
||||
if !ok {
|
||||
return broker.Ack
|
||||
return broker.Ack, "failed"
|
||||
}
|
||||
|
||||
return broker.Drop
|
||||
return broker.Drop, "failed"
|
||||
}
|
||||
|
||||
next := time.Now().Add(w.backoff(job.Attempt))
|
||||
@@ -525,14 +548,14 @@ func (w *Worker) fail(ctx context.Context, job domain.Job, err error) broker.Out
|
||||
ctx, job.ID, job.Attempt, w.config.Owner, code, next, false, "retry",
|
||||
)
|
||||
if retryErr != nil {
|
||||
return broker.Requeue
|
||||
return broker.Requeue, "retried"
|
||||
}
|
||||
|
||||
if failed {
|
||||
return broker.Drop
|
||||
return broker.Drop, "failed"
|
||||
}
|
||||
|
||||
return broker.Ack
|
||||
return broker.Ack, "retried"
|
||||
}
|
||||
|
||||
func (w *Worker) backoff(attempt int) time.Duration {
|
||||
@@ -559,6 +582,8 @@ func (w *Worker) backoff(attempt int) time.Duration {
|
||||
return time.Duration(rand.Int64N(int64(base))) + time.Millisecond
|
||||
}
|
||||
|
||||
var errCompletionCASLost = errors.New("completion CAS lost")
|
||||
|
||||
type permanentError struct {
|
||||
code string
|
||||
err error
|
||||
|
||||
Reference in New Issue
Block a user