nidus-sync/platform/background/background.go
Eli Ribble 393836a86a
Fix notification of job happening before transaction is closed
This is kind of a wild one. Turns out that the triggers I was using
actually fire before the transaction is closed and I was primarily
getting lucky that the job was present on the other side of the
connection rather than having things built correctly.

I've fixed this by removing the trigger entirely and instead manually
triggering as part of the transaction. This makes the NOTIFY call happen
as soon as the transaction closes, just at the cost of making my
application be in charge of ensuring the NOTIFY gets called. Seems like
a win.

Part of doing this is porting the existing job creation code over to use
Jet. It's something I want to do anyway, so it's a win all around.
2026-05-22 23:34:38 +00:00

55 lines
1.9 KiB
Go

package background
import (
"context"
"fmt"
"strconv"
"time"
"github.com/rs/zerolog/log"
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/public/model"
query "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/public"
)
func NewAudioTranscode(ctx context.Context, txn db.Ex, audio_id int32) error {
return newJob(ctx, txn, model.Jobtype_AudioTranscode, audio_id)
}
func NewComplianceMailer(ctx context.Context, txn db.Ex, compliance_report_request_id int32) error {
return newJob(ctx, txn, model.Jobtype_ComplianceMailerSend, compliance_report_request_id)
}
func NewCSVCommit(ctx context.Context, txn db.Ex, csv_id int32) error {
return newJob(ctx, txn, model.Jobtype_CsvCommit, csv_id)
}
func NewCSVImport(ctx context.Context, txn db.Ex, csv_id int32) error {
return newJob(ctx, txn, model.Jobtype_CsvImport, csv_id)
}
func NewEmailSend(ctx context.Context, txn db.Ex, email_id int32) error {
return newJob(ctx, txn, model.Jobtype_EmailSend, email_id)
}
func NewLabelStudioAudioCreate(ctx context.Context, txn db.Ex, note_audio_id int32) error {
return newJob(ctx, txn, model.Jobtype_LabelStudioAudioCreate, note_audio_id)
}
func NewTextRespond(ctx context.Context, txn db.Ex, text_id int32) error {
return newJob(ctx, txn, model.Jobtype_TextRespond, text_id)
}
func NewTextSend(ctx context.Context, txn db.Ex, job_id int32) error {
return newJob(ctx, txn, model.Jobtype_TextSend, job_id)
}
func newJob(ctx context.Context, txn db.Ex, t model.Jobtype, id int32) error {
job := model.Job{
Created: time.Now(),
Type: t,
RowID: id,
}
j, err := query.JobInsert(ctx, txn, job)
if err != nil {
return fmt.Errorf("insert job: %w", err)
}
err = query.JobNotify(ctx, txn, "new_job", strconv.Itoa(int(j.ID)))
if err != nil {
return fmt.Errorf("notify job: %w", err)
}
log.Debug().Int32("id", j.ID).Int32("row_id", id).Msg("created job, added notify")
return nil
}