Change logger to slog
This commit is contained in:
@@ -3,7 +3,7 @@ package service
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -28,6 +28,7 @@ type TranscribeService struct {
|
||||
metaviewer contract.AudioMetaViewer
|
||||
converter contract.AudioFileConverter
|
||||
recognizer contract.AudioRecognizer
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewTranscribeService(
|
||||
@@ -36,6 +37,7 @@ func NewTranscribeService(
|
||||
metaviewer contract.AudioMetaViewer,
|
||||
converter contract.AudioFileConverter,
|
||||
recognizer contract.AudioRecognizer,
|
||||
logger *slog.Logger,
|
||||
) *TranscribeService {
|
||||
return &TranscribeService{
|
||||
jobRepo: jobRepo,
|
||||
@@ -43,6 +45,7 @@ func NewTranscribeService(
|
||||
metaviewer: metaviewer,
|
||||
converter: converter,
|
||||
recognizer: recognizer,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,9 +63,15 @@ func (s *TranscribeService) CreateTranscribeJob(file io.Reader, fileName string)
|
||||
storageFileName := fmt.Sprintf("%s%s", fileId, ext)
|
||||
storageFilePath := filepath.Join(baseStorageDir, storageFileName)
|
||||
|
||||
s.logger.Info("Creating transcribe job",
|
||||
"file_id", fileId,
|
||||
"file_name", fileName,
|
||||
"storage_path", storageFilePath)
|
||||
|
||||
// Создаем файл на диске
|
||||
dst, err := os.Create(storageFilePath)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to create file", "error", err, "path", storageFilePath)
|
||||
return nil, err
|
||||
}
|
||||
defer dst.Close()
|
||||
@@ -70,18 +79,26 @@ func (s *TranscribeService) CreateTranscribeJob(file io.Reader, fileName string)
|
||||
// Копируем содержимое загруженного файла
|
||||
size, err := io.Copy(dst, file)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to copy file content", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := dst.Close(); err != nil {
|
||||
s.logger.Error("Failed to close file", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info, err := s.metaviewer.GetInfo(storageFilePath)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get file info", "error", err, "path", storageFilePath)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("File uploaded successfully",
|
||||
"file_id", fileId,
|
||||
"size", size,
|
||||
"duration_seconds", info.Seconds)
|
||||
|
||||
metrics.InputFileDurationHistogram.WithLabelValues().Observe(float64(info.Seconds))
|
||||
metrics.InputFileSizeHistogram.WithLabelValues(ext).Observe(float64(size))
|
||||
|
||||
@@ -97,6 +114,7 @@ func (s *TranscribeService) CreateTranscribeJob(file io.Reader, fileName string)
|
||||
if err := s.fileRepo.Create(fileRecord); err != nil {
|
||||
// Удаляем файл если не удалось создать запись в БД
|
||||
os.Remove(storageFilePath)
|
||||
s.logger.Error("Failed to create file record", "error", err, "file_id", fileId)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -114,9 +132,11 @@ func (s *TranscribeService) CreateTranscribeJob(file io.Reader, fileName string)
|
||||
}
|
||||
|
||||
if err := s.jobRepo.Create(job); err != nil {
|
||||
s.logger.Error("Failed to create job record", "error", err, "job_id", jobId)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Transcribe job created successfully", "job_id", jobId, "file_id", fileId)
|
||||
return job, nil
|
||||
}
|
||||
|
||||
@@ -129,11 +149,15 @@ func (s *TranscribeService) FindAndRunConversionJob() error {
|
||||
if _, ok := err.(*contract.JobNotFoundError); ok {
|
||||
return &contract.NoopJobError{State: entity.StateCreated}
|
||||
}
|
||||
s.logger.Error("Failed to find and acquire conversion job", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Starting conversion job", "job_id", job.Id, "acquisition_id", acquisitionId)
|
||||
|
||||
srcFile, err := s.fileRepo.GetByID(*job.FileID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get source file", "error", err, "file_id", *job.FileID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -149,6 +173,12 @@ func (s *TranscribeService) FindAndRunConversionJob() error {
|
||||
srcExt = defaultAudioExt
|
||||
}
|
||||
|
||||
s.logger.Info("Converting file",
|
||||
"job_id", job.Id,
|
||||
"src_path", srcFilePath,
|
||||
"dest_path", destFilePath,
|
||||
"src_format", srcExt)
|
||||
|
||||
// Измеряем время конвертации
|
||||
startTime := time.Now()
|
||||
err = s.converter.Convert(srcFilePath, destFilePath)
|
||||
@@ -160,14 +190,24 @@ func (s *TranscribeService) FindAndRunConversionJob() error {
|
||||
Observe(conversionDuration.Seconds())
|
||||
|
||||
if err != nil {
|
||||
s.logger.Error("File conversion failed",
|
||||
"error", err,
|
||||
"job_id", job.Id,
|
||||
"duration", conversionDuration)
|
||||
return err
|
||||
}
|
||||
|
||||
stat, err := os.Stat(destFilePath)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to stat converted file", "error", err, "path", destFilePath)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("File conversion completed",
|
||||
"job_id", job.Id,
|
||||
"duration", conversionDuration,
|
||||
"output_size", stat.Size())
|
||||
|
||||
// Записываем метрику размера выходного файла
|
||||
metrics.OutputFileSizeHistogram.WithLabelValues("ogg").Observe(float64(stat.Size()))
|
||||
|
||||
@@ -185,14 +225,17 @@ func (s *TranscribeService) FindAndRunConversionJob() error {
|
||||
|
||||
err = s.fileRepo.Create(destFileRecord)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to create converted file record", "error", err, "file_id", destFileId)
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.jobRepo.Save(job)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save job", "error", err, "job_id", job.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Conversion job completed successfully", "job_id", job.Id)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -205,11 +248,15 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
if _, ok := err.(*contract.JobNotFoundError); ok {
|
||||
return &contract.NoopJobError{State: entity.StateConverted}
|
||||
}
|
||||
s.logger.Error("Failed to find and acquire transcribe job", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Starting transcribe job", "job_id", jobRecord.Id, "acquisition_id", acquisitionId)
|
||||
|
||||
fileRecord, err := s.fileRepo.GetByID(*jobRecord.FileID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get file record", "error", err, "file_id", *jobRecord.FileID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -217,6 +264,7 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to open file", "error", err, "path", filePath)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
@@ -224,12 +272,19 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
destFileId := uuid.NewString()
|
||||
destFileRecord := fileRecord.CopyWithStorage(destFileId, entity.StorageS3)
|
||||
|
||||
s.logger.Info("Starting recognition", "job_id", jobRecord.Id, "file_path", filePath)
|
||||
|
||||
// Запускаем асинхронное распознавание
|
||||
operationID, err := s.recognizer.Recognize(file, destFileRecord.FileName)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to start recognition", "error", err, "job_id", jobRecord.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Recognition started",
|
||||
"job_id", jobRecord.Id,
|
||||
"operation_id", operationID)
|
||||
|
||||
// Обновляем задачу с ID операции распознавания
|
||||
jobRecord.FileID = &destFileId
|
||||
jobRecord.RecognitionOpID = &operationID
|
||||
@@ -238,14 +293,17 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
|
||||
err = s.fileRepo.Create(destFileRecord)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to create S3 file record", "error", err, "file_id", destFileId)
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.jobRepo.Save(jobRecord)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save job", "error", err, "job_id", jobRecord.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Transcribe job updated successfully", "job_id", jobRecord.Id)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -258,29 +316,33 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
if _, ok := err.(*contract.JobNotFoundError); ok {
|
||||
return &contract.NoopJobError{State: entity.StateTranscribe}
|
||||
}
|
||||
s.logger.Error("Failed to find and acquire transcribe check job", "error", err)
|
||||
return fmt.Errorf("failed find and acquire job: %s, %w", entity.StateTranscribe, err)
|
||||
}
|
||||
|
||||
if job.RecognitionOpID == nil {
|
||||
s.logger.Error("Recognition operation ID not found", "job_id", job.Id)
|
||||
return fmt.Errorf("recogniton opId not found for job: %s", job.Id)
|
||||
}
|
||||
|
||||
opId := *job.RecognitionOpID
|
||||
|
||||
// Проверяем статус операции
|
||||
log.Printf("Check operation status: id %s\n", opId)
|
||||
s.logger.Info("Checking operation status", "job_id", job.Id, "operation_id", opId)
|
||||
recResult, err := s.recognizer.CheckRecognitionStatus(opId)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to check recognition status", "error", err, "operation_id", opId)
|
||||
return err
|
||||
}
|
||||
|
||||
if recResult.IsInProgress() {
|
||||
// Операция еще не завершена, оставляем в статусе обработки
|
||||
log.Printf("Operation in progress: id %s\n", opId)
|
||||
s.logger.Info("Operation in progress", "job_id", job.Id, "operation_id", opId)
|
||||
delayTime := time.Now().Add(10 * time.Second)
|
||||
job.MoveToStateAndDelay(entity.StateTranscribe, &delayTime)
|
||||
err := s.jobRepo.Save(job)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save job", "error", err, "job_id", job.Id)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -288,10 +350,14 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
|
||||
if recResult.IsFailed() {
|
||||
errorText := recResult.GetError()
|
||||
log.Printf("Operation failed: id %s, message %s\n", opId, errorText)
|
||||
s.logger.Error("Operation failed",
|
||||
"job_id", job.Id,
|
||||
"operation_id", opId,
|
||||
"error_message", errorText)
|
||||
job.Fail(errorText)
|
||||
err := s.jobRepo.Save(job)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save failed job", "error", err, "job_id", job.Id)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -300,18 +366,24 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
// Операция завершена, получаем результат
|
||||
transcriptionText, err := s.recognizer.GetRecognitionText(opId)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get recognition text", "error", err, "operation_id", opId)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Operation done: id %s\n", opId)
|
||||
s.logger.Info("Operation completed successfully",
|
||||
"job_id", job.Id,
|
||||
"operation_id", opId,
|
||||
"text_length", len(transcriptionText))
|
||||
|
||||
// Обновляем задачу с результатом
|
||||
job.Done(transcriptionText)
|
||||
|
||||
err = s.jobRepo.Save(job)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save completed job", "error", err, "job_id", job.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Transcribe check job completed successfully", "job_id", job.Id)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user