Разрешите отправку сообщения во время публикации вложения

This commit is contained in:
2026-11-20 10:54:34 +03:00
parent c6a5e9ee47
commit bdc072aab7
3 changed files with 90 additions and 9 deletions
+6 -5
View File
@@ -18,13 +18,14 @@ func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} }
// The callback waits for a routed publisher confirm with a finite timeout.
// Losing that confirm or the SQL commit leaves the same envelope deliverable.
// NO KEY UPDATE excludes other publishers but allows messages to link a queued attachment.
func (s *Store) PublishOne(ctx context.Context, publish func(context.Context, Job) error) (bool, error) {
var found bool
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
j := Job{SchemaVersion: 1, Type: "attachment.process.v1"}
err := tx.QueryRow(ctx, `SELECT job_id,attachment_id,pipeline_version,attempt,created_at
FROM attachment_jobs WHERE state='queued' AND published_at IS NULL AND available_at<=clock_timestamp()
ORDER BY available_at,job_id LIMIT 1 FOR UPDATE SKIP LOCKED`).
ORDER BY available_at,job_id LIMIT 1 FOR NO KEY UPDATE SKIP LOCKED`).
Scan(&j.JobID, &j.AttachmentID, &j.PipelineVersion, &j.Attempt, &j.CreatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil
@@ -60,12 +61,12 @@ func (s *Store) Claim(ctx context.Context, j Job, lease time.Duration) (*Claim,
RETURNING j.source_key,f.hash,f.size_bytes,f.mime_type`,
j.JobID, j.AttachmentID, j.PipelineVersion, j.Attempt, j.CreatedAt, c.Token, lease.Microseconds()).
Scan(&c.SourceKey, &c.Hash, &c.SizeBytes, &c.MIMEType)
if !errors.Is(err, pgx.ErrNoRows) {
if err != nil {
return nil, err
}
if err == nil {
return &c, nil
}
if !errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
var incomplete bool
err = s.pool.QueryRow(ctx, `SELECT EXISTS (
SELECT 1 FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id