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]).
()
| 37 | // from the worker context when using the pgx/v5 driver. |
| 38 | // ([github.com/riverqueue/river/riverdriver/riverpgxv5]). |
| 39 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…