diff --git a/internal/storage/mongo/jobs.go b/internal/storage/mongo/jobs.go index 41fb1c4..fbc0a3a 100644 --- a/internal/storage/mongo/jobs.go +++ b/internal/storage/mongo/jobs.go @@ -7,6 +7,7 @@ package mongo import ( + "context" "time" "forge.chapril.org/dune/jw" @@ -20,21 +21,7 @@ func (ms *mongoStorage) ValidateJob(_ *jw.Job) error { return nil } -func (ms *mongoStorage) InsertJob(job *jw.Job) (bool, error) { - col := ms.Collection("jobs") - - ctx, cancel := util.CtxWithTimeout(5 * time.Second) - defer cancel() - - if job.Category == nil { - _, err := col.InsertOne(ctx, job) - if err != nil { - return false, err - } - - return true, nil - } - +func (ms *mongoStorage) txInsertJob(ctx context.Context, job *jw.Job, col *mongo.Collection) (bool, error) { session, err := ms.client.StartSession() if err != nil { return false, err @@ -75,6 +62,24 @@ func (ms *mongoStorage) InsertJob(job *jw.Job) (bool, error) { return result.(bool), err } +func (ms *mongoStorage) InsertJob(job *jw.Job) (bool, error) { + col := ms.Collection("jobs") + + ctx, cancel := util.CtxWithTimeout(5 * time.Second) + defer cancel() + + if job.Category == nil { + _, err := col.InsertOne(ctx, job) + if err != nil { + return false, err + } + + return true, nil + } + + return ms.txInsertJob(ctx, job, col) +} + func (ms *mongoStorage) UpdateJob(job *jw.Job) error { return nil }