ocr is working
This commit is contained in:
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// OcrJobRow maps ocr_jobs.
|
||||
@@ -18,6 +19,9 @@ type OcrJobRow struct {
|
||||
// ErrJobNotFound marks an OCR job absent or owned by another device.
|
||||
var ErrJobNotFound = errors.New("ocr job not found")
|
||||
|
||||
// ErrNoQueuedJobs marks an OCR queue drained (no job to claim).
|
||||
var ErrNoQueuedJobs = errors.New("no queued ocr jobs")
|
||||
|
||||
type OcrJobs struct{ DB *sql.DB }
|
||||
|
||||
func (o *OcrJobs) Create(jobID, deviceID, fileID string) error {
|
||||
@@ -62,6 +66,49 @@ func (o *OcrJobs) TouchProcessing(deviceID, jobID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ClaimNext atomically picks the oldest queued job (FIFO) and moves it to
|
||||
// `processing`. Safe for concurrent workers: `FOR UPDATE SKIP LOCKED` blocks
|
||||
// the row as part of the same statement. Empty queue → ErrNoQueuedJobs.
|
||||
func (o *OcrJobs) ClaimNext() (OcrJobRow, error) {
|
||||
var row OcrJobRow
|
||||
err := o.DB.QueryRow(
|
||||
`WITH next AS (
|
||||
SELECT job_id FROM ocr_jobs
|
||||
WHERE status = 'queued'
|
||||
ORDER BY created_at, job_id
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE ocr_jobs
|
||||
SET status = 'processing', started_at = NOW()
|
||||
FROM next
|
||||
WHERE ocr_jobs.job_id = next.job_id
|
||||
RETURNING ocr_jobs.job_id, ocr_jobs.file_id, ocr_jobs.device_id, ocr_jobs.status`,
|
||||
).Scan(&row.ID, &row.FileID, &row.DeviceID, &row.Status)
|
||||
if err == sql.ErrNoRows {
|
||||
return OcrJobRow{}, ErrNoQueuedJobs
|
||||
}
|
||||
if err != nil {
|
||||
return OcrJobRow{}, err
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
// ResetStaleProcessing requeues jobs stuck in `processing` (worker crash /
|
||||
// processus redémarré) et plus vieux que `olderThan`. Retourne le nb de jobs
|
||||
// requeued.
|
||||
func (o *OcrJobs) ResetStaleProcessing(olderThan time.Duration) (int64, error) {
|
||||
res, err := o.DB.Exec(
|
||||
`UPDATE ocr_jobs SET status = 'queued', started_at = NULL
|
||||
WHERE status = 'processing' AND started_at < $1`,
|
||||
time.Now().Add(-olderThan),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
func (o *OcrJobs) Complete(deviceID, jobID, text string) error {
|
||||
_, err := o.DB.Exec(
|
||||
`UPDATE ocr_jobs SET status = 'done', text = NULLIF($3, ''), started_at = COALESCE(started_at, NOW()), completed_at = NOW()
|
||||
|
||||
Reference in New Issue
Block a user