From bb93f99911f83d0782d29c6c542fc30b84ee3a7b Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Mon, 11 Aug 2025 15:42:57 +0300 Subject: [PATCH] Fix job fields naiming --- internal/controller/http/transcribe.go | 4 +- internal/entity/job.go | 20 +++++-- internal/repo/sqlite/transcript_job_repo.go | 60 ++++++++++++------- .../002_create_transcribe_jobs_table.sql | 12 ++-- 4 files changed, 62 insertions(+), 34 deletions(-) diff --git a/internal/controller/http/transcribe.go b/internal/controller/http/transcribe.go index 59972ba..d437b55 100644 --- a/internal/controller/http/transcribe.go +++ b/internal/controller/http/transcribe.go @@ -93,12 +93,14 @@ func (h *TranscribeHandler) CreateTranscribeJob(c *gin.Context) { // Создаем запись в таблице transcribe_jobs jobId := uuid.New().String() + now := time.Now() job := &entity.TranscribeJob{ Id: jobId, State: entity.StateCreated, FileID: &fileId, IsError: false, - CreatedAt: time.Now(), + CreatedAt: now, + UpdatedAt: now, } if err := h.jobRepo.Create(job); err != nil { diff --git a/internal/entity/job.go b/internal/entity/job.go index b9ebcf1..3db7f4e 100644 --- a/internal/entity/job.go +++ b/internal/entity/job.go @@ -10,11 +10,13 @@ type TranscribeJob struct { FileID *string IsError bool ErrorText *string - Worker *string - AcquiredAt *time.Time - CreatedAt time.Time + AcquisitionID *string + AcquireTime *time.Time + DelayTime *time.Time RecognitionOpID *string // ID операции распознавания в Yandex Cloud TranscriptionText *string // Результат распознавания + CreatedAt time.Time + UpdatedAt time.Time } const ( @@ -28,6 +30,14 @@ const ( func (j *TranscribeJob) MoveToState(state string) { j.State = state - j.Worker = nil - j.AcquiredAt = nil + j.DelayTime = nil + j.AcquisitionID = nil + j.AcquireTime = nil + j.UpdatedAt = time.Now() +} + +func (j *TranscribeJob) MoveToStateAndDelay(state string, delay *time.Time) { + j.MoveToState(state) + j.DelayTime = delay + j.UpdatedAt = time.Now() } diff --git a/internal/repo/sqlite/transcript_job_repo.go b/internal/repo/sqlite/transcript_job_repo.go index cd69e7b..d041efe 100644 --- a/internal/repo/sqlite/transcript_job_repo.go +++ b/internal/repo/sqlite/transcript_job_repo.go @@ -26,11 +26,13 @@ func (repo *TranscriptJobRepository) Create(job *entity.TranscribeJob) error { "file_id": job.FileID, "is_error": job.IsError, "error_text": job.ErrorText, - "worker": job.Worker, - "acquired_at": job.AcquiredAt, - "created_at": job.CreatedAt, + "acquisition_id": job.AcquisitionID, + "acquire_time": job.AcquireTime, + "delay_time": job.DelayTime, "recognition_op_id": job.RecognitionOpID, "transcription_text": job.TranscriptionText, + "created_at": job.CreatedAt, + "updated_at": job.UpdatedAt, } query := repo.gq.Insert("transcribe_jobs").Rows(record) sql, args, err := query.ToSQL() @@ -52,10 +54,12 @@ func (repo *TranscriptJobRepository) Save(job *entity.TranscribeJob) error { "file_id": job.FileID, "is_error": job.IsError, "error_text": job.ErrorText, - "worker": job.Worker, - "acquired_at": job.AcquiredAt, + "acquisition_id": job.AcquisitionID, + "acquire_time": job.AcquireTime, + "delay_time": job.DelayTime, "recognition_op_id": job.RecognitionOpID, "transcription_text": job.TranscriptionText, + "updated_at": job.UpdatedAt, } query := repo.gq.Update("transcribe_jobs").Set(record).Where(goqu.C("id").Eq(job.Id)) sql, args, err := query.ToSQL() @@ -78,11 +82,13 @@ func (repo *TranscriptJobRepository) GetByID(id string) (*entity.TranscribeJob, "file_id", "is_error", "error_text", - "worker", - "acquired_at", - "created_at", + "acquisition_id", + "acquire_time", + "delay_time", "recognition_op_id", "transcription_text", + "created_at", + "updated_at", ).Where(goqu.C("id").Eq(id)) sql, args, err := query.ToSQL() if err != nil { @@ -96,11 +102,13 @@ func (repo *TranscriptJobRepository) GetByID(id string) (*entity.TranscribeJob, &job.FileID, &job.IsError, &job.ErrorText, - &job.Worker, - &job.AcquiredAt, - &job.CreatedAt, + &job.AcquisitionID, + &job.AcquireTime, + &job.DelayTime, &job.RecognitionOpID, &job.TranscriptionText, + &job.CreatedAt, + &job.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("failed to get transcribe job: %w", err) @@ -113,8 +121,8 @@ func (repo *TranscriptJobRepository) FindAndAcquire(state, acquisitionId string, updateQuery := repo.gq.Update("transcribe_jobs"). Set( goqu.Record{ - "worker": acquisitionId, - "acquired_at": time.Now(), + "acquisition_id": acquisitionId, + "acquire_time": time.Now(), }, ). Where( @@ -125,8 +133,12 @@ func (repo *TranscriptJobRepository) FindAndAcquire(state, acquisitionId string, goqu.C("state").Eq(state), goqu.C("is_error").Eq(0), goqu.Or( - goqu.C("worker").IsNull(), - goqu.C("acquired_at").Lt(rottingTime), + goqu.C("delay_time").IsNull(), + goqu.C("delay_time").Lt(time.Now()), + ), + goqu.Or( + goqu.C("acquisition_id").IsNull(), + goqu.C("acquire_time").Lt(rottingTime), ), ), ). @@ -159,12 +171,14 @@ func (repo *TranscriptJobRepository) FindAndAcquire(state, acquisitionId string, "file_id", "is_error", "error_text", - "worker", - "acquired_at", - "created_at", + "acquisition_id", + "acquire_time", + "delay_time", "recognition_op_id", "transcription_text", - ).Where(goqu.C("worker").Eq(acquisitionId)) + "created_at", + "updated_at", + ).Where(goqu.C("acquisition_id").Eq(acquisitionId)) sql, args, err = selectQuery.ToSQL() if err != nil { @@ -178,11 +192,13 @@ func (repo *TranscriptJobRepository) FindAndAcquire(state, acquisitionId string, &job.FileID, &job.IsError, &job.ErrorText, - &job.Worker, - &job.AcquiredAt, - &job.CreatedAt, + &job.AcquisitionID, + &job.AcquireTime, + &job.DelayTime, &job.RecognitionOpID, &job.TranscriptionText, + &job.CreatedAt, + &job.UpdatedAt, ) if err != nil { return nil, fmt.Errorf("failed to get transcribe job: %w", err) diff --git a/migrations/002_create_transcribe_jobs_table.sql b/migrations/002_create_transcribe_jobs_table.sql index 55b658e..4255e83 100644 --- a/migrations/002_create_transcribe_jobs_table.sql +++ b/migrations/002_create_transcribe_jobs_table.sql @@ -2,17 +2,17 @@ CREATE TABLE transcribe_jobs ( id TEXT PRIMARY KEY, state TEXT NOT NULL, - file_id TEXT, + delay_time DATETIME, - is_error BOOLEAN NOT NULL, - error_text TEXT, + file_id TEXT, + recognition_op_id TEXT, + transcription_text TEXT, acquisition_id TEXT, acquire_time DATETIME, - delay_time DATETIME, - recognition_op_id TEXT, - transcription_text TEXT, + is_error BOOLEAN NOT NULL, + error_text TEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL,