feat(generation): resume manual summary jobs
This commit is contained in:
81
backend/internal/models/generation_jobs.go
Normal file
81
backend/internal/models/generation_jobs.go
Normal file
@ -0,0 +1,81 @@
|
||||
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)
|
||||
}
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -31,6 +32,28 @@ func (r *Repository) SaveGeneration(ctx context.Context, userID, id string, summ
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO summary_generations (id,user_id,summary_id,outcome,created_at) VALUES ($1,$2,$3,$4,$5)`, id, userID, summaryID, string(outcome), createdAt); err != nil {
|
||||
return err
|
||||
}
|
||||
// HTTP jobs complete atomically with their summary/outcome. Scheduled runs
|
||||
// retain the existing persistence path and do not require a job row.
|
||||
if GenerationJobID(ctx) != "" {
|
||||
var terminal struct {
|
||||
Status string `json:"status"`
|
||||
FailureCode string `json:"failure_code"`
|
||||
}
|
||||
if err = json.Unmarshal(outcome, &terminal); err != nil {
|
||||
return err
|
||||
}
|
||||
result, updateErr := tx.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, userID, terminal.Status, terminal.FailureCode)
|
||||
if updateErr != nil {
|
||||
return updateErr
|
||||
}
|
||||
n, updateErr := result.RowsAffected()
|
||||
if updateErr != nil {
|
||||
return updateErr
|
||||
}
|
||||
if n != 1 {
|
||||
return fmt.Errorf("generation job is no longer running")
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
func (r *Repository) ListGenerations(ctx context.Context, userID string, limit int) ([]GenerationHistory, error) {
|
||||
|
||||
Reference in New Issue
Block a user