MCPcopy Index your code
hub / github.com/riverqueue/river / ExampleClientFromContext_pgx

Function ExampleClientFromContext_pgx

example_client_from_context_test.go:39–82  ·  view source on GitHub ↗

ExampleClientFromContext_pgx demonstrates how to extract the River client from the worker context when using the pgx/v5 driver. ([github.com/riverqueue/river/riverdriver/riverpgxv5]).

()

Source from the content-addressed store, hash-verified

37// from the worker context when using the pgx/v5 driver.
38// ([github.com/riverqueue/river/riverdriver/riverpgxv5]).
39func ExampleClientFromContext_pgx() {
40 ctx := context.Background()
41
42 dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL())
43 if err != nil {
44 panic(err)
45 }
46 defer dbPool.Close()
47
48 workers := river.NewWorkers()
49 river.AddWorker(workers, &ContextClientWorker{})
50
51 riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{
52 ID: "ClientFromContextClient",
53 Queues: map[string]river.QueueConfig{
54 river.QueueDefault: {MaxWorkers: 10},
55 },
56 Workers: workers,
57 }))
58 if err != nil {
59 panic(err)
60 }
61
62 // Not strictly needed, but used to help this test wait until job is worked.
63 subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted)
64 defer subscribeCancel()
65
66 if err := riverClient.Start(ctx); err != nil {
67 panic(err)
68 }
69 if _, err = riverClient.Insert(ctx, ContextClientArgs{}, nil); err != nil {
70 panic(err)
71 }
72
73 // Wait for jobs to complete. Only needed for purposes of the example test.
74 riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1)
75
76 if err := riverClient.Stop(ctx); err != nil {
77 panic(err)
78 }
79
80 // Output:
81 // client found in context, id=ClientFromContextClient
82}

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…