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.
This commit is contained in:
parent
7b04822a9b
commit
393836a86a
30 changed files with 1126 additions and 72 deletions
|
|
@ -10,13 +10,13 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/stephenafamo/scan"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
|
||||
modelfileupload "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/fileupload/model"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/db/models"
|
||||
queryfileupload "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/fileupload"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/lint"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/background"
|
||||
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/file"
|
||||
|
|
@ -82,34 +82,34 @@ func GetUploadDetail(ctx context.Context, organization_id int32, file_id int32)
|
|||
return nil, errors.New("No idea what to do with upload type")
|
||||
}
|
||||
|
||||
func NewUpload(ctx context.Context, u User, upload file.Upload, t enums.FileuploadCsvtype) (*int32, error) {
|
||||
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
|
||||
func NewUpload(ctx context.Context, u User, upload file.Upload, t modelfileupload.Csvtype) (*int32, error) {
|
||||
txn, err := db.BeginTxn(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to begin transaction: %w", err)
|
||||
}
|
||||
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
|
||||
|
||||
file, err := models.FileuploadFiles.Insert(&models.FileuploadFileSetter{
|
||||
ContentType: omit.From(upload.ContentType),
|
||||
Created: omit.From(time.Now()),
|
||||
CreatorID: omit.From(int32(u.ID)),
|
||||
Deleted: omitnull.FromPtr[time.Time](nil),
|
||||
Error: omit.From(""),
|
||||
Name: omit.From(upload.Name),
|
||||
OrganizationID: omit.From(u.Organization.ID),
|
||||
Status: omit.From(enums.FileuploadFilestatustypeUploaded),
|
||||
SizeBytes: omit.From(int32(upload.SizeBytes)),
|
||||
FileUUID: omit.From(upload.UUID),
|
||||
}).One(ctx, txn)
|
||||
file, err := queryfileupload.FileInsert(ctx, txn, modelfileupload.File{
|
||||
ContentType: upload.ContentType,
|
||||
Created: time.Now(),
|
||||
CreatorID: int32(u.ID),
|
||||
Deleted: nil,
|
||||
Error: "",
|
||||
Name: upload.Name,
|
||||
OrganizationID: u.Organization.ID,
|
||||
Status: modelfileupload.Filestatustype_Uploaded,
|
||||
SizeBytes: int32(upload.SizeBytes),
|
||||
FileUUID: upload.UUID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create file upload: %w", err)
|
||||
}
|
||||
_, err = models.FileuploadCSVS.Insert(&models.FileuploadCSVSetter{
|
||||
Committed: omitnull.FromPtr[time.Time](nil),
|
||||
FileID: omit.From(file.ID),
|
||||
Rowcount: omit.From(int32(0)),
|
||||
Type: omit.From(t),
|
||||
}).One(ctx, txn)
|
||||
_, err = queryfileupload.CSVInsert(ctx, txn, modelfileupload.Csv{
|
||||
Committed: nil,
|
||||
FileID: file.ID,
|
||||
Rowcount: 0,
|
||||
Type: t,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to create csv: %w", err)
|
||||
}
|
||||
|
|
@ -124,19 +124,13 @@ func NewUpload(ctx context.Context, u User, upload file.Upload, t enums.Fileuplo
|
|||
return &file.ID, nil
|
||||
}
|
||||
func UploadCommit(ctx context.Context, org Organization, file_id int32, committer User) error {
|
||||
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
|
||||
txn, err := db.BeginTxn(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to begin transaction: %w", err)
|
||||
}
|
||||
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
|
||||
|
||||
_, err = psql.Update(
|
||||
um.Table(models.FileuploadFiles.Alias()),
|
||||
um.SetCol("status").ToArg("committing"),
|
||||
um.SetCol("committer").ToArg(committer.ID),
|
||||
um.Where(psql.Quote("id").EQ(psql.Arg(file_id))),
|
||||
um.Where(psql.Quote("organization_id").EQ(psql.Arg(org.ID))),
|
||||
).Exec(ctx, txn)
|
||||
err = queryfileupload.FileUpdateCommitting(ctx, txn, int64(org.ID), int64(file_id), int64(committer.ID))
|
||||
if err != nil {
|
||||
return fmt.Errorf("update upload: %w", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue