Example_completeJobWithinTx demonstrates how to transactionally complete a job alongside other database changes being made.
()
| 56 | // Example_completeJobWithinTx demonstrates how to transactionally complete |
| 57 | // a job alongside other database changes being made. |
| 58 | func Example_completeJobWithinTx() { |
| 59 | ctx := context.Background() |
| 60 | |
| 61 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 62 | if err != nil { |
| 63 | panic(err) |
| 64 | } |
| 65 | defer dbPool.Close() |
| 66 | |
| 67 | workers := river.NewWorkers() |
| 68 | river.AddWorker(workers, &TransactionalWorker{dbPool: dbPool}) |
| 69 | |
| 70 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{ |
| 71 | Queues: map[string]river.QueueConfig{ |
| 72 | river.QueueDefault: {MaxWorkers: 100}, |
| 73 | }, |
| 74 | Workers: workers, |
| 75 | })) |
| 76 | if err != nil { |
| 77 | panic(err) |
| 78 | } |
| 79 | |
| 80 | // Not strictly needed, but used to help this test wait until job is worked. |
| 81 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted) |
| 82 | defer subscribeCancel() |
| 83 | |
| 84 | if err := riverClient.Start(ctx); err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | |
| 88 | if _, err = riverClient.Insert(ctx, TransactionalArgs{}, nil); err != nil { |
| 89 | panic(err) |
| 90 | } |
| 91 | |
| 92 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 93 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1) |
| 94 | |
| 95 | if err := riverClient.Stop(ctx); err != nil { |
| 96 | panic(err) |
| 97 | } |
| 98 | |
| 99 | // Output: |
| 100 | // Transitioned TransactionalWorker job from "running" to "completed" |
| 101 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…