MCPcopy
hub / github.com/riverqueue/river / Example_completeJobWithinTx

Function Example_completeJobWithinTx

example_complete_job_within_tx_test.go:58–101  ·  view source on GitHub ↗

Example_completeJobWithinTx demonstrates how to transactionally complete a job alongside other database changes being made.

()

Source from the content-addressed store, hash-verified

56// Example_completeJobWithinTx demonstrates how to transactionally complete
57// a job alongside other database changes being made.
58func 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}

Callers

nothing calls this directly

Calls 13

TestDatabaseURLFunction · 0.92
NewWorkersFunction · 0.92
AddWorkerFunction · 0.92
NewClientFunction · 0.92
NewFunction · 0.92
WaitOrTimeoutNFunction · 0.92
PanicTBFunction · 0.92
SubscribeMethod · 0.80
InsertMethod · 0.80
initTestConfigFunction · 0.70
CloseMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…