package models import ( "context" "database/sql" "encoding/json" "time" ) type GenerationJob struct { ID string `json:"id"` Status string `json:"status"` FailureCode string `json:"failure_code,omitempty"` CreatedAt time.Time `json:"created_at"` FinishedAt *time.Time `json:"finished_at"` SummaryID *string `json:"summary_id"` Outcome json.RawMessage `json:"outcome"` UserID string `json:"-"` } type jobContextKey struct{} func WithGenerationJob(ctx context.Context, id string) context.Context { return context.WithValue(ctx, jobContextKey{}, id) } func GenerationJobID(ctx context.Context) string { id, _ := ctx.Value(jobContextKey{}).(string) return id } func (r *Repository) CreateGenerationJob(ctx context.Context, j GenerationJob) (GenerationJob, bool, error) { // The partial unique index serializes concurrent starts for a user. for { result, err := r.db.ExecContext(ctx, `INSERT INTO summary_generation_jobs(id,user_id,status,created_at) VALUES($1,$2,'running',$3) ON CONFLICT (user_id) WHERE status='running' DO NOTHING`, j.ID, j.UserID, j.CreatedAt) if err != nil { return GenerationJob{}, false, err } n, err := result.RowsAffected() if err != nil { return GenerationJob{}, false, err } if n == 1 { return j, true, nil } existing, err := r.findGenerationJob(ctx, j.UserID, " AND j.status='running'", nil) if err != nil { return GenerationJob{}, false, err } if existing != nil { return *existing, false, nil } // A concurrent completion removed the conflict: retry the insertion. } } func (r *Repository) FinishGenerationJob(ctx context.Context, id, user, status, code string) error { _, err := r.db.ExecContext(ctx, `UPDATE summary_generation_jobs SET status=$3,failure_code=$4,finished_at=NOW() WHERE id=$1 AND user_id=$2 AND status='running'`, id, user, status, code) return err } func (r *Repository) RecoverGenerationJobs(ctx context.Context) error { _, err := r.db.ExecContext(ctx, `UPDATE summary_generation_jobs SET status='failed',failure_code='interrupted',finished_at=NOW() WHERE status='running'`) return err } func (r *Repository) findGenerationJob(ctx context.Context, user, filter string, id *string) (*GenerationJob, error) { args := []any{user} if id != nil { args = append(args, *id) } j := &GenerationJob{} var outcome []byte err := r.db.QueryRowContext(ctx, `SELECT j.id,j.status,j.failure_code,j.created_at,j.finished_at,g.summary_id,g.outcome FROM summary_generation_jobs j LEFT JOIN summary_generations g ON g.id=j.id AND g.user_id=j.user_id WHERE j.user_id=$1`+filter+` ORDER BY (j.status='running') DESC,j.created_at DESC,j.id DESC LIMIT 1`, args...).Scan(&j.ID, &j.Status, &j.FailureCode, &j.CreatedAt, &j.FinishedAt, &j.SummaryID, &outcome) if err == sql.ErrNoRows { return nil, nil } j.Outcome = json.RawMessage(outcome) return j, err } func (r *Repository) GetGenerationJob(ctx context.Context, user, id string) (*GenerationJob, error) { return r.findGenerationJob(ctx, user, " AND j.id=$2", &id) } func (r *Repository) RecentGenerationJob(ctx context.Context, user string) (*GenerationJob, error) { return r.findGenerationJob(ctx, user, "", nil) }