Добавить наблюдаемость и проверки отказов для ЛР 6

This commit is contained in:
2026-11-26 18:20:32 +03:00
parent 1d81159dd3
commit 9db37405fb
46 changed files with 7878 additions and 173 deletions
+14 -4
View File
@@ -10,11 +10,16 @@ import (
"mtusi2026/internal/idgen"
"mtusi2026/internal/model"
"mtusi2026/internal/telemetry"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
const JobType = "attachment.process.v1"
type Job struct {
SchemaVersion int `json:"schema_version"`
ID string `json:"job_id"`
@@ -38,7 +43,9 @@ type Work struct {
func Enqueue(ctx context.Context, tx pgx.Tx, attachmentID string) error {
id := idgen.GenerateUUIDv4()
_, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id) VALUES($1,$2)`, id, attachmentID)
carrier := telemetry.Inject(ctx)
_, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,created_at,traceparent,tracestate)
VALUES($1,$2,clock_timestamp(),NULLIF($3,''),NULLIF($4,''))`, id, attachmentID, carrier.Get("traceparent"), carrier.Get("tracestate"))
if err != nil {
return err
}
@@ -57,7 +64,7 @@ func (r Repo) Claim(ctx context.Context, j Job) (*Work, error) {
lease_token=$3,lease_until=now()+$4::interval,updated_at=now()
WHERE id=$1 AND attachment_id=$2 AND pipeline_version=$5 AND status='queued'
AND next_attempt_at<=now() AND attempt=$6-1
RETURNING attempt`, j.ID, j.AttachmentID, w.Token, r.Lease.String(), j.PipelineVersion, j.Attempt).Scan(&w.Attempt)
RETURNING attempt,created_at`, j.ID, j.AttachmentID, w.Token, r.Lease.String(), j.PipelineVersion, j.Attempt).Scan(&w.Attempt, &w.CreatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
@@ -76,7 +83,10 @@ func (r Repo) Claim(ctx context.Context, j Job) (*Work, error) {
return w, nil
}
func (r Repo) Complete(ctx context.Context, w *Work, variants []model.Variant) (bool, error) {
func (r Repo) Complete(ctx context.Context, w *Work, variants []model.Variant) (_ bool, err error) {
ctx, span := telemetry.Tracer.Start(ctx, "db.job.complete",
trace.WithAttributes(attribute.String("db.system.name", "postgresql")))
defer func() { telemetry.Finish(span, err) }()
tx, err := r.Pool.Begin(ctx)
if err != nil {
return false, err
@@ -247,7 +257,7 @@ func (r Repo) Failed(ctx context.Context) ([]map[string]any, error) {
}
func (j Job) Validate() error {
if j.SchemaVersion != 1 || j.Type != "attachment.process.v1" || j.PipelineVersion != 1 || j.Attempt < 1 || !idgen.IsValidUUID(j.ID) || !idgen.IsValidUUID(j.AttachmentID) || j.CreatedAt.IsZero() {
if j.SchemaVersion != 1 || j.Type != JobType || j.PipelineVersion != 1 || j.Attempt < 1 || !idgen.IsValidUUID(j.ID) || !idgen.IsValidUUID(j.AttachmentID) || j.CreatedAt.IsZero() {
return fmt.Errorf("invalid job envelope")
}
return nil