Add conversion job logic
This commit is contained in:
@@ -3,6 +3,8 @@ package sqlite
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
@@ -42,6 +44,29 @@ func (repo *TranscriptJobRepository) Create(job *entity.TranscribeJob) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *TranscriptJobRepository) Save(job *entity.TranscribeJob) error {
|
||||
record := goqu.Record{
|
||||
"state": job.State,
|
||||
"file_id": job.FileID,
|
||||
"is_error": job.IsError,
|
||||
"error_text": job.ErrorText,
|
||||
"worker": job.Worker,
|
||||
"acquired_at": job.AcquiredAt,
|
||||
}
|
||||
query := repo.gq.Update("transcribe_jobs").Set(record).Where(goqu.C("id").Eq(job.Id))
|
||||
sql, args, err := query.ToSQL()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to build query: %w", err)
|
||||
}
|
||||
|
||||
_, err = repo.db.Exec(sql, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update transcribe job: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *TranscriptJobRepository) GetByID(id string) (*entity.TranscribeJob, error) {
|
||||
query := repo.gq.From("transcribe_jobs").Select(
|
||||
"id",
|
||||
@@ -75,3 +100,81 @@ func (repo *TranscriptJobRepository) GetByID(id string) (*entity.TranscribeJob,
|
||||
|
||||
return &job, nil
|
||||
}
|
||||
|
||||
func (repo *TranscriptJobRepository) FindAndAcquire(state, acquisitionId string, rottingTime time.Time) (*entity.TranscribeJob, error) {
|
||||
updateQuery := repo.gq.Update("transcribe_jobs").
|
||||
Set(
|
||||
goqu.Record{
|
||||
"worker": acquisitionId,
|
||||
"acquired_at": time.Now(),
|
||||
},
|
||||
).
|
||||
Where(
|
||||
goqu.C("id").Eq(
|
||||
repo.gq.From("transcribe_jobs").Select("id").
|
||||
Where(
|
||||
goqu.And(
|
||||
goqu.C("state").Eq(state),
|
||||
goqu.C("is_error").Eq(0),
|
||||
goqu.Or(
|
||||
goqu.C("worker").IsNull(),
|
||||
goqu.C("acquired_at").Lt(rottingTime),
|
||||
),
|
||||
),
|
||||
).
|
||||
Limit(1),
|
||||
),
|
||||
)
|
||||
|
||||
sql, args, err := updateQuery.ToSQL()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build query: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("aquire sql: %s", sql)
|
||||
|
||||
result, err := repo.db.Exec(sql, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to aquire job with state %s: %w", state, err)
|
||||
}
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed check affected rows: %w", err)
|
||||
}
|
||||
if rowsAffected != 1 {
|
||||
return nil, fmt.Errorf("unexpected affected rows count: %d", rowsAffected)
|
||||
}
|
||||
|
||||
selectQuery := repo.gq.From("transcribe_jobs").Select(
|
||||
"id",
|
||||
"state",
|
||||
"file_id",
|
||||
"is_error",
|
||||
"error_text",
|
||||
"worker",
|
||||
"acquired_at",
|
||||
"created_at",
|
||||
).Where(goqu.C("worker").Eq(acquisitionId))
|
||||
|
||||
sql, args, err = selectQuery.ToSQL()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build query: %w", err)
|
||||
}
|
||||
|
||||
var job entity.TranscribeJob
|
||||
err = repo.db.QueryRow(sql, args...).Scan(
|
||||
&job.Id,
|
||||
&job.State,
|
||||
&job.FileID,
|
||||
&job.IsError,
|
||||
&job.ErrorText,
|
||||
&job.Worker,
|
||||
&job.AcquiredAt,
|
||||
&job.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get transcribe job: %w", err)
|
||||
}
|
||||
|
||||
return &job, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user