Move comms work to background goroutine
This is a sort of random checkpoint of work * add schema for tracking messages sent to DB * add terms of service and privacy policy for RCS compliance * standardize some things about background workers * update some missing stuff from generated DB code
This commit is contained in:
parent
8f44e57c72
commit
842e6cff43
47 changed files with 7361 additions and 179 deletions
101
background/audio.go
Normal file
101
background/audio.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package background
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/queue"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/userfile"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// StartAudioWorker initializes the audio job channel and starts the worker goroutine.
|
||||
func StartWorkerAudio(ctx context.Context, audioJobChannel chan queue.JobAudio) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Info().Msg("Audio worker shutting down.")
|
||||
return
|
||||
case job := <-audioJobChannel:
|
||||
log.Info().Str("uuid", job.AudioUUID.String()).Msg("Processing audio job")
|
||||
err := processAudioFile(job.AudioUUID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("uuid", job.AudioUUID.String()).Msg("Error processing audio file")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func normalizeAudio(audioUUID uuid.UUID) error {
|
||||
source := userfile.AudioFileContentPathRaw(audioUUID.String())
|
||||
_, err := os.Stat(source)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
log.Warn().Str("source", source).Msg("file doesn't exist, skipping normalization")
|
||||
return nil
|
||||
}
|
||||
log.Info().Str("sourcce", source).Msg("Normalizing")
|
||||
destination := userfile.AudioFileContentPathNormalized(audioUUID.String())
|
||||
// Use "ffmpeg" directly, assuming it's in the system PATH
|
||||
cmd := exec.Command("ffmpeg", "-i", source, "-filter:a", "loudnorm", destination)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("FFmpeg output for normalization: %s", out)
|
||||
return fmt.Errorf("ffmpeg normalization failed: %v", err)
|
||||
}
|
||||
err = db.NoteAudioNormalized(audioUUID.String())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update database for normalized audio %s: %v", audioUUID, err)
|
||||
}
|
||||
log.Info().Str("destination", destination).Msg("Normalized audio")
|
||||
return nil
|
||||
}
|
||||
|
||||
func processAudioFile(audioUUID uuid.UUID) error {
|
||||
// Normalize audio
|
||||
err := normalizeAudio(audioUUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to normalize audio %s: %v", audioUUID, err)
|
||||
}
|
||||
|
||||
// Transcode to OGG
|
||||
err = transcodeToOgg(audioUUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to transcode audio %s to OGG: %v", audioUUID, err)
|
||||
}
|
||||
|
||||
queue.EnqueueLabelStudioJob(queue.LabelStudioJob{
|
||||
UUID: audioUUID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func transcodeToOgg(audioUUID uuid.UUID) error {
|
||||
source := userfile.AudioFileContentPathNormalized(audioUUID.String())
|
||||
_, err := os.Stat(source)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
log.Warn().Str("source", source).Msg("file doesn't exist, skipping OGG transcoding")
|
||||
return nil
|
||||
}
|
||||
log.Info().Str("source", source).Msg("Transcoding to ogg")
|
||||
destination := userfile.AudioFileContentPathOgg(audioUUID.String())
|
||||
// Use "ffmpeg" directly, assuming it's in the system PATH
|
||||
cmd := exec.Command("ffmpeg", "-i", source, "-vn", "-acodec", "libvorbis", destination)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Bytes("out", out).Msg("FFmpeg output for OGG transcoding")
|
||||
return fmt.Errorf("ffmpeg OGG transcoding failed: %v", err)
|
||||
}
|
||||
err = db.NoteAudioTranscodedToOgg(audioUUID.String())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update database for OGG transcoded audio %s: %v", audioUUID, err)
|
||||
}
|
||||
log.Info().Str("destination", destination).Msg("Transcoded audio")
|
||||
return nil
|
||||
}
|
||||
52
background/comms.go
Normal file
52
background/comms.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package background
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/queue"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func StartWorkerEmail(ctx context.Context, channel chan queue.JobEmail) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Info().Msg("Email worker shutting down.")
|
||||
return
|
||||
case job := <-channel:
|
||||
err := processJobEmail(job)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("dest", job.Destination).Str("type", string(job.Type)).Msg("Error processing audio file")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func StartWorkerSMS(ctx context.Context, channel chan queue.JobSMS) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Info().Msg("Email worker shutting down.")
|
||||
return
|
||||
case job := <-channel:
|
||||
err := processJobSMS(job)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("dest", job.Destination).Str("type", string(job.Type)).Msg("Error processing audio file")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func processJobEmail(job queue.JobEmail) error {
|
||||
log.Info().Str("dest", job.Destination).Str("type", string(job.Type)).Msg("Pretend doing email job")
|
||||
return nil
|
||||
}
|
||||
|
||||
func processJobSMS(job queue.JobSMS) error {
|
||||
log.Info().Str("dest", job.Destination).Str("type", string(job.Type)).Msg("Pretend doing email job")
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue