forked from mtusi2026/mtusi2026lab6
lab6
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/telemetry"
|
||||
)
|
||||
|
||||
// PipelineRepository owns the durable job/outbox/variant state. Queries that
|
||||
@@ -357,13 +358,15 @@ func insertOutboxTx(
|
||||
kind string,
|
||||
availableAt time.Time,
|
||||
) error {
|
||||
payload, err := domain.NewJobMessage(domain.Job{
|
||||
message := domain.NewJobMessage(domain.Job{
|
||||
ID: job.ID,
|
||||
AttachmentID: job.AttachmentID,
|
||||
PipelineVersion: job.PipelineVersion,
|
||||
Attempt: attempt,
|
||||
CreatedAt: job.CreatedAt,
|
||||
}).MarshalPayload()
|
||||
})
|
||||
message.Traceparent, message.Tracestate = telemetry.Inject(ctx)
|
||||
payload, err := message.MarshalPayload()
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal job message: %w", err)
|
||||
}
|
||||
@@ -1158,6 +1161,37 @@ func (r *PipelineRepository) ReapExpiredLeases(
|
||||
return reaped, nil
|
||||
}
|
||||
|
||||
// CountOverdueJobs returns queued/processing jobs older than the TTR objective.
|
||||
func (r *PipelineRepository) CountOverdueJobs(ctx context.Context, olderThan time.Duration) (int64, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var count int64
|
||||
if err := r.store.pool.QueryRow(ctx, `
|
||||
SELECT count(*) FROM attachment_jobs
|
||||
WHERE status IN ('queued', 'processing')
|
||||
AND created_at < now() - $1::interval`, intervalString(olderThan)).Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("count overdue jobs: %w", mapError(err))
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// OldestActiveJobAge returns the age of the oldest queued/processing job.
|
||||
// It is intentionally a small read-only query with the standard query timeout.
|
||||
func (r *PipelineRepository) OldestActiveJobAge(ctx context.Context) (float64, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var seconds float64
|
||||
if err := r.store.pool.QueryRow(ctx, `
|
||||
SELECT COALESCE(EXTRACT(EPOCH FROM (now() - min(created_at))), 0)
|
||||
FROM attachment_jobs
|
||||
WHERE status IN ('queued', 'processing')`).Scan(&seconds); err != nil {
|
||||
return 0, fmt.Errorf("oldest active job age: %w", mapError(err))
|
||||
}
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
func (r *PipelineRepository) ClaimOutbox(
|
||||
ctx context.Context,
|
||||
owner string,
|
||||
@@ -2022,6 +2056,7 @@ func (r *PipelineRepository) ReconcileQueuedJobs(
|
||||
ctx context.Context,
|
||||
olderThan time.Duration,
|
||||
limit int,
|
||||
traceparent, tracestate string,
|
||||
) (int, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
@@ -2031,7 +2066,7 @@ func (r *PipelineRepository) ReconcileQueuedJobs(
|
||||
SELECT j.job_id, j.attachment_id, j.pipeline_version, j.attempt, j.created_at
|
||||
FROM attachment_jobs j
|
||||
WHERE j.status = 'queued'
|
||||
AND j.available_at < now() - $1::interval
|
||||
AND j.available_at <= now() - $1::interval
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM outbox_messages o
|
||||
WHERE o.job_id = j.job_id AND o.published_at IS NULL
|
||||
@@ -2049,10 +2084,15 @@ func (r *PipelineRepository) ReconcileQueuedJobs(
|
||||
'attachment_id', attachment_id,
|
||||
'pipeline_version', pipeline_version,
|
||||
'attempt', attempt,
|
||||
'created_at', created_at),
|
||||
'created_at', created_at)
|
||||
|| CASE
|
||||
WHEN $4::text = '' THEN '{}'::jsonb
|
||||
WHEN COALESCE($5::text, '') = '' THEN jsonb_build_object('traceparent', $4::text)
|
||||
ELSE jsonb_build_object('traceparent', $4::text, 'tracestate', $5::text)
|
||||
END,
|
||||
now()
|
||||
FROM lost`,
|
||||
intervalString(olderThan), limit, defaultRoutingKey)
|
||||
intervalString(olderThan), limit, defaultRoutingKey, traceparent, tracestate)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reconcile queued jobs: %w", mapError(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user